一,绪言
在Mysql中有一mysqld_multi命令,可用于在一台物理服务器运行多个Mysql服务,今天参考一些文档,亲自测试并通过,真高兴,现将操作过程共享给大家!
操作系统:Linux 2.6.13 (Slackware),其它版的Linux应该也差不多.
数据库:Mysql 4.0.17 源程序安装(相信最新的5.1.*也差不多,过些天再试试)
规划:运行4个mysql服务:
假设服务器名:db-app (IP为192.168.0.100),
假设服务器名:db-app2 (IP为192.168.0.101),
下文直到第二部分,均为讲述db-app中的mysql,
二,准备
用mysql源程序安装,假设在安装时用的configura选择项氯?
./configure --prefix=/usr/local/mysql --datadir=/usr/local/mysql/data1 --sysconfdir=/etc
备注:--prefix将MYSQL安装到/usr/local/mysql,
--datadir将数据库生成/usr/local/mysql/data1
sysconfdir是指定mysql使用到的my.cnf配置文件的搜索路径为/etc
其他mysql安装过程略.
根据Mysql管理手册中提到:每个Mysql的服务都可为独立的,所以它都调用一个my.cnf中各自不同的启动选项--就是下文中将提到的GNR值,使用不同的端口,生成各自的套接文件,服务的数据库都是独立的(更多可查阅mysql官方网站的英文管理手册).
mysqld_multi是管理多个mysqld的服务进程,这些服务进程程序不同的unix socket或是监听于不同的端口。他可以启动、停止和监控当前的服务状态。
----程序在my.cnf(或是在--config-file自定义的配置文件)中搜索[mysqld#]段,"#"可以是任意的正整数。这个正整数就是在下面提及的段序列,即GNR。段的序号做为mysqld_multi的参数,来区别不同的段,这样你就可以控制特定mysqld进程的启动、停止或得到他的报告信息。这些组里的参数就像启动一个mysqld所需要的组的参数一样。但是,如果使用多服务,必须为每个服务指定一个unix socket或端口(摘自http://mifor.4dian.org中的使用mysqld_multi程序管理多个MySQL服务 )。
从上述文字可看到多Mysql服务中最重要的就是my.cnf配置文件了.
现我贴出我的my.cnf文件.-----------------------
[mysqld_multi]
mysqld = /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin
user = mysql (用一个帐号来启动所有的mysql服务器,因为是用一相同的帐号。那个么这帐号必须都是每个mysql服务都要用的帐号,最好是管理帐号,下面的口令与相同)
password = mypaswd
[mysqld1]
port = 3306
socket = /tmp/mysql.sock1
pid-file=/usr/local/mysql/data1/db-app1.pid
log=/usr/local/mysql/data1/db-app.log
datadir = /usr/local/mysql/data
user = mysql
[mysqld2]
port = 3307
socket = /tmp/mysql.sock2
pid-file = /usr/local/mysql/data2/db-app2.pid
datadir = /usr/local/mysql/data2
log=/usr/local/mysql/data2/db-app.log
user = mysql
[mysqld3]
port = 3308
socket = /tmp/mysql.sock3
pid-file = /usr/local/mysql/data3/db-app3.pid3
datadir = /usr/local/mysql/data3
log=/usr/local/mysql/data3/db-app.log
user = mysql
[mysqld4]
port = 3309
socket = /tmp/mysql.sock4
pid-file = /usr/local/mysql/data3/db-app4.pid
datadir = /usr/local/mysql/data4
log=/usr/local/mysql/data4/db-app.log
user = mysql
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[isamchk]
key_buffer = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[myisamchk]
key_buffer = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
我的配置文件中有mysqld1,mysqld2,mysqld3,mysqld4。就是说我将启动4个mysql服务在同一服务器的不同端口——3306-3309,每datadir所指定的数据库文件路径都是不相同的,都有各自不同的日志文件。其它一些设置可用my.cnf 原来的内容.
ok, 重要的my.cnf编好的,现在为配置中不同mysql 服务建立各自文件夹和初始数据库等.
[mysqld1]是一个默认的,在我们安装mysql时已经有了,所以不用管它.
[mysqld2],只要根据配置就的路径为它建立一个目录就可以了.将把该目录改为mysql管理权
db-app:/ # mkdir /usr/local/mysql/data2
建数据库,我们可以把默认的mysql数据库复制过来,以利用其它的mysql帐号,其它数据库根据应用再建立.
db-app:/ # cp /usr/local/mysql/data1/mysql /usr/local/mysql/data2 -R
db-app:/ # chmod mysql.mysql /usr/local/mysql/data2 -R
[mysqld3],[mysqld4], 相同.
我们可能看看这些目录是否都存在.
db-app:/ # ls -l /usr/local/mysql/
drwxr-xr-x 6 mysql mysql 4096 Apr 9 17:54 data4
drwxr-x--- 2 mysql mysql 4096 Apr 9 17:14 data1
drwxr-xr-x 3 mysql mysql 4096 Apr 9 17:54 data2
drwxr-xr-x 3 mysql mysql 4096 Apr 9 17:54 data3
现在可以通过mysqld_multi启动了。
三,mysqld_multi命令.
使用如下参数来启动mysqld_multi: (注:该命令在mysql的bin目录中,根据上面所提到./configure --prefix=/usr/local/mysql ,所以该文件应该在 /usr/local/mysq/bin, 这得根据你安装时所指定的路径 )
db-app:/ # mysqld_multi [options] {start|stop|report} [GNR[,GNR]...]
start,stop和report是指你想到执行的操作。你可以在单独的服务或是多服务上指定一个操作,区别于选项后面的GNR列表。如果没有指定GNR列表,那么mysqld_multi将在所有的服务中根据选项文件进行操作。
每一个GNR的值是组的序列号或是一个组的序列号范围。此项的值必须是组名字最后的数字,比如说如果组名为mysqld17,那么此项的值则为17.如果指定一个范围,使用"-"(破折号)来连接二个数字。如GNR的值为10-13,则指组mysqld10到组mysqld13。多个组或是组范围可以在命令行中指定,使用","(逗号)隔开。不能有空白的字符(如空格或tab),在空白字符后面的参数将会被忽略。 (注:GNR值就是我们定义my.cnf中mysqld#中的值,我这里只有1-4).
db-app:/ # /usr/local/mysq/bin/mysqld_multi --config-file=/etc/my.cnf start 1 只启动 第一个mysql服务,相关文件由my.cnf中mysql1设定.
db-app:/ # /usr/local/mysq/bin/mysqld_multi --config-file=/etc/my.cnf stop 1 启止 第一个mysql服务
db-app:/ # /usr/local/mysq/bin/mysqld_multi --config-file=/etc/my.cnf start 1-4, 启动 第1至4mysql服务,其实就是我这里的全部.
db-app:/ # /usr/local/mysq/bin/mysqld_multi --config-file=/etc/my.cnf report 1-4
查看启动:
db-app:/ # ps aux
root 10467 0.0 0.2 2712 1300 pts/0 S 18:59 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --port=3306 --socket=/tmp/mysql.sock1
root 10475 0.0 0.2 2712 1300 pts/0 S 18:59 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --port=3307 --socket=/tmp/mysql.sock2
root 10482 0.0 0.2 2716 1300 pts/0 S 18:59 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --port=3308 --socket=/tmp/mysql.sock3
root 10487 0.0 0.2 2716 1300 pts/0 S 18:59 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --port=3309 --socket=/tmp/mysql.sock4
.............................................
四,客户端访问
任何客户端访问都需要指定访问端口.方才能进入指定数据库服务.否则将使用到Mysql默认的端口(3306)所服务的MYSQL。
**********************************************************
第二部分, 在一台服务器构建多mysql主 服务.
**********************************************************
提示:建立一个帐号,专门用于下面主从复制,本例用的的帐号repl, 口令为:'1234567890', 可让其拥有select_priv, reload_priv,process_priv,grant_priv,super_priv,repl_slave_priv,repl_client_priv权限,并能通过任何客户进行访问,即访问客户为'%'。
主要是修改my.cnf中的内容,让每个mysql生成自己的bin-log文件及各自的运行环境,现贴上我的my.cnf全部内容,相关参数与请参考mysql 官文手册.
#[client]
#password = your_password
#port = 3306
#socket = /tmp/mysql.sock
[mysqld_multi]
mysqld = /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin
user = mysql
password = mypasswd
[mysqld1]
port = 3306
socket = /tmp/mysql.sock1
skip-locking
pid-file=/usr/local/mysql/data/net-app1a.pid
datadir = /usr/local/mysql/data
log=/usr/local/mysql/data/net-app1.log
user = mysql
log-slow-queries=/usr/local/mysql/data/slowquery.log
long_query_time = 2
key_buffer = 256M
max_allowed_packet = 1M
table_cache = 512
sort_buffer_size = 2M
read_buffer_size = 2M
myisam_sort_buffer_size = 64M
thread_cache = 32
query_cache_size = 32M
thread_concurrency = 2
max_connections=500
log-bin
log-bin=/usr/local/mysql/data/app-net1_1-bin
server-id = 1
[mysqld2]
port = 3307
socket = /tmp/mysql.sock2
pid-file = /usr/local/mysql/data2/net-app1b.pid
datadir = /usr/local/mysql/data2
log=/usr/local/mysql/data2/net-app1.log
user = mysql
log-slow-queries=/usr/local/mysql/data2/slowquery.log
long_query_time = 10
key_buffer = 128M
max_allowed_packet = 1M
table_cache = 512
sort_buffer_size = 1M
read_buffer_size = 1M
myisam_sort_buffer_size = 32M
thread_cache = 32
query_cache_size = 16M
thread_concurrency = 2
max_connections=300
log-bin
log-bin=/usr/local/mysql/data2/app-net1_2-bin
server-id = 1
[mysqld3]
port = 3308
socket = /tmp/mysql.sock3
skip-locking
pid-file = /usr/local/mysql/data3/net-app1c.pid
datadir = /usr/local/mysql/data3
log=/usr/local/mysql/data3/net-app1.log
user = mysql
log-bin
log-bin=/usr/local/mysql/data3/app-net1_3-bin
server-id = 1
[mysqld4]
port = 3309
socket = /tmp/mysql.sock4
skip-locking
pid-file = /usr/local/mysql/data1/app-net1d.pid
datadir = /usr/local/mysql/data1
log=/usr/local/mysql/data1/net-app1.log
user = mysql
log-bin
log-bin=/usr/local/mysql/data1/app-net1_4-bin
server-id = 1
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates
[isamchk]
key_buffer = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[myisamchk]
key_buffer = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
**********************
启动多mysql服务都是相同的,
db-app:/ # /usr/local/mysq/bin/mysqld_multi --config-file=/etc/my.cnf start 1-4
**********************************************************
第三部分, 在一台服务器构建多mysql 从服务.
**********************************************************
构建从服务器的预选准备: 建议用mysqld_multi 把主服务器的mysql全部停掉.删除数据目录中的所有除数据库目录以外的任何文件(此文中的数据目录库有4个,datadir = /usr/local/mysql/中的 data1 -- data1).建主从都用相同的数据目录路径.
用Tar 命令把每数据库封装起来,并通过sftp命令put/get到从服务器(db-app1 192.168.0.101).
下列操作供参考:
在db-app主机上的操作
db-app:/ # tar -cf data1.tar /usr/local/mysql/data1
db-app:/ # tar -cf data2.tar /usr/local/mysql/data2
db-app:/ # tar -cf data3.tar /usr/local/mysql/data3
db-app:/ # tar -cf data4.tar /usr/local/mysql/data4
在db-app1主机上的操作
db-app1:/ # tar xvf data1.tar
db-app1:/ # tar xvf data2.tar
db-app1:/ # tar xvf data3.tar
db-app1:/ # tar xvf data4.tar
同时,请确认系统帐号mysql是否对主/从服务器的中的mysql数据目录都有操作权限,如果无法确认,你直接更修改这些目录的所有权即可。
在db-app主机上的操作
db-app:/ # chown mysql.mysql /usr/local/mysql/data1 -R
db-app:/ # chown mysql.mysql /usr/local/mysql/data2 -R
db-app:/ # chown mysql.mysql /usr/local/mysql/data3 -R
db-app:/ # chown mysql.mysql /usr/local/mysql/data4 -R
在db-app1主机上的操作
db-app1:/ # chown mysql.mysql /usr/local/mysql/data1 -R
db-app2:/ # chown mysql.mysql /usr/local/mysql/data2 -R
db-app3:/ # chown mysql.mysql /usr/local/mysql/data3 -R
db-app4:/ # chown mysql.mysql /usr/local/mysql/data4 -R
下面就是从服务器上/etc/my.cnf的全部内容.
提示:下面的my.cnf中将会提到一个帐号:repl, 口令为:'1234567890', 这个帐号就是上面专门建立的。
其实都一样,主要是修改my.cnf中的内容,让每个从mysql通过主mysql的不同的端口,去获取各自bin-log来更新自生的数据库内容.现贴上我的my.cnf全部内容(从服务器),相关参数与请参考mysql 官文手册.
#[client]
#password = your_password
#port = 3306
#socket = /tmp/mysql.sock
[mysqld_multi]
mysqld = /usr/local/mysql/bin/mysqld_safe
mysqladmin = /usr/local/mysql/bin/mysqladmin
user = mysql
password = netmoniit
[mysqld1]
port = 3306
socket = /tmp/mysql.sock1
skip-locking
pid-file=/usr/local/mysql/data/net-app1a.pid
datadir = /usr/local/mysql/data
log=/usr/local/mysql/data/net-app1.log
user = mysql
log-slow-queries=/usr/local/mysql/data/slowquery.log
long_query_time = 2
key_buffer = 256M
max_allowed_packet = 1M
table_cache = 512
sort_buffer_size = 2M
read_buffer_size = 2M
myisam_sort_buffer_size = 64M
thread_cache = 32
query_cache_size = 32M
thread_concurrency = 2
max_connections=500
server-id = 2
master-host = 192.168.0.100
master-user = 'repl'
master-password = '1234567890'
master-port = 3309
report-host = net-app1
master-connect-retry = 30
log-bin
log-slave-updates
[mysqld2]
port = 3307
socket = /tmp/mysql.sock2
pid-file = /usr/local/mysql/data2/net-app1b.pid
datadir = /usr/local/mysql/data2
log=/usr/local/mysql/data2/net-app1.log
user = mysql
log-slow-queries=/usr/local/mysql/data2/slowquery.log
long_query_time = 10
key_buffer = 128M
max_allowed_packet = 1M
table_cache = 512
sort_buffer_size = 1M
read_buffer_size = 1M
myisam_sort_buffer_size = 32M
thread_cache = 32
query_cache_size = 16M
thread_concurrency = 2
max_connections=300
server-id = 2
master-host = 192.168.0.100
master-user = 'repl'
master-password = '1234567890'
master-port = 3309
report-host = net-app1
master-connect-retry = 30
log-bin
log-slave-updates
[mysqld3]
port = 3308
socket = /tmp/mysql.sock3
pid-file = /usr/local/mysql/data3/net-app1c.pid
datadir = /usr/local/mysql/data3
log=/usr/local/mysql/data3/net-app1.log
user = mysql
log-slow-queries=/usr/local/mysql/data3/slowquery.log
long_query_time = 10
key_buffer = 128M
max_allowed_packet = 1M
table_cache = 512
sort_buffer_size = 1M
read_buffer_size = 1M
myisam_sort_buffer_size = 32M
thread_cache = 32
query_cache_size = 16M
thread_concurrency = 2
max_connections=300
server-id = 2
master-host = 192.168.0.100
master-user = 'repl'
master-password = '1234567890'
master-port = 3309
report-host = net-app1
master-connect-retry = 30
log-bin
log-slave-updates
[mysqld3]
port = 3308
socket = /tmp/mysql.sock4
pid-file = /usr/local/mysql/data4/net-app1d.pid
datadir = /usr/local/mysql/data4
log=/usr/local/mysql/data4/net-app1.log
user = mysql
log-slow-queries=/usr/local/mysql/data4/slowquery.log
long_query_time = 10
key_buffer = 128M
max_allowed_packet = 1M
table_cache = 512
sort_buffer_size = 1M
read_buffer_size = 1M
myisam_sort_buffer_size = 32M
thread_cache = 32
query_cache_size = 16M
thread_concurrency = 2
max_connections=300
server-id = 2
master-host = 192.168.0.100
master-user = 'repl'
master-password = '1234567890'
master-port = 3309
report-host = net-app1
master-connect-retry = 30
log-bin
log-slave-updates
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates
[isamchk]
key_buffer = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[myisamchk]
key_buffer = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
****************************************
在功告成,现在分别启动两台主机上的多mysql服务,这样,每个主服务的每个mysql有变化,都会自动复制/更新到从服务器对应的数据库中。
db-app:/ # /usr/local/mysq/bin/mysqld_multi --config-file=/etc/my.cnf start 1-4
db-app1:/ # /usr/local/mysq/bin/mysqld_multi --config-file=/etc/my.cnf start 1-4
********************
五,未来测试,
接下来我想做一个多主一从的Mysql服务器复制解决方案! 那位可以给点意见啊!结构如下.
就是有主服务器Server A , Server B和从服务器 Server C, A 和 B运行着不同的数据库应用, 假设数据库名都不同。Server C(假设这三台PC上都只运行了一个mysql服务),包括了A了B服务器的所有Mysql 用户及相同的访问权限, 并集成在一个Mysql服务中. C通过主/从方式复制A和B的数据库。