這裏使用mysqld_multi來安裝多實例的方式 ,安裝mysql略,能夠採起yum直接安裝方式。 html
因爲資源有限,環境需求單臺機器創建兩個mysql實例來做爲測試使用:mysql
路徑分別爲linux
/var/lib/mysql3306sql
/var/lib/mysql3307數據庫
更改屬主、組權限socket
[root@dev02 lib]#chown mysql.mysql /var/lib/mysql3306 -Ride
[root@dev02 lib]#chown mysql.mysql /var/lib/mysql3307 -R測試
初始化mysql:優化
[root@dev02 lib]#cd /var/lib/mysql3306/ui
[root@dev02 lib]#mysql_install_db --datadir=/var/lib/mysql3306 --user=mysql
[root@dev02 lib]#cd /var/lib/mysql3307/
[root@dev02 lib]#mysql_install_db --datadir=/var/lib/mysql3307 --user=mysql
建立多實例的配置文件,能夠在任何路徑下,只要後面啓動時指定此配置文件路徑便可。
注意兩實例的socket,port,datadir,pid的配置要區別開.
[root@dev02 lib]# more /var/lib/my_multi.cnf
[mysqld_multi]
mysqld = /usr/bin/mysqld_safe
mysqladmin = /usr/bin/mysqladmin
user = root
[mysqld1]
socket = /tmp/mysql_3306.sock
port = 3306
pid-file = /var/lib/mysql3306/mysql3306.pid
datadir = /var/lib/mysql3306
log = /var/lib/mysql3306/mysql3306.log
user = mysql
#----------------parameter optimization---------- 參數優化部分可忽略
symbolic-links=0
max_allowed_packet=50M
character-set-server = utf8
#lower_case_table_names=1
general_log=/var/log/codfei.log
skip-external-locking
#skip-name-resolve
tmp_table_size = 256M
sql-mode= STRICT_TRANS_TABLES
default-storage-engine=INNODB
#query_cache_size = 16M
sort_buffer_size = 16M
read_buffer_size = 16M
table_cache = 512
ignore_builtin_innodb
plugin_dir=/usr/lib64/mysql/plugin
plugin_load=innodb=ha_innodb_plugin.so
;innodb_trx=ha_innodb_plugin.so
;innodb_locks=ha_innodb_plugin.so
;innodb_lock_waits=ha_innodb_plugin.so
;innodb_cmp=ha_innodb_plugin.so
;innodb_cmp_reset=ha_innodb_plugin.so
;innodb_cmpmem=ha_innodb_plugin.so
;innodb_cmpmem_reset=ha_innodb_plugin.so
max_connections = 1024
slow_query_log = /var/log/mysqlslowqueries.log
long_query_time = 4
skip-host-cache
max_allowed_packet = 160M
[mysqld2]
socket = /tmp/mysql_3307.sock
port = 3307
pid-file = /var/lib/mysql3306/mysql3307.pid
datadir = /var/lib/mysql3307
log = /var/lib/mysql3306/mysql3307.log
user = mysql
#----------------parameter optimization---------- 參數優化部分可忽略
symbolic-links=0
max_allowed_packet=50M
character-set-server = utf8
#lower_case_table_names=1
general_log=/var/log/codfei.log
skip-external-locking
#skip-name-resolve
tmp_table_size = 256M
sql-mode= STRICT_TRANS_TABLES
default-storage-engine=INNODB
#query_cache_size = 16M
sort_buffer_size = 16M
read_buffer_size = 16M
table_cache = 512
ignore_builtin_innodb
plugin_dir=/usr/lib64/mysql/plugin
plugin_load=innodb=ha_innodb_plugin.so
;innodb_trx=ha_innodb_plugin.so
;innodb_locks=ha_innodb_plugin.so
;innodb_lock_waits=ha_innodb_plugin.so
;innodb_cmp=ha_innodb_plugin.so
;innodb_cmp_reset=ha_innodb_plugin.so
;innodb_cmpmem=ha_innodb_plugin.so
;innodb_cmpmem_reset=ha_innodb_plugin.so
max_connections = 1024
slow_query_log = /var/log/mysqlslowqueries.log
long_query_time = 4
skip-host-cache
max_allowed_packet = 160M
[root@dev02 lib]#chown mysql.mysql /var/lib/my_multi.cnf -R
[root@dev02 lib]#/usr/bin/mysqld_multi --defaults-extra-file=/var/lib/my_multi.cnf start 1,2 啓動多實例,建議加入到開機自啓動中
[root@dev02 lib]# ps aux|grep mysqld 查看兩實例已啓動
root 3893 0.0 0.0 110272 1380 pts/0 S 07:05 0:00 /bin/sh /usr/bin/mysqld_safe --socket=/tmp/mysql_3306.sock --port=3306 --pid-file=/var/lib/mysql3306/mysql3306.pid --datadir=/var/lib/mysql3306 --log=/var/lib/mysql3306/mysql3306.log --user=mysql
root 3900 0.0 0.0 110272 1384 pts/0 S 07:05 0:00 /bin/sh /usr/bin/mysqld_safe --socket=/tmp/mysql_3307.sock --port=3307 --pid-file=/var/lib/mysql3306/mysql3307.pid --datadir=/var/lib/mysql3307 --log=/var/lib/mysql3306/mysql3307.log --user=mysql
mysql 4041 0.0 0.6 185856 25544 pts/0 Sl 07:05 0:01 /usr/sbin/mysqld --basedir=/ --datadir=/var/lib/mysql3306 --user=mysql --log=/var/lib/mysql3306/mysql3306.log --log-error=/var/lib/mysql3306/dev02.err --pid-file=/var/lib/mysql3306/mysql3306.pid --socket=/tmp/mysql_3306.sock --port=3306
mysql 4047 0.0 0.7 186100 30996 pts/0 Sl 07:05 0:01 /usr/sbin/mysqld --basedir=/ --datadir=/var/lib/mysql3307 --user=mysql --log=/var/lib/mysql3306/mysql3307.log --log-error=/var/lib/mysql3307/dev02.err --pid-file=/var/lib/mysql3306/mysql3307.pid --socket=/tmp/mysql_3307.sock --port=3307
root 4226 0.0 0.0 107428 896 pts/0 S+ 07:52 0:00 grep mysqld
分別鏈接實例更改密碼:
[root@dev02 lib]#mysql -uroot -p -S /tmp/mysql_3306.sock 登陸第一個mysql,空密碼直接回車
mysql> set password=password("olymtech1");
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> grant all on *.* to user1@"%" Identified by "123456"; 建立普通用戶並賦予權限
[root@dev02 lib]#mysql -uroot -p -S /tmp/mysql_3307.sock 登陸第二個mysql,同上設置密碼
中止各實例(試了下未成功,只能手動kill掉):
/usr/bin/mysqld_multi --defaults-extra-file=/var/lib/my_multi.cnf stop 1
/usr/bin/mysqld_multi --defaults-extra-file=/var/lib/my_multi.cnf stop 2
啓動各實例:
/usr/bin/mysqld_multi --defaults-extra-file=/var/lib/my_multi.cnf start 1
/usr/bin/mysqld_multi --defaults-extra-file=/var/lib/my_multi.cnf start 2
補充多實例下mysql配置文件查找的順序:
1. 首先讀取/etc/my.cnf。
2. $datadir/my.cnf,在數據庫目錄下尋找此配置文件。
3. defaultfile=/path/my.cnf 一般寫在命令行上,mysqld_safe defaultfile=/tmp/my.cnf &等執行。
4,~/my.cnf 當前用戶下的配置文件。
參考文檔:
http://sharkyan.blog.51cto.com/536264/271144/
http://www.cnblogs.com/acpp/archive/2010/08/09/1795466.html
http://www.linuxidc.com/Linux/2012-11/73615.htm