环境:CentOS7 + MySQL5.6.37
1. 卸载自带的mariadb
#查询是否有mariadb
[root@zoljzaquyepqrnq6-1014045 mysql]# rpm -qa |grep mariadb
mariadb-libs-5.5.65-1.el7.x86_64
#卸载,rpm -e后面跟上面查询出来的名称
[root@zoljzaquyepqrnq6-1014045 mysql]# rpm -e --nodeps mariadb-libs-5.5.65-1.el7.x86_64
2. 安装依赖包
[root@zoljzaquyepqrnq6-1014045 ~]# yum install -y libaio net-tools autoconf
3. 下载rpm安装包
#在/usr/local/src目录下创建mysql目录,并在此目录里下载MySQL5.6.37的rpm包
[root@zoljzaquyepqrnq6-1014045 ~]# cd /usr/local/src/
[root@zoljzaquyepqrnq6-1014045 ~]# mkdir mysql
[root@zoljzaquyepqrnq6-1014045 ~]# cd mysql
[root@zoljzaquyepqrnq6-1014045 src]# wget https://cdn.mysql.com/archives/mysql-5.6/MySQL-5.6.37-1.el7.x86_64.rpm-bundle.tar
#如果报没有wget命令,就按如下先安装wget命令再下载
[root@zoljzaquyepqrnq6-1014045 src]# yum install -y wget
#解压下载的rpm包
[root@zoljzaquyepqrnq6-1014045 src]# tar -xvf MySQL-5.6.37-1.el7.x86_64.rpm-bundle.tar
4. 安装MySQL
#安装MySQL Client
[root@zoljzaquyepqrnq6-1014045 src]# rpm -ivh MySQL-client-5.6.37-1.el7.x86_64.rpm
#安装MySQL Server
[root@zoljzaquyepqrnq6-1014045 src]# rpm -ivh MySQL-server-5.6.37-1.el7.x86_64.rpm
#启动MySQL
[root@zoljzaquyepqrnq6-1014045 mysql]# service mysql start
Starting MySQL.Logging to '/var/lib/mysql/zoljzaquyepqrnq6-1014045.err'.
SUCCESS!
#查询是否启动成功,如下显示SUCCESS表示启动成功
[root@zoljzaquyepqrnq6-1014045 mysql]# service mysql status
SUCCESS! MySQL running (5694)
5. 修改密码
安装成功后,会生成一个随机的默认密码保存在/root/.mysql_secret 文件中。
#查看默认密码
[root@zoljzaquyepqrnq6-1014045 hotent]# cat /root/.mysql_secret
# The random password set for the root user at Tue Apr 5 20:07:22 2022 (local time): WaGfLdW28oUq_SSc
使用默认密码连接数据库,把如下语句中的密码替换成/root/.mysql_secret 文件中的默认密码。注意把如下语句中-p后面的密码替换成默认密码。
[root@zoljzaquyepqrnq6-1014045 mysql]# mysql -uroot -pWaGfLdW28oUq_SSc
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.37
Copyright (c) 2000, 2017, 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>
通过如下语句设置密码,如下表示把root用户的密码设置成123456,如果要改成其它的密码,替换引号中的内容即可。
#如下表示把root用户的密码设置成123456,如果要改成其它的密码,替换引号中的内容即可
mysql> set password=password('123456');
#验证修改后的密码是否生效
mysql> exit
mysql> mysql -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.37
Copyright (c) 2000, 2017, 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>
6. 设置root用户的远程连接权限
#选择mysql数据库
mysql> use mysql;
#把如下引号中的123456替换成前面设置的root用户的密码
mysql> grant all privileges on *.* to 'root'@'%'identified by '123456';
#刷新
mysql> flush privileges;
到这一步,就完成了MySQL5.6.37的安装与配置。
注:通过这种方式安装的MySQL的配置文件是/usr/my.cnf。