> 数据库 > MySQL >

MySQL启多个实例

很多朋友都想在一台服务器上运行多个MySQL Instance,究竟怎么做呢?
首先要明晰几个原理, 简称为mysqld读取my.cnf的顺序:
第一搜,首先读取/etc/my.cnf,多实例这个配置文件不会存在。:-(
第二搜,$datadir/my.cnf,在data目录下寻找此配置文件。
第三搜,defaultfile=/path/my.cnf 通常写在命令行上,mysqld_safe defaultfile=/tmp/my.cnf &等执行。
第四搜,~/my.cnf 当前用户下的配置文件。

mysqld多进程运行的必要条件,就是pid文件,datadir,socket,port是独立分开的。
| Variable_name | Value |
| datadir | /home/mysql/ |
| Variable_name | Value |
| socket | /home/mysql/mysql.sock |
| Variable_name | Value |
| pid_file | /home/mysql/mysql.pid |
| Variable_name | Value |
| port | 3306 |

单一版本实现多实例
#pwd
/usr/local/
#touch my_multi.cnf
#cat my_multi.cnf
[mysqld_multi]
mysqld = /usr/bin/mysqld_safe
mysqladmin = /usr/bin/mysqladmin
user = root

[mysqld1]
socket = /tmp/mysql_3301.sock
port = 3301
pid-file = /usr/local/mysql_3301/data/mysql_3301.pid
datadir = /usr/local/mysql_3301/data/
log = /usr/local/mysql_3301/data/mysql_3301.log
user = mysql

[mysqld2]
socket = /tmp/mysql_3302.sock
port = 3302
pid-file = /usr/local/mysql_3302/data/mysql_3302.pid
datadir = /usr/local/mysql_3302/data/
log = /usr/local/mysql_3302/data/mysql_3302.log
user = mysql

启动mysqld 的两个实例

/usr/bin/mysqld_multi –config-file=./my_multi.cnf start 1,2

Starting mysqld daemon with databases from /usr/local/mysql_3301/data/
Starting mysqld daemon with databases from /usr/local/mysql_3302/data/
http://linux.it.net.cn

查看3301,3302端口是否被监听
# netstat -lt |grep 33
tcp 0 0 *:3301 *:* LISTEN
tcp 0 0 *:3302 *:* LISTEN

测试mysql是否可以连通

#pwd
/usr/local/
通过/tmp/mysql_3301.sock连接3001端口的mysqld
# bin/mysql -u root -S /tmp/mysql_3301.sock -e “SELECT @@VERSION;”
停止3001端口的mysqld
# /usr/bin/mysqld_multi –config-file=./my_multi.cnf stop 1
STOPPING server from pid file /usr/local/mysql_3301/data/mysql_3301.pid
091010 11:56:04 mysqld ended
再次连接,系统会提示错误
# bin/mysql -u root -S /tmp/mysql_3301.sock -e “SELECT @@VERSION;”
ERROR 2002 (HY000):
Can’t connect to local MySQL server through socket ‘/tmp/mysql_3301.sock’ (2)
相同的步骤测试3002端口的mysqld,输出与上面相同。
# /usr/bin/mysql -u root -S /tmp/mysql_3302.sock -e “SELECT @@VERSION;”
# /usr/bin/mysqld_multi –config-file=./my_multi.cnf stop 2
# /usr/bin/mysql -u root -S /tmp/mysql_3302.sock -e “SELECT @@VERSION;”

补充上两句:
[Nova@imdba.cn mysql]# cp ../mysql-5.0.67/support-files/my-large.cnf /etc/my.cnf
[Nova@imdba.cn mysql]# cp ../mysql-5.0.67/support-files/mysql.server /etc/rc.d/init.d/mysqld
[Nova@imdba.cn mysql]# chkconfig –add mysql
[Nova@imdba.cn mysql]# chkconfig –level mysql 345 on
[Nova@imdba.cn mysql]# chkconfig –list mysql





(责任编辑:IT)