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)
2. 安装MySQL5.6
2.1 冲突数据库
卸载CentOS中自带的mariadb
[root@192 ~]# rpm -qa | grep mariadb | xargs rpm -e --nodeps
1
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文件
# 创建mysq56l目录用于存储解压后RPM程序包
[root@192 ~]# mkdir mysql56
# 解压文件到mysql56目录
[root@192 ~]# tar -xvf ./MySQL-5.6.51-1.el7.x86_64.rpm-bundle.tar -C ./mysql56/
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
2.4 安装依赖
[root@192 ~]# yum install -y perl-Data-Dumper
1
2.5 安装mysql
安装MySQL-server服务和MySQL-client客户端服务
# 创建mysql组
[root@192 ~]# groupadd mysql
# 创建mysql用户
[root@192 ~]# useradd -g mysql mysql
# 安装MySQL-server服务,./mysql56表示安装包在根目录的mysql56文件夹内
[root@192 ~]# rpm -ivh ./mysql56/MySQL-server-5.6.51-1.el7.x86_64.rpm
# 安装MySQL-client客户端服务
[root@192 ~]# rpm -ivh ./mysql56/MySQL-client-5.6.51-1.el7.x86_64.rpm
1
2
3
4
5
6
7
8
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@192 ~]# mv /usr/my.cnf /etc/
# 配置my.cnf文件
[root@192 ~]# vim /etc/my.cnf
1
2
3
4
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
2.7 启动MySQL服务
[root@192 ~]# systemctl start mysql
1
2.8 配置密码
查询root用户初始密码
# 初始密码查询
[root@192 ~]# cat /root/.mysql_secret
# The random password set for the root user at Fri Feb 5 16:01:13 2021 (local time): zg3wxtWOoXFFcQt6
1
2
3
2.9 使用MySQL客户端首次登录控制台,并修改root用户初始密码和取消root用户的192访问限制
# 登录MySQL控制台
[root@192 ~]# 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用户192访问限制
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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
3. 注意事项
3.1 防火墙配置
注意MySQL服务的防火墙开通,可使用如下三种方式:
# 方法一:开通MySQL端口的访问权限
[root@192 ~]# firewall-cmd --permanent --add-port=3306/tcp
success
[root@192 ~]# firewall-cmd --reload
success
# 方法二:开通MySQL服务的访问权限
[root@192 ~]# firewall-cmd --permanent --add-service=mysql
success
[root@192 ~]# firewall-cmd --reload
success
# 方法三:关闭防火墙,并禁止防火墙开机启动(不推荐)
[root@192 ~]# systemctl stop firewalld
[root@192 ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
3.2 启动与停止
使用RPM安装的会自动将MySQL注册为系统服务,使用CentOS系统的systemctl命令操作该服务即可
启动:systemctl start mysql
停止:systemctl stop mysql
开启开机启动:systemctl enable mysql
禁止开机启动:systemctl disable mysql
3.3 默认安装位置
以RPM方式安装MySQL的默认路径以及数据存储路径为/var/lib/mysql