MySQL数据库的多实例安装


0. summary

1. 多实例介绍
2. 参数要求
3. 安装和管理多实例
.   3.1 初始化数据库目录
.   3.2 启动和停止多实例
.   3.3 多实例的连接
.   3.4 拷贝启动脚本,方便自启
.   3.5 mysqld_multi命令的实质
.   3.6 mysqld_mutli管理多版本的mysql 

1. 多实例介绍

  • 一台服务器上安装多个MySQL数据库实例
  • 可以充分利用服务器的硬件资源
  • 通过mysqld_multi进行管理

2. 参数要求

[mysqld_multi]
mysqld=/usr/local/mysql/bin/mysqld_safe                 ---- 也可以通过mysqld来启动,是否需要看应用场景
mysqladmin=/usr/local/mysql/bin/mysqladmin              ---- 进行关闭的
log=/usr/local/mysql/mysqld_multi.log                   ---- 日志位置,要在mysql组下面

[mysqld1]
#slave-parallel-type=LOGICAL-CLOCK
#slave-parallel-workers=4
server-id=110
innodb_page_size=16384
innodb_buffer_pool_size=500M
basedir=/usr/local/mysql/
datadir=/mdata/data1                            ---- 这是需要配置的
socket=/tmp/mysql.sock1                         ---- 这是需要配置的
port=3307                               ---- 这是需要配置的

[mysqld2]
#slave-parallel-type=LOGICAL-CLOCK
#slave-parallel-workers=4
server-id=120
innodb_page_size=16384
innodb_buffer_pool_size=500M
basedir=/usr/local/mysql/
datadir=/mdata/data2                            ---- 这是需要配置的
socket=/tmp/mysql.sock2                         ---- 这是需要配置的
port=3308                               ---- 这是需要配置的

[mysqld3]
plugin_dir=/usr/local/mysql56/lib/plugin
#plugin_load = "rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
server-id=130
innodb_buffer_pool_size=500M
basedir=/usr/local/mysql56/
datadir=/mdata/data3
socket=/tmp/mysql.sock3
port=3309

3. 安装和管理多实例

3.1 初始化数据库目录

[root@lab11g mysql]# bin/mysqld --initialize --user=mysql --datadir=/mdata/data1
[root@lab11g mysql]# bin/mysqld --initialize --user=mysql --datadir=/mdata/data2

初始化完成后检查error.log, 并记下临时密码

3.2 启动和停止多实例

#### 启动单个实例 ####

[root@lab11g mysql]# mysqld_multi start 1

#### 启动所有 ####

[root@lab11g mysql]# mysqld_multi start

#### 查看实例状态 ####

[root@lab11g mysql]# mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld1 is running
MySQL server from group: mysqld2 is running

#### 停止到单个实例 ####

[root@lab11g mysql]# mysqld_multi stop 1

#### 停止所有 ####

[root@lab11g mysql]# mysqld_multi stop

3.3 多实例的连接

[root@lab11g mysql]# mysql -u root -S /tmp/mysql.sock1 -P 3307 -p
[root@lab11g mysql]# mysql -u root -S /tmp/mysql.sock2 -P 3308 -p

3.4 拷贝启动脚本,方便自启

cp /usr/local/mysql/support-files/mysqld_multi.server  /etc/init.d/mysqld_multid
chkconfig --add mysqld_multi.server
chkconfig mysqld_multid.server on

3.5 mysqld_multi命令的实质

# 1.COMMON USER
#
#   Make sure that the MySQL user, who is stopping the mysqld services, has
#   the same password to all MySQL servers being accessed by $my_progname.
#   This user needs to have the 'Shutdown_priv' -privilege, but for security
#   reasons should have no other privileges. It is advised that you create a
#   common 'multi_admin' user for all MySQL servers being controlled by
#   $my_progname. Here is an example how to do it:
#
#   GRANT SHUTDOWN ON *.* TO multi_admin\@localhost IDENTIFIED BY 'password'
#
#   You will need to apply the above to all MySQL servers that are being
#   controlled by $my_progname. 'multi_admin' will shutdown the servers
#   using 'mysqladmin' -binary, when '$my_progname stop' is being called.
......

实际上mysqld_multi就是通过mysqladmin来管理实例的,所以配置文件之前配上了mysqladmin的路径。

[root@lab11g ~]# mysqladmin -u root shutdown
[root@lab11g ~]# netstat -ntl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State      
tcp        0      0 127.0.0.1:2208              0.0.0.0:*                   LISTEN      
tcp        0      0 192.168.1.21:3307           0.0.0.0:*                   LISTEN      
tcp        0      0 192.168.1.21:3308           0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:21                  0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      
tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:733                 0.0.0.0:*                   LISTEN      
tcp        0      0 127.0.0.1:2207              0.0.0.0:*                   LISTEN
[root@lab11g ~]# mysqladmin -u root shutdown -S /tmp/mysql.sock1
[root@lab11g ~]# netstat -ntl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State      
tcp        0      0 127.0.0.1:2208              0.0.0.0:*                   LISTEN      
tcp        0      0 192.168.1.21:3308           0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:21                  0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      
tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      
tcp        0      0 0.0.0.0:733                 0.0.0.0:*                   LISTEN      
tcp        0      0 127.0.0.1:2207              0.0.0.0:*                   LISTEN    

3.6 mysqld_mutli管理多版本的mysql

管理多个实例实际上还可以用

mysqld_safe --defaults-file=/etc/my2.cnf

但是这样很麻烦,而且mysqld_multi还可以管理不同版本的mysql, 如下:

[root@lab11g local]# ls -ltr
总计 88
drwxr-xr-x  2 root root  4096 2009-10-01 src
drwxr-xr-x  2 root root  4096 2009-10-01 sbin
drwxr-xr-x  2 root root  4096 2009-10-01 libexec
drwxr-xr-x  2 root root  4096 2009-10-01 lib64
drwxr-xr-x  2 root root  4096 2009-10-01 lib
drwxr-xr-x  2 root root  4096 2009-10-01 include
drwxr-xr-x  2 root root  4096 2009-10-01 games
drwxr-xr-x  2 root root  4096 2009-10-01 etc
drwxr-xr-x  5 root root  4096 2015-04-18 share
drwxr-xr-x  2 root root  4096 2015-04-18 bin
drwxr-xr-x 13 root mysql 4096 06-25 23:48 mysql-5.6.31-linux-glibc2.5-x86_64
lrwxrwxrwx  1 root root    34 07-06 20:34 mysql -> mysql-5.7.13-linux-glibc2.5-x86_64
drwxr-xr-x 11 root mysql 4096 07-08 15:02 mysql-5.7.13-linux-glibc2.5-x86_64
lrwxrwxrwx  1 root root    45 07-08 17:23 mysql56 -> /usr/local/mysql-5.6.31-linux-glibc2.5-x86_64
[root@lab11g local]# cd mysql56/
[root@lab11g mysql56]# chown -R mysql .
[root@lab11g mysql56]# chgrp -R mysql .
[root@lab11g mysql56]# vi /etc/my.cnf                   ---- 修改autocommit
[root@lab11g mysql56]# scripts/mysql_install_db --user=mysql --datadir=/mdata/data3 --basedir=/usr/local/mysql56
Installing MySQL system tables...2016-07-12 16:14:28 0 [Warning] 'ERROR_FOR_DIVISION_BY_ZERO' is deprecated and will be removed in a future release.
2016-07-12 16:14:28 0 [Warning] 'NO_ZERO_DATE' is deprecated and will be removed in a future release.
2016-07-12 16:14:28 0 [Warning] 'NO_ZERO_IN_DATE' is deprecated and will be removed in a future release.
2016-07-12 16:14:28 0 [Note] /usr/local/mysql56/bin/mysqld (mysqld 5.6.31-log) starting as process 18654 ...

OK

Filling help tables...2016-07-12 16:14:37 0 [Warning] 'ERROR_FOR_DIVISION_BY_ZERO' is deprecated and will be removed in a future release.
2016-07-12 16:14:37 0 [Warning] 'NO_ZERO_DATE' is deprecated and will be removed in a future release.
2016-07-12 16:14:37 0 [Warning] 'NO_ZERO_IN_DATE' is deprecated and will be removed in a future release.
2016-07-12 16:14:37 0 [Note] /usr/local/mysql56/bin/mysqld (mysqld 5.6.31-log) starting as process 18682 ...
OK

......
[root@lab11g mysql56]# mysqld_multi start
[root@lab11g data3]# mysqld_multi report
Reporting MySQL servers
MySQL server from group: mysqld1 is running
MySQL server from group: mysqld2 is running
MySQL server from group: mysqld3 is running

#### 测试连接 ####

[root@lab11g data3]# mysql -u root -S /tmp/mysql.sock3 -P 3309
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@lab11g data3]# mysql -u root -S /tmp/mysql.sock3 -P 3309 -p
Enter password: 
......
mysql> set password=password('mysql');
Query OK, 0 rows affected (0.01 sec)

mysql> exit
Bye
[root@lab11g data3]# mysql -u root -S /tmp/mysql.sock3 -P 3309
......

mysql>

你可能感兴趣的:(MySQL数据库的多实例安装)