1. 下载MySQL安装包

  • MySQL 8.0下载网址:https://dev.mysql.com/downloads/mysql/
  • Select Operating System: 选择 Red Hat ,CentOS 是基于红帽的,Select OS Version: 选择 Linux 7
  • 选择RPM Bundle 点击Download
  • 点击No thanks,just start my download.进行下载
  • 下载完毕后传输至Centos7中

2. 清理Mariadb安装包

1
2
3
4
5
6
# 通过命令查看mariadb的安装包
[root@master ~]# rpm -qa | grep mariadb
# 通过命令清除mariabd的所有安装包
[root@master ~]# rpm -e mariadb-libs-5.5.56-2.el7.x86_64 --nodeps
# 通过命令再次查看mariadb的安装包是否已清理
[root@master ~]# rpm -qa | grep mariadb

3. 解压安装MySQL包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 解压MySQL包
[root@master local]# tar -xvf mysql-8.0.16-2.el7.x86_64.rpm-bundle.tar
# 安装common
[root@master local]# rpm -ivh mysql-community-common-8.0.16-2.el7.x86_64.rpm --nodeps --force
# 安装libs
[root@master local]# rpm -ivh mysql-community-libs-8.0.16-2.el7.x86_64.rpm --nodeps --force
# 安装client
[root@master local]# rpm -ivh mysql-community-client-8.0.16-2.el7.x86_64.rpm --nodeps --force
# 安装server
[root@master local]# rpm -ivh mysql-community-server-8.0.16-2.el7.x86_64.rpm --nodeps --force
# 通过命令查看mysql的安装包
[root@master local]# rpm -qa | grep mysql
mysql-community-common-8.0.16-2.el7.x86_64
mysql-community-libs-8.0.16-2.el7.x86_64
mysql-community-client-8.0.16-2.el7.x86_64
mysql-community-server-8.0.16-2.el7.x86_64

4. 初始化MySQL

1
2
3
4
5
6
7
8
9
10
11
# 初始化MySQL数据库及相关配置
[root@master local]# mysqld --initialize
# 更改所属用户组 如添加失败,请先添加相应的 '组''用户'
[root@master local]# chown mysql:mysql /var/lib/mysql -R
# 开启mysqld服务
[root@master local]# systemctl start mysqld.service
# 设置开启启动
[root@master local]# systemctl enable mysqld
# 查询MySQL临时生成密码
[root@master local]# cat /var/log/mysqld.log | grep password
2019-06-28T10:02:12.733076Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: rldW%6_5J3h<

5. 接入MySQL

1
2
3
4
5
6
7
8
9
# 进入MySQL登录
[root@master local]# mysql -u root -p
# 使用上述的临时密码进行登录
Enter password: rldW%6_5J3h<
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.16 MySQL Community Server - GPL
# 通过命令修改登录密码
mysql> alter user 'root'@'localhost' identified with mysql_native_password by '所要修改的密码需包含大小写字母、数字及特殊符号';

默认密码检查策略要求必须包含:大小写字母、数字和特殊符号,并且长度不能少于8位,否则会提示ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

6. 远程访问授权

1
2
3
4
5
6
7
8
9
# 创建远程访问用户
mysql> create user 'root'@'%' identified with mysql_native_password by '远程访问密码';
Query OK, 0 rows affected (0.01 sec)
# 允许访问权限
mysql> grant all privileges on *.* to 'root'@'%' with grant option;
Query OK, 0 rows affected (0.01 sec)
# 刷新权限列表
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

至此,可通过远程访问账号登入MySQL使用可视化工具。


默认配置文件路径:

配置文件:/etc/my.cnf
日志文件:/var/log//var/log/mysqld.log
服务启动脚本:/usr/lib/systemd/system/mysqld.service
socket文件:/var/run/mysqld/mysqld.pid

如果忘记root密码,则按以下操作恢复:

在[mysqld]的段中加上一句:skip-grant-tables 保存并且退出vi。

1
2
3
$ mysql  -u root
$ update mysql.user set authentication_string=password('123qwe') where user='root' and Host = 'localhost';
$ flush privileges