一、什麼是MySQL多實例
簡單的說就是在一臺機器上開啓多個不同的服務端口(例如:3306、3307),運行多個mysql服務進程,這些服務進程通過不同的socket監聽不同的服務端口來提供各自的服務。
這些mysql多實例共用一套mysql安裝程序,使用不同(也可以相同)的my.cnf配置文件、啓動程序、數據文件。在提供服務時,多實例mysql在邏輯上看來是各自獨立的,多個實例是根據配置文件中配置的參數來獲取服務器相關硬件資源。
二、MySQL常見應用場景
由於公司業務訪問量不是很大,服務器的資源基本都是浪費的,這時候很適合使用多實例的應用,如果對SQL語句優化做的比較好,mysql多實例是一個很值得使用的技術,即使併發很大,合理分配系統資源,也不會有太大問題。
三、MySQL多實例常見配置方案
3.1 安裝MySQL數據庫
具體安裝方法,請參見 編譯方式安裝MySQL數據庫 。
3.2 配置多實例
3.2.1 創建多實例目錄
[root@mysql-multi ~]# mkdir -p /data/{3306,3307}/data [root@mysql-multi ~]# tree /data /data ├── 3306 │ └── data └── 3307 └── data
3.2.2 將數據目錄及臨時目錄授權mysql用戶(在安裝mysql之前已經創建mysql用戶組和用戶)
[root@mysql-multi ~]# chown -R mysql.mysql /data [root@mysql-multi ~]# chmod -R 1777 /tmp [root@mysql-multi ~]# ls -ld /data/{3306,3307}/data drwxr-xr-x. 2 mysql mysql 4096 1月 18 22:40 /data/3306/data drwxr-xr-x. 2 mysql mysql 4096 1月 18 22:40 /data/3307/data
3.2.3 創建配置文件my.cnf
3306端口 | 3307端口 |
[client] [mysql] [mysqld]
lower_case_table_names = 1 server-id = 1 innodb_additional_mem_pool_size = 4M
[mysqld_safe] | [client] [mysql] [mysqld]
lower_case_table_names = 1 server-id = 3 innodb_additional_mem_pool_size = 4M
[mysqld_safe] |
3.3 初始化數據庫表
初始化3306端口數據庫 [root@mysql-multi ~]# cd /application/mysql/scripts/ [root@mysql-multi scripts]# ./mysql_install_db --basedir=/application/mysql --datadir=/data/3306/data --user=mysql WARNING: The host 'mysql-multi' could not be looked up with resolveip. This probably means that your libc libraries are not 100 % compatible with this binary MySQL version. The MySQL daemon, mysqld, should work normally with the exception that host name resolving will not work. This means that you should use IP addresses instead of hostnames when specifying MySQL privileges ! Installing MySQL system tables... OK Filling help tables... OK ......此處省略 初始化3307端口數據庫 [root@mysql-multi scripts]# ./mysql_install_db --basedir=/application/mysql --datadir=/data/3307/data --user=mysql WARNING: The host 'mysql-multi' could not be looked up with resolveip. This probably means that your libc libraries are not 100 % compatible with this binary MySQL version. The MySQL daemon, mysqld, should work normally with the exception that host name resolving will not work. This means that you should use IP addresses instead of hostnames when specifying MySQL privileges ! Installing MySQL system tables... OK Filling help tables... OK ......此處省略
說明:一般情況下初始化數據庫表時日誌顯示兩個“OK”即表示初始化成功。
3.4 啓動數據庫多實例
[root@mysql-multi ~]# /data/3306/mysql start Starting MySQL... [root@mysql-multi ~]# /data/3307/mysql start Starting MySQL... [root@mysql-multi ~]# netstat -lntup|grep 330 tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 28220/mysqld tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 26267/mysqld
說明:“mysql”是自己寫的啓動腳本就不貼出來了,mysql啓動和停止的實質爲:
mysql數據庫啓動命令:
# /application/mysql/bin/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 > /dev/null &
mysql數據庫停止命令:
# /application/mysql/bin/mysqladmin -u${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdown
3.5 配置環境變量
[root@mysql-multi ~]# echo "export PATH=/application/mysql/bin:$PATH" >> /etc/profile [root@mysql-multi ~]# tail -1 /etc/profile export PATH=/application/mysql/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin [root@mysql-multi ~]# . /etc/profile
3.6 登錄數據庫
登錄3306數據庫
[root@mysql-multi ~]# mysql -S /data/3306/mysql.sock Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.5.32-log Source distribution Copyright (c) 2000, 2013, 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>
登錄3307數據庫
[root@mysql-multi ~]# mysql -S /data/3307/mysql.sock Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.5.32 Source distribution Copyright (c) 2000, 2013, 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數據庫在初始登錄未設置登錄用戶名和密碼,可以在登錄數據庫之後增加賬戶信息。至此配置MySQL數據庫多實例完成。