mysql可以以多實例的方式,實現一台服務器,運行在不同端口不同數據文件的mysql,它們是相互獨立的。
1、關閉原有的默認端口3306的mysql:service mysqd stop
2、拷貝或創建數據文件
#拷貝現有的mysql數據庫文件 #我的在/var/lib/mysql,拷貝一份至mysql_3307文件夾 [root@test-206 ~]# cp -r /var/lib/mysql /var/lib/mysql_3307
#創建一個新的空數據庫 [root@test-206 ~]# mkdir /var/lib/mysql_3307 [root@test-206 ~]# mysql_install_db --datadir=/var/lib/mysql_3307 --user=mysql
3、給數據文件賦予mysql用戶與用戶組
[root@test-206 ~]# chown -R mysql.mysql /var/lib/mysql_3307
4、創建multi的配置cnf文件,用於啟動這個mysql實例(如3307)載入執行
[root@test-206 ~]# touch /usr/local/my_multi.cnf
文件中寫入你想要的配置,如下為典型配置
[mysqld_multi] mysqld = /usr/bin/mysqld_safe mysqladmin = /usr/bin/mysqladmin user = root #用於登陸和關閉此服務 password = 123456 #同上 [mysqld3307] socket = /tmp/mysql_3307.sock port = 3307 pid-file = /var/lib/mysql_3307/3307.pid datadir = /var/lib/mysql_3307/ log = /var/lib/mysql_3307/3307.log character-set-server = utf8 user = mysql
5、啟動你的多實例
[root@test-206 ~]# mysqld_multi --defaults-extra-file=/usr/local/my_multi.cnf start 3307
6、檢查是否啟動成功
[root@test-206 ~]# netstat -ntlp tcp 0 0 :::3306 :::* LISTEN 3919/mysqld tcp 0 0 :::3307 :::* LISTEN 15027/mysqld
如果沒有發現你要的端口號mysql實例,可以檢查下/var/lib/mysql_3307/3307.log文件,排除問題
7、設置新的密碼
[root@test-206 ~]# mysqladmin -uroot -S /tmp/mysql_3307.sock password 123456
8、登入你的新實例
[root@test-206 ~]# mysql -uroot -S /tmp/mysql_3307.sock -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.5.20-log Distributed by The IUS Community Project Copyright (c) 2000, 2011, 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>
再次確認下,你的這個實例,是不是用的mysql_3307這個文件夾的數據
mysql> show variables like '%datadir%'; +---------------+----------------------+ | Variable_name | Value | +---------------+----------------------+ | datadir | /var/lib/mysql_3307/ | +---------------+----------------------+ 1 row in set (0.00 sec) mysql>
恩,沒有錯!最后,搞搞權限、用戶之類。收工!
#查用戶 mysql> select user,host from mysql.user; +------+-----------+ | user | host | +------+-----------+ | root | 127.0.0.1 | | root | ::1 | | | localhost | | root | localhost | | | test-206 | | root | test-206 | +------+-----------+ 6 rows in set (0.00 sec) #設權限 mysql> grant all on *.* to root@'%' identified by 'root' with grant option; Query OK, 0 rows affected (0.00 sec) ##查權限 mysql> show grants for root; ##創用戶 mysql> grant select on *.* to backup@'%' identified by 'backup'; Query OK, 0 rows affected (0.00 sec)
