Linux下安装MySQL5.7及遇到的问题解决方法
一、下载地址
https://dev.mysql.com/downloads/mysql/5.7.html#downloads
本文安装的版本:
或者使用wget下载:
[root@localhost opt]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.32-el7-x86_64.tar.gz
[root@localhost opt]# tar -xvf mysql-5.7.32-el7-x86_64.tar.gz
二、检查是否已经安装过mysql
[root@localhost opt]# rpm -qa | grep mysql
# 若出现:mysql-libs*
[root@localhost /]# rpm -e --nodeps mysql-libs* #进行卸载
# 再次执行查询命令,查看是否删除
[root@localhost /]# rpm -qa | grep mysql
# 若没有mysql字样出现,说明mysql已卸载
# 查询所有Mysql对应的文件夹
[root@localhost /]# whereis mysql
mysql: /usr/bin/mysql /usr/include/mysql
[root@localhost lib]# find / -name mysql
/data/mysql
/data/mysql/mysql
# 删除相关目录或文件
[root@localhost /]# rm -rf /usr/bin/mysql /usr/include/mysql /data/mysql /data/mysql/mysql
三、安装
[root@localhost opt]# mv mysql-5.7.32-el7-x86_64 /usr/local/mysql
[root@localhost opt]# mkdir /usr/local/mysql/data
[root@localhost opt]# groupadd mysql
[root@localhost opt]# useradd -r -g mysql mysql
[root@localhost opt]# chown mysql:mysql -R /usr/local/mysql
[root@localhost mysql]# vim /etc/my.cnf.d/mysql-clients.cnf
[root@localhost opt]# cd /usr/local/mysql/bin/
[root@localhost bin]# ./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql --initialize
[root@localhost bin]# ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
[root@localhost bin]# service mysql start
[root@localhost bin]# mkdir /var/log/mariadb
[root@localhost bin]# touch /var/log/mariadb/mariadb.log
[root@localhost bin]# chown mysql:mysql -R /var/log/mariadb
[root@localhost bin]# service mysql start
四、RPM安装:
下载下图中的四个包:
[root@localhost ~]# cd /opt
[root@localhost opt]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-server-5.7.32-1.el7.x86_64.rpm
[root@localhost opt]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-common-5.7.32-1.el7.x86_64.rpm
[root@localhost opt]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-libs-5.7.32-1.el7.x86_64.rpm
[root@localhost opt]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-client-5.7.32-1.el7.x86_64.rpm
[root@localhost opt]# rpm -qa|grep mariadb
mariadb-libs-5.5.52-1.el7.x86_64
[root@localhost opt]# rpm -e mariadb-libs-5.5.52-1.el7.x86_64
[root@localhost opt]# rpm -e --nodeps mariadb-libs-5.5.52-1.el7.x86_64
[root@localhost opt]# rpm -ivh mysql-community-common-5.7.32-1.el7.x86_64.rpm
[root@localhost opt]# rpm -ivh mysql-community-libs-5.7.32-1.el7.x86_64.rpm
[root@localhost opt]# rpm -ivh mysql-community-client-5.7.32-1.el7.x86_64.rpm
[root@localhost opt]# rpm -ivh mysql-community-server-5.7.32-1.el7.x86_64.rpm
[root@localhost opt]# systemctl start mysqld
四、修改MySQL密码及开发远程连接
修改MySQL初始密码:
[root@localhost bin]# mysql -uroot -p
Enter password:
mysql> alter user 'root'@'localhost' identified by '新密码';
退出,使用新密码重新登陆
开放远程连接
mysql> grant all privileges on *.* to 'root'@'%' identified by '密码';