很多朋友都想在一台伺服器上運行多個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/
查看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