CentOS安装 MySQL 8.0

CentOS 7安装 MySQL 8.0 详细步骤

1、添加包

centos8:

1
2
wget https://dev.mysql.com/get/mysql80-community-release-el8-1.noarch.rpm
rpm -ivh mysql80-community-release-el8-1.noarch.rpm

centos7:

1
2
wget https://dev.mysql.com/get/mysql80-community-release-el7-7.noarch.rpm
rpm -ivh mysql80-community-release-el7-7.noarch.rpm

rpm下载页面https://dev.mysql.com/downloads/repo/yum/

版本下载页面https://dev.mysql.com/downloads/mysql/

如果是安装指定版本。到https://downloads.mysql.com/archives/community/

页面选择版本后,替换下载链接即可,例如安装8.0.20版本

1
2
3
4
5
6
7
8
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-community-common-8.0.20-1.el8.x86_64.rpm
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-community-libs-8.0.20-1.el8.x86_64.rpm
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-community-client-8.0.20-1.el8.x86_64.rpm
wget https://downloads.mysql.com/archives/get/p/23/file/mysql-community-server-8.0.20-1.el8.x86_64.rpm
rpm -ivh mysql-community-common-8.0.20-1.el8.x86_64.rpm
rpm -ivh mysql-community-libs-8.0.20-1.el8.x86_64.rpm
rpm -ivh mysql-community-client-8.0.20-1.el8.x86_64.rpm
rpm -ivh mysql-community-server-8.0.20-1.el8.x86_64.rpm
1
yum install -y  mysql-community-server

执行成功后直接跳到第4步配置文件

2、更新 yum 命令

1
yum clean all && yum makecache

3、安装

1
yum install -y  mysql-server

4、配置文件

修改配置文件

1
vim /etc/my.cnf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[mysqld]

port = 3306

character-set-server=utf8mb4
collation-server=utf8mb4_general_ci

# 表名不区分大小写(启动前配置)
lower_case_table_names=1

# 设置日志时区和系统一致
log_timestamps=SYSTEM

[client]
default-character-set=utf8mb4

5、启动服务

启动服务

1
systemctl start mysqld

查看版本信息

1
mysql -V

查看状态

1
systemctl status mysqld

开机启动

1
2
systemctl enable mysqld
systemctl daemon-reload

6、修改账号密码

1、查看MySQL为Root账号生成的临时密码

1
grep "A temporary password" /var/log/mysqld.log

2、进入MySQL shell

1
mysql -u root -p

3、修改密码

1
ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';

7 、开启 MySQL 远程连接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 选择 mysql 数据库:
USE mysql;

# 在 mysql 数据库的 user 表中查看当前 root 用户的相关信息:
SELECT host, user, authentication_string, plugin FROM user;

# 设置root 用户远程访问:
update user set host = '%' where user ='root';

# 刷新权限:
FLUSH PRIVILEGES;

# 授权的所有权限
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';

# 更新 root 用户密码及加密规则(如果客户端不支持加密插件):
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'MyNewPass4!';
# 刷新权限:
FLUSH PRIVILEGES;

8、新建远程用户

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 新建数据库
CREATE DATABASE `test` CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_0900_ai_ci'

# 新建远程用户
CREATE USER 'devops'@'%' IDENTIFIED BY 'MyNewPass3!';

# 赋予指定账户指定(数据库名称.表名)远程访问权限
GRANT ALL PRIVILEGES ON test.* TO 'devops'@'%';

# 查看权限
SHOW GRANTS FOR 'devops'@'%';

# 收回权限
REVOKE ALL PRIVILEGES ON *.* FROM 'devops'@'%';

# 删除用户
DROP USER 'devops'@'%';

# 刷新权限
FLUSH PRIVILEGES;

9、 找回密码

如果忘记了MySQL root密码,可以用以下方法重新设置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 1.在`/etc/my.conf`中`[mysqld]`下添加`skip-grant-tables`
vim /etc/my.conf

[mysqld]
skip-grant-tables

# 2.重启mysql
systemctl restart mysqld

# 3.无密码进入mysql
mysql -u root

# 4.重置登录密码
USE mysql;
SELECT host, user, authentication_string, plugin FROM user;
update user set host = '%' where user ='root';
FLUSH PRIVILEGES;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'MyNewPass4!';
FLUSH PRIVILEGES;
# 退出
exit

# 5.在`/etc/my.conf`中`[mysqld]`下删除`skip-grant-tables`,添加#号注释
vim /etc/my.conf

[mysqld]
#skip-grant-tables

# 6.重启mysql
systemctl restart mysqld

# 7.使用新密码进入mysql
mysql -u root -p

10、启动失败

1
2
3
4
# 权限问题
chown mysql:mysql -R /var/run/mysqld

/usr/sbin/mysqld --user=mysql &

卸载mysql

1
yum remove mysql-server
1
2
3
4
5
rpm -qa|grep -i mysql

如下:
[root@ecs src]# rpm -qa|grep -i mysql
mysql80-community-release-el7-3.noarch
1
rpm -ev mysql80-community-release-el7-3.noarch
1
2
rm -rf /etc/my.cnf
rm -rf /etc/my.cnf.d
作者

风静花犹落

发布于

2020-05-22

更新于

2020-05-22

许可协议