安装包下载
下载安装包: mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar
安装新版mysql前,需将系统自带的mariadb-lib卸载
[root@hadoop01 ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.56-2.el7.x86_64
[root@hadoop01 ~]# rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64
[root@hadoop01 ~]# rpm -qa|grep mariadb
或者
yum remove mariadb
解压安装包
[root@hadoop01 mysql-5.7]# tar -xvf mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar
mysql-community-embedded-devel-5.7.26-1.el7.x86_64.rpm
mysql-community-libs-5.7.26-1.el7.x86_64.rpm
mysql-community-embedded-5.7.26-1.el7.x86_64.rpm
mysql-community-test-5.7.26-1.el7.x86_64.rpm
mysql-community-embedded-compat-5.7.26-1.el7.x86_64.rpm
mysql-community-common-5.7.26-1.el7.x86_64.rpm
mysql-community-devel-5.7.26-1.el7.x86_64.rpm
mysql-community-client-5.7.26-1.el7.x86_64.rpm
mysql-community-server-5.7.26-1.el7.x86_64.rpm
mysql-community-libs-compat-5.7.26-1.el7.x86_64.rpm
安装mysql-community-common-5.7.26-1.el7.x86_64.rpm
[root@hadoop01 mysql-5.7]# rpm -ivh mysql-community-common-5.7.26-1.el7.x86_64.rpm
warning: mysql-community-common-5.7.26-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:mysql-community-common-5.7.26-1.e################################# [100%]
安装mysql-community-libs-5.7.26-1.el7.x86_64.rpm
[root@hadoop01 mysql-5.7]# rpm -ivh mysql-community-libs-5.7.26-1.el7.x86_64.rpm
warning: mysql-community-libs-5.7.26-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:mysql-community-libs-5.7.26-1.el7################################# [100%]
安装mysql-community-client-5.7.26-1.el7.x86_64.rpm
[root@hadoop01 mysql-5.7]# rpm -ivh mysql-community-client-5.7.26-1.el7.x86_64.rpm
warning: mysql-community-client-5.7.26-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:mysql-community-client-5.7.26-1.e################################# [100%]
安装mysql-community-server-5.7.20-1.el7.x86_64.rpm
在安装之前需要安装libaio
[root@hadoop01 mysql-5.7]# rpm -qa|grep libaio
libaio-0.3.109-13.el7.x86_64
或者 yum install -y libaio
如果不存需要下载离线包:
http://mirror.centos.org/centos/6/os/x86_64/Packages/
安装libaio库:
rpm -ivh libaio-0.3.107-10.el6.x86_64.rpm(若在有网情况下可执行yum install libaio)
安装server,rpm -ivh mysql-community-server-5.7.26-1.el7.x86_64.rpm
[root@hadoop01 mysql-5.7]# rpm -ivh mysql-community-server-5.7.26-1.el7.x86_64.rpm
warning: mysql-community-server-5.7.26-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:mysql-community-server-5.7.26-1.e################################# [100%]
如果提示没有 perl 则, 安装 yum install -y perl
初始化数据库
// 指定datadir, 执行后会生成~/.mysql_secret密码文件(5.7以后不在使用)
[root@hadoop01 mysql-5.7]# mysql_install_db --datadir=/var/lib/mysql
// 初始化,执行生会在/var/log/mysqld.log生成随机密码
[root@hadoop01 mysql-5.7]# mysqld --initialize
更改mysql数据库目录的所属用户及其所属组(没用创建mysql用户)
[root@hadoop01 mysql-5.7]# chown mysql:mysql /var/lib/mysql -R
启动mysql
[root@hadoop01 mysql-5.7]# systemctl start mysqld.service
授权
[root@hadoop01 mysql-5.7]# chmod -R 777 mysql
查看效果
[root@hadoop01 mysql-5.7]# systemctl status mysqld.service
修改MySQL的登录设置
[root@hadoop01 mysql-5.7]# vim /etc/my.cnf
在[mysqld]的段中加上一句:skip-grant-tables
重新启动mysqld
[root@hadoop01 mysql-5.7]# service mysqld restart
Redirecting to /bin/systemctl restart mysqld.service
登录并修改MySQL的root密码
[root@hadoop01 mysql-5.7]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.26 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
# 修改root用户的密码
mysql> update mysql.user set authentication_string=password('你要修改的密码') where user='root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
# 刷新该表
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
# 退出MySQL
mysql> quit;
Bye
将MySQL的登录设置修改回来
重新启动mysqld
[root@hadoop01 mysql-5.7]# service mysqld restart
Redirecting to /bin/systemctl restart mysqld.service
登录到mysql,更改root用户的密码
命令可以查看初始密码
grep 'temporary password' /var/log/mysqld.log
[root@hadoop01 mysql-5.7]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.26 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
# 修改root用户的密码
mysql> set password=password('你要修改的密码');
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
# 刷新该表
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
# 退出MySQL
mysql> quit;
Bye
远程登陆授权
[root@hadoop01 mysql-5.7]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.26 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
# root用户远程登录
mysql> grant all privileges on *.* to 'root'@'%' identified by '你的密码' with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)
# 刷新表
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
# 退出MySQL
mysql> quit;
Bye
设置mysql开机启动
// 检查是否已经是开机启动
[root@hadoop01 mysql-5.7]# systemctl list-unit-files | grep mysqld
// 开机启动
[root@hadoop01 mysql-5.7]# systemctl enable mysqld.service
默认配置文件路径:
配置文件:/etc/my.cnf
日志文件:/var/log/mysqld.log
服务启动脚本:/usr/lib/systemd/system/mysqld.service
socket文件:/var/run/mysqld/mysqld.pid
配置默认编码为utf8
修改/etc/my.cnf配置文件,在[mysqld]下添加编码配置,如下所示:
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'