(一)实验环境
操作系统 :CentOS Linux release 7.4.1708 (Core)
数据库版本:5.7.24-log
预计划安装3个MySQL实例,规划信息为:
实例1 | 实例2 | 实例3 |
basedir=/usr/local/mysql datadir=/mysql/3306/data port=3306 socket=/tmp/mysql_3306.sock 参数文件:/mysql/3306/my.cnf |
basedir=/usr/local/mysql datadir=/mysql/3307/data port=3307 socket=/tmp/mysql_3307.sock 参数文件:/mysql/3307/my.cnf |
basedir=/usr/local/mysql datadir=/mysql/3308/data port=3308 socket=/tmp/mysql_3308.sock 参数文件:/mysql/3308/my.cnf |
(二)实验过程
(2.1)在安装MySQL之前,需要卸载服务器自带的MySQL包和MySQL数据库分支mariadb的包
[[email protected] ~]# rpm -qa|grep mysql [[email protected] ~]# rpm -qa |grep mariadb mariadb-libs-5.5.56-2.el7.x86_64 [[email protected] ~]# rpm -e mariadb-libs-5.5.56-2.el7.x86_64 --nodeps
(2.2)依赖包安装
MySQL对libaio 库有依赖性。如果未在本地安装该库,则数据目录初始化和随后的服务器启动步骤将失败
# install library [[email protected] mysql]# yum install libaio
对于MySQL 5.7.19和更高版本:通用Linux版本中增加了对非统一内存访问(NUMA)的支持,该版本现在对libnuma库具有依赖性 。
# install library [[email protected] mysql]# yum install libnuma
(2.3)创建用户和用户组
[[email protected] ~]# groupadd mysql [[email protected] ~]# useradd -r -g mysql -s /bin/false mysql
(2.4)解压安装包
[[email protected] ~]# cd /usr/local/ [[email protected] local]# tar xzvf /root/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz # 修改解压文件名,与前面定义的basedir相同 [[email protected] local]# mv mysql-5.7.24-linux-glibc2.12-x86_64/ mysql
最终解压结果如下:
[[email protected] mysql]# ls -l total 36 drwxr-xr-x 2 root root 4096 Mar 28 13:48 bin -rw-r--r-- 1 7161 31415 17987 Oct 4 2018 COPYING drwxr-xr-x 2 root root 55 Mar 28 13:48 docs drwxr-xr-x 3 root root 4096 Mar 28 13:48 include drwxr-xr-x 5 root root 230 Mar 28 13:48 lib drwxr-xr-x 4 root root 30 Mar 28 13:48 man -rw-r--r-- 1 7161 31415 2478 Oct 4 2018 README drwxr-xr-x 28 root root 4096 Mar 28 13:48 share drwxr-xr-x 2 root root 90 Mar 28 13:48 support-files
(2.5)创建数据文件存放路径
[[email protected] mysql]# mkdir -p /mysql/{3306,3307,3308,3309}/data [[email protected] mysql]# chown -R mysql:mysql /mysql [[email protected] mysql]# cd /mysql [[email protected] mysql]# tree . ├── 3306 │ └── data ├── 3307 │ └── data └── 3308 └── data
(2.6)创建MySQL参数配置文件
分别为3306、3307、3308各自创建一个my.cnf配置文件。配置文件位置为:
[[email protected] mysql]# tree /mysql /mysql ├── 3306 │ ├── data │ └── my.cnf ├── 3307 │ ├── data │ └── my.cnf └── 3308 ├── data └── my.cnf
其中,各个实例的配置文件信息如下:
实例1(3306) | 实例2(3307) | 实例3(3308) |
[mysqld] port = 3306 basedir=/usr/local/mysql/ datadir=/mysql/3306/data lower_case_table_names=1 innodb_buffer_pool_size=128M socket=/tmp/mysql_3306.sock |
[mysqld] port = 3307 basedir=/usr/local/mysql/ datadir=/mysql/3307/data lower_case_table_names=1 innodb_buffer_pool_size=128M socket=/tmp/mysql_3307.sock |
[mysqld] port = 3308 basedir=/usr/local/mysql/ datadir=/mysql/3308/data lower_case_table_names=1 innodb_buffer_pool_size=128M socket=/tmp/mysql_3308.sock |
(2.7)初始化数据库
需要注意,初始化结束的最后一行记录了root的密码
# 初始化3306数据库 [[email protected] mysql]# /usr/local/mysql/bin/mysqld --defaults-file=/mysql/3306/my.cnf --initialize --basedir=/usr/local/mysql/ --datadir=/mysql/3306/data 2020-03-28T08:19:11.202256Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2020-03-28T08:19:11.352108Z 0 [Warning] InnoDB: New log files created, LSN=45790 2020-03-28T08:19:11.383671Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2020-03-28T08:19:11.441325Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: cdab4062-70cc-11ea-aedf-000c29ea7752. 2020-03-28T08:19:11.442190Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2020-03-28T08:19:11.442995Z 1 [Note] A temporary password is generated for [email protected]:
# 初始化3307数据库 [[email protected] mysql]# /usr/local/mysql/bin/mysqld --defaults-file=/mysql/3307/my.cnf --initialize --basedir=/usr/local/mysql/ --datadir=/mysql/3307/data 2020-03-28T08:19:21.704008Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2020-03-28T08:19:21.846567Z 0 [Warning] InnoDB: New log files created, LSN=45790 2020-03-28T08:19:21.877435Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2020-03-28T08:19:21.934698Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: d3ec69cd-70cc-11ea-b101-000c29ea7752. 2020-03-28T08:19:21.935421Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2020-03-28T08:19:21.937056Z 1 [Note] A temporary password is generated for [email protected]:
# 初始化3308数据库 [[email protected] mysql]# /usr/local/mysql/bin/mysqld --defaults-file=/mysql/3308/my.cnf --initialize --basedir=/usr/local/mysql/ --datadir=/mysql/3308/data 2020-03-28T08:20:04.342747Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2020-03-28T08:20:04.488694Z 0 [Warning] InnoDB: New log files created, LSN=45790 2020-03-28T08:20:04.519974Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2020-03-28T08:20:04.579649Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: ed57843b-70cc-11ea-b206-000c29ea7752. 2020-03-28T08:20:04.580544Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2020-03-28T08:20:04.581480Z 1 [Note] A temporary password is generated for [email protected]:
(2.8)设置环境变量
[[email protected] mysql]# vim /etc/profile # 在文件末尾添加下面信息 export PATH=/usr/local/mysql/bin:$PATH #使环境变量生效 [[email protected] mysql]# source /etc/profile
(2.9)启动数据库
# 经过测试,mysql在初始化的时候新生成的部分文件权限为root,所以最好在启动之前重新将datadir路径授权给mysql [[email protected] mysql]# chown -R mysql:mysql /mysql # 启动MySQL数据库实例 [[email protected] ~]# nohup /usr/local/mysql/bin/mysqld --defaults-file=/mysql/3306/my.cnf --user=mysql & [[email protected] ~]# nohup /usr/local/mysql/bin/mysqld --defaults-file=/mysql/3307/my.cnf --user=mysql & [[email protected] ~]# nohup /usr/local/mysql/bin/mysqld --defaults-file=/mysql/3308/my.cnf --user=mysql &
(2.10)确认MySQL数据库状况
# 确认MySQL进程 [[email protected] 3307]# ps -ef|grep mysql mysql 11092 1371 0 16:36 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --defaults-file=/mysql/3307/my.cnf --user=mysql mysql 11146 1371 0 16:36 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --defaults-file=/mysql/3308/my.cnf --user=mysql mysql 11182 1371 7 16:37 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --defaults-file=/mysql/3306/my.cnf --user=mysql root 11211 1371 0 16:37 pts/0 00:00:00 grep --color=auto mysql # 确认端口使用情况 [[email protected] 3307]# netstat -ntl Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN tcp6 0 0 :::3307 :::* LISTEN tcp6 0 0 :::3308 :::* LISTEN tcp6 0 0 :::22 :::* LISTEN tcp6 0 0 ::1:25 :::* LISTEN tcp6 0 0 :::3306 :::* LISTEN