环境说明:centos7.5,mysql5.7
#卸载系统自带的Mariadb
# rpm -qa | grep mariadb
# rpm -e --nodeps mariadb文件名
#删除etc目录下的my.cnf文件
# rm -rf /etc/my.cnf
#安装mysql依赖库文件libaio
# yum -y install libaio*
#下载压缩包到/usr/local,解压在当前目录并重命名为mysql5.7
# cd /usr/local
# yum install wget
# wget https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
# tar -zxvf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
# mv mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz mysql5.7
#添加组和用户
# groupadd mysql
# useradd -g mysql mysql
#创建配置文件
# vi /etc/my.cnf
[mysqld]
user=mysql
port=3306
basedir=/usr/local/mysql5.7/
datadir=/usr/local/mysql5.7/data/
character-set-server=utf8
collation-server=utf8_bin
#创建数据目录并初始化
# mkdir -p /usr/local/mysql5.7/data/
# chown -R mysql:mysql /usr/local/mysql5.7/data/
# /usr/local/mysql5.7/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql5.7 --datadir=/usr/local/mysql5.7/data/&
A temporary password is generated for root@localhost:这里就是数据库的初始密码
#开启mysql服务,用初始密码登录数据,并修改初始密码
# /usr/local/mysql5.7/support-files/mysql.server start
# /usr/local/mysql5.7/bin/mysql -u root -p
> set password=password('新密码');
#设置环境变量
# vi /etc/profile
## FOR MYSQL ##
export PATH=/usr/local/mysql5.7/bin:$PATH
# source /etc/profile
#设置开机自启动
# cp /usr/local/mysql5.7/support-files/mysql.server /etc/init.d/mysql
# chmod +x /etc/init.d/mysql
# chkconfig --add mysql
# chkconfig --level 35 mysql on
#数据库设置允许远程主机访问
> use mysql;
> update user set host = '%' where user = 'root';
#重启数据库服务
# service mysql stop
# service mysql start
#防火墙放开mysql数据库3306端口
# firewall-cmd --zone=public --add-port=3306/tcp --permanent
# firewall-cmd --reload
好了,大功告成!