曲径通幽论坛

 找回密码
 立即注册
搜索
查看: 4032|回复: 0
打印 上一主题 下一主题

使用 mysqld_multi 启动多个服务器实例

[复制链接]

4918

主题

5880

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34395
跳转到指定楼层
楼主
发表于 2013-3-6 00:37:48 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
mysql_multi 可以管理多个 mysqld 进程,这些服务进程在不同的 Unix socket 文件和 TCP/IP 端口上监听,并且可以启动/停止服务器,或报告它们的当前状态。

下面以实际操作来演示如何使用 mysqld_multi 脚本及相关文件的配置。

1. 先检查 mysqld 是否在运行,如果在运行,可先关闭:
# mysqladmin ping
mysqld is alive     #服务器正在运行

# mysqladmin shutdown     #关闭之

# ps -ef |grep mysql
root      7995  7975  0 20:44 pts/1    00:00:00 grep mysql    #确认已经关闭

在 《 CentOS6.3 源码安装 MySQL 5.6.10 》里说到过,数据文件就放在 /opt/mysql-5.6.10/data 目录下。实际上,如果只有一个 mysqld 实例的话,它就对应这个目录;但是如果有多个 mysqld 实例,那么就需要为他们分别建立对应的目录,比如:
#mkdir -p /opt/mysql-5.6.10/data2
mkdir -p /opt/mysql-5.6.10/data3

创建完目录后,需要修改一下目录的权限:
# cd /opt/mysql-5.6.10
# chown mysql.mysql ./data2 –R
# chown mysql.mysql ./data3 –R

2. 接着,使用 mysql_install_db 脚本对上面建立的 data2 和 data3 这两个目录进行初始化。如果想了解 mysql_install_db 的选项用法,可以敲入命令 mysql_install_db --help 。
[root@centos mysql-5.6.10]# ./scripts/mysql_install_db --basedir=/opt/mysql-5.6.10 --datadir=/opt/mysql-5.6.10/data2 --user=mysql
[root@centos mysql-5.6.10]# ./scripts/mysql_install_db --basedir=/opt/mysql-5.6.10 --datadir=/opt/mysql-5.6.10/data3 --user=mysql
初始化完后,可以检查一下数据库目录:
[root@centos mysql-5.6.10]# cd data2
[root@centos data2]# ll
total 110604
-rw-rw----. 1 mysql mysql 12582912 Mar  5 22:02 ibdata1
-rw-rw----. 1 mysql mysql 50331648 Mar  5 22:02 ib_logfile0
-rw-rw----. 1 mysql mysql 50331648 Mar  5 20:57 ib_logfile1
drwx------. 2 mysql mysql     4096 Mar  5 20:57 mysql
drwx------. 2 mysql mysql     4096 Mar  5 22:02 performance_schema
drwx------. 2 mysql mysql     4096 Mar  5 20:57 test


3. 接下来需要编辑一下 my.cnf 这个文件了。在修改之前,最好做一个备份:
# cp my.cnf my.cnf.bak

由于 mysqld_multi 脚本是从 my.cnf 文件里的 [mysqld_multi] 组里查找相应信息的,因此可以对 my.cnf 里的该组进行编辑,如果没有则添加之,比如:
[mysqld_multi]
mysqld          = /opt/mysql-5.6.10/bin/mysqld_safe
mysqladmin      = /opt/mysql-5.6.10/bin/mysqladmin
user            = groad
password        = 123456

[mysqld1]
socket          = /tmp/mysql1.sock
port            = 3306
pid-file        = /tmp/mysql1.pid
basedir = /opt/mysql-5.6.10
datadir = /opt/mysql-5.6.10/data
log_error = /opt/mysql-5.6.10/mysql1_error.log
user = mysql

[mysqld2]
socket          = /tmp/mysql2.sock
port            = 3307
pid-file        = /tmp/mysql2.pid
basedir = /opt/mysql-5.6.10
datadir = /opt/mysql-5.6.10/data2
log_error = /opt/mysql-5.6.10/mysql2_error.log
user = mysql

[mysqld3]
socket          = /tmp/mysql3.sock
port            = 3308
pid-file        = /tmp/mysql3.pid
basedir = /opt/mysql-5.6.10
datadir = /opt/mysql-5.6.10/data3
log_error = /opt/mysql-5.6.10/mysql3_error.log
user = mysql
上面在 [mysqld_multi] 组下指定了一个用户及其密码。注意,这个用户 groad 并不是系统用户,而是数据库用户。
另外,还建立了 3 个组:[mysql1], [mysql2], [mysql3] ,这 3 个组将对应 3 个服务实例,其中编号 1,2,3 在官方说明手册里称之为 GNR 。

配置完后,检测一下启动是否成功。mysqld_multi 的使用语法如下:
mysqld_multi [options] {start|stop|reload|report} [GNR[,GNR] ...]
按照上面的语法,试着启动一下服务器:
[root@centos mysql-5.6.10]# ./bin/mysqld_multi --defaults-file=./my.cnf start 1
[root@centos mysql-5.6.10]# ps -ef | grep "mysql"
root      8677     1  0 23:13 pts/1    00:00:00 /bin/sh /opt/mysql-5.6.10/bin/mysqld_safe --socket=/tmp/mysql1.sock --port=3306 --pid-file=/tmp/mysql2.pid --basedir=/opt/mysql-5.6.10 --datadir=/opt/mysql-5.6.10/data --log_error=/opt/mysql-5.6.10/mysql1_error.log --user=mysql
mysql     8834  8677 17 23:13 pts/1    00:00:00 /opt/mysql-5.6.10/bin/mysqld --basedir=/opt/mysql-5.6.10 --datadir=/opt/mysql-5.6.10/data --plugin-dir=/opt/mysql-5.6.10/lib/plugin --user=mysql --log-error=/opt/mysql-5.6.10/mysql1_error.log --pid-file=/tmp/mysql2.pid --socket=/tmp/mysql1.sock --port=3306
root      8858  7975  0 23:14 pts/1    00:00:00 grep mysql
可见启动是成功的。由上面的输出信息还可以看到,是 root 的首先执行了 mysql_safe ,然后再以 mysql 这个用户调用了 mysqld 这个程序;这和 my.cnf 里 [mysqld_multi] 组里配置的 mysqld          = /opt/mysql-5.6.10/bin/mysqld_safe 是对应的。

同样,可以再启动监听在 3307 和 3308 端口的服务实例:
# ./bin/mysqld_multi --defaults-file=./my.cnf start 2
# ./bin/mysqld_multi --defaults-file=./my.cnf start 3

通过检查 /tmp 下生成的 socket 文件也能看到启动成功:


现在使用客户端通过 socket 文件进行登录:
[root@centos mysql-5.6.10]# mysql -S /tmp/mysql1.sock
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.10 Source distribution

Copyright (c) 2000, 2013, 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.

mysql>


[root@centos mysql-5.6.10]# mysql -S /tmp/mysql2.sock
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.10 Source distribution

Copyright (c) 2000, 2013, 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.

mysql>
这些登录都是成功的。


那如何关闭这些服务器呢?
在上面所配置的 my.cnf 文件的 [mysqld_multi] 组里可以看到,mysqld          = /opt/mysql-5.6.10/bin/mysqld_safe 这一行所指定的 mysqld_safe 是负责启动的,下一行 mysqladmin      = /opt/mysql-5.6.10/bin/mysqladmin 里指定的 mysqladmin 则是负责关闭和打印服务器状态的,并且关闭时还需要指定的用户,即 groad 。

如果直接执行 ./bin/mysqld_multi --defaults-file=./my.cnf stop 1 语句能否将数据库关闭呢:可以来验证一下:
[root@centos mysql-5.6.10]# ./bin/mysqld_multi --defaults-file=./my.cnf stop 1
[root@centos mysql-5.6.10]# ps -ef |grep "3306"
root      2116     1  0 Mar05 pts/0    00:00:00 /bin/sh /opt/mysql-5.6.10/bin/mysqld_safe --socket=/tmp/mysql1.sock --port=3306 --pid-file=/tmp/mysql1.pid --basedir=/opt/mysql-5.6.10 --datadir=/opt/mysql-5.6.10/data --log_error=/opt/mysql-5.6.10/mysql1_error.log --user=mysql
mysql     2271  2116  0 Mar05 pts/0    00:00:00 /opt/mysql-5.6.10/bin/mysqld --basedir=/opt/mysql-5.6.10 --datadir=/opt/mysql-5.6.10/data --plugin-dir=/opt/mysql-5.6.10/lib/plugin --user=mysql --log-error=/opt/mysql-5.6.10/mysql1_error.log --pid-file=/tmp/mysql1.pid --socket=/tmp/mysql1.sock --port=3306
root      2771  2092  0 00:11 pts/0    00:00:00 grep 3306

为什么没法关闭呢?可以看一下相关日志以找到原因。其中,myslqd_multi 所产生的日志存放在 share 目录下,其名称为 mysqld_multi.log ,打开查看可以看到:

由上面可以看到,数据库中并没有 groad 这个用户,因此必须先创建该用户:
[root@centos mysql-5.6.10]# mysql -S /tmp/mysql1.sock            # 先登录 mysql1 这个服务器实例
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.10 Source distribution

Copyright (c) 2000, 2013, 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.

mysql>grant shutdown on *.* to 'groad'@'localhost' identified by '123456';           #注意一定要给出 my.cnf 文件里给出的密码
Query OK, 0 rows affected (0.00 sec)
好,现在用上面的 groad 用户关闭一下数据库:
[root@centos mysql-5.6.10]# ./bin/mysqld_multi --defaults-file=./my.cnf stop 1
[root@centos mysql-5.6.10]# ps -ef |grep "3306"
root      2858  2092  0 00:39 pts/0    00:00:00 grep 3306
这下关掉了,如果查看 /tmp 目录,还可以发现 mysql1.sock 文件也被删除。同样的方法关掉其它的两个服务实例。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|曲径通幽 ( 琼ICP备11001422号-1|公安备案:46900502000207 )

GMT+8, 2025-5-4 16:38 , Processed in 0.066021 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表