在一台Linux服务器上安装多个MySQL实例(二)--使用单独的MySQL配置文件

上一篇文章介绍了两种MySQL部署多实例的方法,分别是:

  • 使用官方自带的mysqld_multi来配置管理,特点是使用同一份MySQL配置文件,这种方式属于集中式管理,管理起来较为方便;
  • 使用单独的MySQL配置文件来单独配置实例,这种方式逻辑简单,数据库之间没有关联。

两种方法最大的区别在于:使用mysqld_multi方式,所有的实例共使用一个参数文件,使用第二种方法,则各自实例使用自己的参数文件。

第一种方方法已经在第一篇文档里面演示过了,这里学习一下第二种方法。


(一)实验环境

操作系统   :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的包

[root@masterdb ~]# rpm -qa|grep mysql
[root@masterdb ~]# rpm -qa |grep mariadb
mariadb-libs-5.5.56-2.el7.x86_64

[root@masterdb ~]# rpm -e mariadb-libs-5.5.56-2.el7.x86_64 --nodeps


(2.2)依赖包安装

MySQL对libaio 库有依赖性。如果未在本地安装该库,则数据目录初始化和随后的服务器启动步骤将失败

# install library
[root@mysql mysql]# yum install libaio

对于MySQL 5.7.19和更高版本:通用Linux版本中增加了对非统一内存访问(NUMA)的支持,该版本现在对libnuma库具有依赖性 。

# install library 
[root@mysql mysql]# yum install libnuma

(2.3)创建用户和用户组

[root@masterdb ~]# groupadd mysql
[root@masterdb ~]# useradd -r -g mysql -s /bin/false mysql


(2.4)解压安装包

[root@masterdb ~]# cd /usr/local/
[root@masterdb local]# tar xzvf /root/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz

# 修改解压文件名,与前面定义的basedir相同
[root@masterdb local]# mv mysql-5.7.24-linux-glibc2.12-x86_64/ mysql

最终解压结果如下:

[root@masterdb 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)创建数据文件存放路径

[root@masterdb mysql]# mkdir -p /mysql/{3306,3307,3308,3309}/data
[root@masterdb mysql]# chown -R mysql:mysql /mysql 
[root@masterdb mysql]# cd /mysql

[root@masterdb mysql]# tree
.
├── 3306
│   └── data
├── 3307
│   └── data
└── 3308
    └── data


(2.6)创建MySQL参数配置文件

分别为3306、3307、3308各自创建一个my.cnf配置文件。配置文件位置为:

[root@masterdb 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数据库
[root@masterdb 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 root@localhost: XE4kl>x4fo?v

# 初始化3307数据库
[root@masterdb 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 root@localhost: 3Oir)9uX2?su

# 初始化3308数据库
[root@masterdb 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 root@localhost: NhwFZv6i!UfZ


(2.8)设置环境变量

[root@masterdb mysql]# vim /etc/profile
# 在文件末尾添加下面信息
export PATH=/usr/local/mysql/bin:$PATH

#使环境变量生效
[root@masterdb mysql]# source /etc/profile


(2.9)启动数据库

# 经过测试,mysql在初始化的时候新生成的部分文件权限为root,所以最好在启动之前重新将datadir路径授权给mysql
[root@masterdb mysql]# chown -R mysql:mysql /mysql

# 启动MySQL数据库实例
[root@masterdb ~]# nohup /usr/local/mysql/bin/mysqld --defaults-file=/mysql/3306/my.cnf --user=mysql &
[root@masterdb ~]# nohup /usr/local/mysql/bin/mysqld --defaults-file=/mysql/3307/my.cnf --user=mysql &
[root@masterdb ~]# nohup /usr/local/mysql/bin/mysqld --defaults-file=/mysql/3308/my.cnf --user=mysql &


(2.10)确认MySQL数据库状况

# 确认MySQL进程
[root@masterdb 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

# 确认端口使用情况
[root@masterdb 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


【完】


相关文档集合:

1.在一台Linux服务器上安装多个MySQL实例(一)--使用mysqld_multi方式

2.在一台Linux服务器上安装多个MySQL实例(二)--使用单独的MySQL配置文件

3.在一台Linux服务器上安装多个MySQL实例(三)--遇到的几个坑

上一篇:南阳办南阳证HJ


下一篇:[链表]酒店查询