1. 下载MySQL5.6
1.1 官方下载网站
https://downloads.mysql.com/archives/community/
1.2 选择版本号为5.6的最新一个发布版本,如当前为5.6.51;然后选择相应的操作系统与操作系统版本,因本文安装的操作系统为CentOS7 64位,所以选择Red Hat Enterprise Linux 7 / Oracle Linux 7 (x86, 64-bit)
;最后选择包含完整的RPM程序压缩包进行下载
2. 安装MySQL5.6
2.1 卸载CentOS中自带的mariadb
[root@localhost ~]# rpm -qa | grep mariadb | xargs rpm -e --nodeps
2.2 上传刚刚下载的MySQL-5.6.51-1.el7.x86_64.rpm-bundle.tar
到CentOS系统
2.3 解压MySQL-5.6.51-1.el7.x86_64.rpm-bundle.tar
文件
[root@localhost ~]# ll
总用量 388172
-rw-------. 1 root root 1331 11月 22 14:42 anaconda-ks.cfg
-rw-r--r--. 1 root root 143142634 11月 23 22:06 jdk-8u271-linux-x64.tar.gz
-rw-r--r--. 1 root root 254341120 2月 5 15:24 MySQL-5.6.51-1.el7.x86_64.rpm-bundle.tar
# 创建mysql目录用于存储解压后RPM程序包
[root@localhost ~]# mkdir mysql
# 解压文件到mysql目录
[root@localhost ~]# tar -xvf ./MySQL-5.6.51-1.el7.x86_64.rpm-bundle.tar -C ./mysql/
MySQL-client-5.6.51-1.el7.x86_64.rpm
MySQL-devel-5.6.51-1.el7.x86_64.rpm
MySQL-embedded-5.6.51-1.el7.x86_64.rpm
MySQL-server-5.6.51-1.el7.x86_64.rpm
MySQL-shared-5.6.51-1.el7.x86_64.rpm
MySQL-shared-compat-5.6.51-1.el7.x86_64.rpm
MySQL-test-5.6.51-1.el7.x86_64.rpm
# 查看解压出的RPM程序包
[root@localhost ~]# ll ./mysql/
总用量 248388
-rw-r--r--. 1 7155 31415 21606520 1月 5 23:37 MySQL-client-5.6.51-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 3536604 1月 5 23:37 MySQL-devel-5.6.51-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 93754464 1月 5 23:37 MySQL-embedded-5.6.51-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 70091008 1月 5 23:37 MySQL-server-5.6.51-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 2338300 1月 5 23:38 MySQL-shared-5.6.51-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 2299660 1月 5 23:38 MySQL-shared-compat-5.6.51-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 60704356 1月 5 23:38 MySQL-test-5.6.51-1.el7.x86_64.rpm
2.4 安装依赖
[root@localhost ~]# yum install -y perl-Data-Dumper
2.5 安装MySQL-server服务和MySQL-client客户端服务,其他组件可根据情况自行安装,这里就不做演示了
# 创建mysql组
[root@localhost ~]# groupadd mysql
# 创建mysql用户
[root@localhost ~]# useradd -g mysql mysql
# 安装MySQL-server服务
[root@localhost ~]# rpm -ivh ./mysql/MySQL-server-5.6.51-1.el7.x86_64.rpm
# 安装MySQL-client客户端服务
[root@localhost ~]# rpm -ivh ./mysql/MySQL-client-5.6.51-1.el7.x86_64.rpm
2.6 配置my.cnf;MySQL加载my.cnf配置文件路径顺序:
- /etc/my.cnf
- /etc/mysql/my.cnf
- /usr/local/etc/my.cnf
- ~/.my.cnf
# 移动/usr/my.cnf文件到/etc/目录下
[root@localhost ~]# mv /usr/my.cnf /etc/
# 配置my.cnf文件
[root@localhost ~]# vim /etc/my.cnf
my.cnf文件配置参考
# 客户端配置
[client]
port = 3306
default-character-set = utf8mb4
# 服务端配置
[mysqld]
user = mysql
port = 3306
character-set-server = utf8mb4
back_log = 100
max_connections = 1000
max_connect_errors = 100
open_files_limit = 10000
thread_cache_size = 8
sql_mode = ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
# 禁止客户端域名解析
skip-name-resolve
# 禁止slave复制进程随mysql启动而自动启动
skip-slave-start
[mysqldump]
quick
[mysql]
auto-rehash
default-character-set = utf8mb4
2.7 启动MySQL服务
[root@localhost ~]# systemctl start mysql
2.8 查询root用户初始密码
# 初始密码查询
[root@localhost ~]# cat /root/.mysql_secret
# The random password set for the root user at Fri Feb 5 16:01:13 2021 (local time): zg3wxtWOoXFFcQt6
2.9 使用MySQL客户端首次登录控制台,并修改root用户初始密码和取消root用户的localhost访问限制
# 登录MySQL控制台
[root@localhost ~]# mysql -u root -p
# 使用初始密码登录
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.51
Copyright (c) 2000, 2021, 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.
# 修改root用户初始密码
mysql> SET PASSWORD = PASSWORD('123456');
Query OK, 0 rows affected (0.00 sec)
# 取消root用户localhost访问限制
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> update user set Host='%' where User='root';
# 该错误可忽略, 实际上已操作成功
ERROR 1062 (23000): Duplicate entry '%-root' for key 'PRIMARY'
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
3. 相关命令
使用RPM安装的会自动将MySQL注册为系统服务,使用CentOS系统的systemctl命令操作该服务即可
启动:systemctl start mysql
停止:systemctl stop mysql
开启开机启动:systemctl enable mysql
禁止开机启动:systemctl disable mysql
4. 注意事项
4.1 注意MySQL服务的防火墙开通,可使用如下三种方式:
# 方法一:开通MySQL端口的访问权限
[root@localhost ~]# firewall-cmd --permanent --add-port=3306/tcp
success
[root@localhost ~]# firewall-cmd --reload
success
# 方法二:开通MySQL服务的访问权限
[root@localhost ~]# firewall-cmd --permanent --add-service=mysql
success
[root@localhost ~]# firewall-cmd --reload
success
# 方法三:关闭防火墙,并禁止防火墙开机启动(不推荐)
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
4.2 以RPM方式安装MySQL的默认路径以及数据存储路径为/var/lib/mysql