一、清理环境
1、首先查询系统是否安装了MySQL
[root@localhost etc]# rpm -qa | grep mysql
mysql-community-libs-8.0.16-2.el7.x86_64
mysql-community-common-8.0.16-2.el7.x86_64
mysql-community-client-8.0.16-2.el7.x86_64
mysql80-community-release-el7-3.noarch
mysql-community-server-8.0.16-2.el7.x86_64
2、查询到已安装的MySQL库后,执行以下命令依次卸载
yum remove mysql-xxx-xxx
若没有查询到,表示没有安装Mysql库,可直接跳过此步。
二、删除MariaDB的文件
由于 在CentOS中默认安装有MariaDB,所以如果我们不删除MariaDB文件的话,安装mysql时就会发生冲突。
1、使用rpm 命令查找出要删除的mariadb文件:
rpm -pa | grep mariadb
2、可能出现的结果:
mariadb-libs-5.5.56-2.el7.x86_64
3、删除上面的程序
yum -y remove mariadb-libs.x86_64
至此,原有的mysql和mariadb数据库就删除了
三、下载并安装mysql
1、安装mysql官网提供的mysql repo源
官网下载地址:https://dev.mysql.com/downloads/repo/yum/
选择red hat版:
复制下载地址:
2、下载镜像
wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
3、安装镜像
rpm -ivh mysql80-community-release-el7-3.noarch.rpm
4、安装所有更新软件
yum update
更新过程中有[y/n]提示,按y就好;
5、安装MySQL
yum install mysql-server
大概有2G,请耐心等待安装完成;
默认配置文件路径:
配置文件:/etc/my.cnf
日志文件:var/log/mysqld.log
服务启动脚本:/usr/lib/systemd/system/mysqld.service
socket文件:/var/run/mysqld/mysqld.pid
四、初始化MySQL
mysqld --initialize
五、启动mysql服务
systemctl start mysqld
报错 提示:
Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.
解决方法:改变拥有者和群组:
chown mysql:mysql -R /var/lib/mysql
再次启动就可以了:
systemctl start mysqld
六、设置mysql开机自动启动
systemctl enable mysqld.service
七、查看运行状态
[root@fxt src]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2019-08-09 13:33:36 CST; 5min ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Main PID: 7126 (mysqld)
Status: "Server is operational"
CGroup: /system.slice/mysqld.service
└─7126 /usr/sbin/mysqld
Aug 09 13:33:35 fxt systemd[1]: Starting MySQL Server...
Aug 09 13:33:36 fxt systemd[1]: Started MySQL Server.
八、查看当前mysql安装版本
[root@fxt src]# mysqladmin --version
mysqladmin Ver 8.0.17 for Linux on x86_64 (MySQL Community Server - GPL)
九、修改初始密码
1、编辑MyQL配置文件添加以下内容
vim /etc/my.cnf
default-authentication-plugin=mysql_native_password #配置文件本来就有去掉注释即可
symbolic-links=0
skip-grant-tables
保存后退出
2、重启MySQL服务
systemctl restart mysqld
3、无密码登录MySQL
[root@fxt src]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.17 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.
mysql>
Enter password:直接敲回车
4、选择数据库
use mysql;
5、将原密码设置为空
update user set authentication_string='' where user='root';
成功示例:
mysql> update user set authentication_string='' where user='root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
6、退出mysql, 删除/etc/my.cnf文件最后的 skip-grant-tables 重启mysql服务;
7、 使用root用户进行登录,因为上面设置了authentication_string为空,所以可以免密码登录;
[root@fxt src]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.17
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.
mysql>
8、将root密码设置Haha@123
mysql> alter user 'root'@'localhost' identified by 'Haha@123';
Query OK, 0 rows affected (0.01 sec)
9、选择数据库
use mysql;
10、将host更改% 方便远程接入
mysql> update user set host='%' where user='root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
11、使用已经设置的密码来更改加密方式
ALTER USER ‘root’@’%’ IDENTIFIED BY ‘Haha@123’ PASSWORD EXPIRE NEVER;
如果报错,则跳过此步骤
12、使用新的加密方式重新设置密码
mysql> alter user 'root'@'%' identified with mysql_native_password by 'Haha@123';
Query OK, 0 rows affected (0.01 sec)
13、刷新
flush privileges;
14、重启MySQL服务
systemctl restart mysqld