mysql下载地址 https://dev.mysql.com/downloads/mysql/5.7.html#downloads
1.检查当前系统是否安装过Mysql
[linux@node1 ~]$ rpm -qa|grep mariadb
mariadb-libs-5.5.64-1.el7.x86_64 //如果存在通过如下命令卸载
[linux@node1 ~]$ sudo rpm -e --nodeps mariadb-libs //用此命令卸载mariadb
2.将MySQL安装包拷贝到/opt/software/mysql目录下
3.在安装目录下执行rpm安装
[linux@node1 mysql]$ sudo rpm -ivh 01_mysql-community-common-5.7.29-1.el7.x86_64.rpm
[linux@node1 mysql]$ sudo rpm -ivh 02_mysql-community-libs-5.7.29-1.el7.x86_64.rpm
[linux@node1 mysql]$ sudo rpm -ivh 03_mysql-community-libs-compat-5.7.29-1.el7.x86_64.rpm
[linux@node1 mysql]$ sudo rpm -ivh 04_mysql-community-client-5.7.29-1.el7.x86_64.rpm
[linux@node1 mysql]$ sudo rpm -ivh 05_mysql-community-server-5.7.29-1.el7.x86_64.rpm
注意:按照顺序依次执行
如果Linux是最小化安装的,在安装mysql-community-server-5.7.28-1.el7.x86_64.rpm时可能会出 现如下错误
[linux@node1 mysql]$ sudo rpm -ivh 05_mysql-community-server-5.7.29-1.el7.x86_64.rpm
警告:mysql-community-server-5.7.28-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
错误:依赖检测失败:
libaio.so.1()(64bit) 被 mysql-community-server-5.7.28-1.el7.x86_64 需要
libaio.so.1(LIBAIO_0.1)(64bit) 被 mysql-community-server-5.7.28-1.el7.x86_64 需要
libaio.so.1(LIBAIO_0.4)(64bit) 被 mysql-community-server-5.7.28-1.el7.x86_64 需要
通过yum安装缺少的依赖,然后重新安装mysql-community-server-5.7.28-1.el7.x86_64 即可
[linux@node1 mysql]$ sudo yum install -y libaio
4.删除/etc/my.cnf文件中datadir指向的目录下的所有内容,如果有内容的情况下(可选):
[linux@node1 mysql]$ sudo vim /etc/my.cnf
查看datadir的值:
[mysqld]
datadir=/var/lib/mysql
删除/var/lib/mysql目录下的所有内容(一般刚装的mysql是没有数据的):
[linux@node1 mysql]$ cd /var/lib/mysql
[linux@node1 mysql]$ sudo rm -rf ./* //注意执行命令的位置
5.初始化数据库
[linux@node1 mysql]$ sudo mysqld --initialize --user=mysql
6.查看临时生成的root用户的密码
[linux@node1 mysql]$ sudo cat /var/log/mysqld.log
7.启动MySQL服务
[linux@node1 mysql]$ sudo systemctl start mysqld
8.登录MySQL数据库
[linux@node1 mysql]$ mysql -uroot -p
Enter password: 输入临时生成的密码
登录成功
9.必须先修改root用户的密码,否则执行其他的操作会报错
mysql> set password = password("root");
10.修改mysql库下的user表中的root用户允许任意ip连接
mysql> update mysql.user set host='%' where user='root';
mysql> flush privileges;