一、简介

1、概述

MySQL多实例就是在一台机器上开启多个不同的服务端口(如:3306,3307),运行多个MySQL服务进程,通过不同的socket监听不同的服务端口来提供各自的服务

2.1、优点

1)有效利用服务器资源

当单个服务器资源过剩时,可以充分利用剩余的资源来提供更多的服务;

2)节约服务器资源

当公司资金紧张,但数据库又需要数据库之间各自提供服务时,并且还想使用主从同步等技术,此时多实例就再好不过了;

3)方便后期架构扩展

当公司的某个项目才启动时,启动初期并不一定有很大的用户量,因此可以先用一组物理数据库服务器,在上面部署多个实例,方便后续架构扩展、迁移;

2.2、缺点

资源互相抢占问题

当某个服务实例并发很高或者有慢查询时,整个实例会消耗更多的内存、CPU和IO资源,这将导致服务器上的其它实例提供服务的质量下降。这就比如说合租房的各个租客,每当早晨上班时,都会洗漱,此时卫生间的占用率就大,各个租客总会发生等待。

3、部署mysql多实例的两种方式

1、基于多配置文件

通过使用多个配置文件来启动不同的进程,以此来实现多实例。

优点:逻辑简单,配置简单

缺点:管理起来不方便

2、基于mysqld_multi

通过官方自带的 mysqld_multi 工具,使用单独配置文件来实现多实例

优点: 便于集中管理管理

缺点: 不方便针对每个实例配置进行定制

4、同一开发环境下安装两个数据库,必须处理以下问题

(1) 配置文件安装路径不能相同

(2)数据库目录不能相同

(3)启动脚本不能同名

(4)端口不能相同

(5)socket文件的生成路径不能相同

二、服务搭建

1、部署环境    

CentOS Linux release 7.5.1804 (Core)

2、下载免编译的二进制包

mysql-5.6.41-linux-glibc2.12-x86_64.tar.gz

3、解压和迁移(/usr/local

[root@SQL local]# tar -zxvf mysql-5.6.41-linux-glibc2.12-x86_64.tar.gz

[root@SQL local]# mv mysql-5.6.41-linux-glibc2.12-x86_64 mysql

 4、暂时关闭iptables和seLinux

5、创建mysql用户

# groupadd mysql

# useradd -r -g mysql -s /bin/false mysql

6、创建相关目录

[root@SQL ~]# mkdir -p /data/mysql/{mysql3307,mysql3308,mysql3309}

[root@SQL ~]# mkdir -p /data/mysql/mysql3307/{data,log,tmp}

[root@SQL ~]# mkdir -p /data/mysql/mysql3308/{data,log,tmp}

[root@SQL ~]# mkdir -p /data/mysql/mysql3309/{data,log,tmp}

7、更改目录权限

[root@SQL ~]# chown -R mysql:mysql /data/mysql/

[root@SQL ~]# chown -R mysql:mysql /usr/local/mysql/

8、添加环境变量

# echo 'export PATH=$PATH:/usr/local/mysql/bin' >>  /etc/profile

# source /etc/profile

9、修改my.cnf

# cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf

[root@SQL ~]# vim /etc/my.cnf

  [client]

port = 3307

socket = /tmp/mysql.sock

[mysqld_multi]

mysqld = /usr/local/mysql/bin/mysqld_safe

mysqladmin= /usr/local/mysql/bin/mysqladmin

log = /data/mysql/mysqld_multi.log

[mysqld]

user = mysql

basedir = /usr/local/mysql

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

[mysqld3307]

port = 3307

mysqld = mysqld

mysqladmin = mysqladmin

datadir = /data/mysql/mysql3307/data

language = /usr/local/mysql/share/english/

server-id = 3307

socket = /tmp/mysql3307.sock

pid-file = /data/mysql/mysql3307/mysql3307.pid

log-output = file

slow_query_log = 1

long_query_time = 1

slow_query_log_file = /data/mysql/mysql3307/log/slow.log

log-bin = /data/mysql/mysql3307/log/mysql-bin

binlog_format = row

binlog_rows-query-log_events = 1

expire_logs_days = 30

log-error = /data/mysql/mysql3307/log/error.log

explicit_defaults_for_timestamp=true

character_set_server = utf8

[mysqld3308]

port = 3308

mysqld = mysqld

mysqladmin = mysqladmin

datadir = /data/mysql/mysql3308/data

language = /usr/local/mysql/share/english/

server-id = 3308

socket = /tmp/mysql3308.sock

pid-file = /data/mysql/mysql3308/mysql3308.pid

log-output = file

slow_query_log = 1

long_query_time = 1

slow_query_log_file = /data/mysql/mysql3308/log/slow.log

log-bin = /data/mysql/mysql3308/log/mysql-bin

binlog_format = row

binlog_rows-query-log_events = 1

expire_logs_days = 30

log-error = /data/mysql/mysql3308/log/error.log

explicit_defaults_for_timestamp=true

character_set_server = utf8

[mysqld3309]

port = 3309

mysqld = mysqld

mysqladmin = mysqladmin

datadir = /data/mysql/mysql3309/data

language = /usr/local/mysql/share/english/

server-id = 3309

socket = /tmp/mysql3309.sock

pid-file = /data/mysql/mysql3309/mysql3309.pid

log-output = file

slow_query_log = 1

long_query_time = 1

slow_query_log_file = /data/mysql/mysql3309/log/slow.log

log-bin = /data/mysql/mysql3309/log/mysql-bin

binlog_format = row

binlog_rows-query-log_events = 1

expire_logs_days = 30

log-error = /data/mysql/mysql3309/log/error.log

explicit_defaults_for_timestamp=true

character_set_server = utf8

10、初始化数据库(5.6)

[root@SQL ~]# /usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/data/mysql/mysql3307/data --basedir=/usr/local/mysql/ --socket=/tmp/mysql3307.sock

[root@SQL ~]# /usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/data/mysql/mysql3308/data --basedir=/usr/local/mysql/ --socket=/tmp/mysql3308.sock

[root@SQL ~]# /usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/data/mysql/mysql3309/data --basedir=/usr/local/mysql/ --socket=/tmp/mysql3309.sock

备注:(5.7)

# cd /usr/local/mysql/

[root@SQL mysql]# ./bin/mysqld --initialize-insecure --user=mysql --datadir=/data/mysql/mysql3307/data --basedir=/usr/local/mysql/ --socket=/tmp/mysql3307.sock

[root@SQL mysql]# ./bin/mysqld --initialize-insecure --user=mysql --datadir=/data/mysql/mysql3308/data --basedir=/usr/local/mysql/ --socket=/tmp/mysql3308.sock

[root@SQL mysql]# ./bin/mysqld --initialize-insecure --user=mysql --datadir=/data/mysql/mysql3309/data --basedir=/usr/local/mysql/ --socket=/tmp/mysql3309.sock

检查数据库是否初始化成功状态

出现两个”OK”

MySQL 多实例安装_安装_02

MySQL 多实例安装_安装_04

11、查看数据库是否初始化成功

查看3307数据库

[root@SQL ~]# ll /data/mysql/mysql3307/data/

MySQL 多实例安装_安装_06

12、设置启动文件

# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql

13、mysqld_multi进行多实例管理

启动全部实例

# /usr/local/mysql/bin/mysqld_multi report

查看全部实例状态

[root@SQL ~]# /usr/local/mysql/bin/mysqld_multi report

1、运行失败,如下图:

MySQL 多实例安装_多实例_08

#数据库日志报如下错误

MySQL 多实例安装_安装_10

#mysqld_multi错误日志如下

MySQL 多实例安装_多实例_12

解决方式:

# vim /etc/my.cnf

[mysql330*]

language = /usr/local/mysql/share/english/

MySQL 多实例安装_安装_14

启动单个实例:/usr/local/mysql/bin/mysqld_multi start 3306

停止单个实例:

/usr/local/mysql/bin/mysqladmin -S /mysql/mysql3306/mysql3306.sock -u root -p shutdown

或者

/usr/local/mysql/bin/mysqld_multi stop 3306

查看单个实例状态:/usr/local/mysql/bin/mysqld_multi report 3306        

14、查看启动进程   

MySQL 多实例安装_mysq_16

15、修改密码

# mysql -S /tmp/mysql3307.sock

mysql> set password for root@'localhost'=password('123456');

mysql> flush privileges;

MySQL 多实例安装_安装_18

其他实例同理

16、新建用户并授权

# mysql -S /tmp/mysql3307.sock  -p

mysql> use mysql

mysql> grant select,delete,update,insert on *.* to jiangjj@'%' identified by '123456';

mysql> flush privileges;

MySQL 多实例安装_多实例_20

注意:

mysqld_multi关闭不了MySQL处理Tips

vim /usr/local/mysql/bin/mysqld_multi

    修改mysqld_mutli 把

    my $com= join ' ', 'my_print_defaults ', @defaults_options, $group;

    替换为:

    my $com= join ' ', 'my_print_defaults -s', @defaults_options, $group;

MySQL 多实例安装_多实例_22

补充:mysql5.7配置类似,只是在配置数据库账号密码用法不同