您好,登錄后才能下訂單哦!
一、MySQL多實(shí)例簡(jiǎn)介
MySQL多實(shí)例,簡(jiǎn)單地說,就是在一臺(tái)服務(wù)器上同時(shí)開啟多個(gè)不同的服務(wù)端口(如:3306、3307),同時(shí)運(yùn)行多個(gè)MySQL服務(wù)進(jìn)程,這些服務(wù)進(jìn)程通過不同的socket監(jiān)聽來自不同的端口來提供服務(wù);
多實(shí)例不僅節(jié)省物理主機(jī)成本,還有效提升了單臺(tái)物理主機(jī)的CPU、磁盤I/O使用效率,而且還可以在多實(shí)例之間做部署數(shù)據(jù)庫HA方案。
隨著實(shí)例數(shù)量的增加,就面臨統(tǒng)一管理問題,這樣我們就需要用MySQL自帶的管理程序 mysqld_multi 來進(jìn)行管理...
二、MySQL啟動(dòng)流程
mysqld_multi #多實(shí)例管理程序
mysqld #MySQL最主要的啟動(dòng)方式,里面有很多參數(shù);現(xiàn)在使用多實(shí)例就需要用新的mysql_safe 來啟動(dòng)mysql
mysql_safe #實(shí)則還是調(diào)用mysqld,并且會(huì)讀取mysqld中的my.cnf配置參數(shù)來啟動(dòng)mysql,mysql_safe本身也有很多參數(shù),但是這些參數(shù)會(huì)優(yōu)先于my.cnf
my.cnf #mysql的配置文件
my.sock #mysql創(chuàng)建的sock文件,開啟、停止、登陸和管理mysql都是通過這個(gè)接口文件
三、接下來基于mysql5.5.52版本,安裝方法請(qǐng)看MySQL5.5.52編譯安裝,利用mysqld_multi配置一個(gè)多實(shí)例
1、停止單實(shí)例mysql數(shù)據(jù)庫
[root@db01 ~]# /etc/init.d/mysqld stop Shutting down MySQL. SUCCESS!
2、禁止開機(jī)自啟動(dòng)
[root@db01 ~]# chkconfig mysqld off [root@db01 ~]# chkconfig --list mysqld mysqld 0:關(guān)閉 1:關(guān)閉 2:關(guān)閉 3:關(guān)閉 4:關(guān)閉 5:關(guān)閉6:關(guān)閉
3、創(chuàng)建多實(shí)例根目錄/data/目錄
[root@db01 ~]# mkdir -p /data/{3306,3307}/data
4、拷貝mysqld_multi程序文件
[root@db01 ~]# cp /application/mysql/support-files/mysqld_multi.server /etc/init.d/mysqld_multi.server
1)修改mysqld_multi.server路徑配置
[root@db01 ~]# sed -i 's#basedir=/usr/local/mysql#basedir=/application/mysql#g' /etc/init.d/mysqld_multi.server [root@db01 ~]# sed -i 's#bindir=/usr/local/mysql/bin#bindir=/application/mysql/bin#g' /etc/init.d/mysqld_multi.server
2)添加mysqld_multi用到的/etc/mysqld_multi.cnf配置文件
#這個(gè)模板文件可以用命令mysqld_multi --example導(dǎo)出來
[root@db01 ~]# vim /etc/mysqld_multi.cnf [mysqld_multi] mysqld = /application/mysql/bin/mysqld_safe mysqladmin = /application/mysql/bin/mysqladmin #user = multi_admin #password = my_password [mysqld1] socket = /data/3306/mysql.sock port = 3306 pid-file = /data/3306/mysql.pid datadir = /data/3306/data #language = /application/mysql/share/mysql/english user = mysql [mysqld2] socket = /data/3307/mysql.sock port = 3307 pid-file = /data/3307/mysql.pid datadir = /data/3307/data #language = /application/mysql/share/mysql/english user = mysql
5、配置MySQL多實(shí)例的文件權(quán)限
通過下面的命令授權(quán)mysql用戶和用戶組管理整個(gè)多實(shí)例的根目錄/data
[root@db01 ~]# chown -R mysql.mysql /data
6、初始化MySQL多實(shí)例的數(shù)據(jù)庫文件
(1)初始化MySQL數(shù)據(jù)庫
cd /application/mysql/scripts/ <==注意和MySQL5.1的路徑不同,MySQL5.1不在MySQL bin路徑下了
3306實(shí)例
/application/mysql/scripts/mysql_install_db \
--basedir=/application/mysql \
--datadir=/data/3306/data \
--user=mysql
3307實(shí)例
/application/mysql/scripts/mysql_install_db \
--basedir=/application/mysql \
--datadir=/data/3307/data \
--user=mysql
提示:--basedir=/application/mysql為MySQL的安裝路徑,--datadir為不同的實(shí)例數(shù)據(jù)目錄
操作過程:
[root@db01 ~]# cd /application/mysql/scripts/ 3306實(shí)例 [root@db01 scripts]# /application/mysql/scripts/mysql_install_db \ > --basedir=/application/mysql \ > --datadir=/data/3306/data \ > --user=mysql WARNING: The host 'db01' 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... 161117 14:14:14 [Note] /application/mysql/bin/mysqld (mysqld 5.5.52) starting as process 46676 ... OK Filling help tables... 161117 14:14:15 [Note] /application/mysql/bin/mysqld (mysqld 5.5.52) starting as process 46683 ... OK 如果有兩個(gè)ok,就表示初始化成功 3307實(shí)例 [root@db01 scripts]# /application/mysql/scripts/mysql_install_db \ > --basedir=/application/mysql \ > --datadir=/data/3307/data \ > --user=mysql Installing MySQL system tables... 161117 14:18:20 [Note] /application/mysql/bin/mysqld (mysqld 5.5.52) starting as process 46733 ... OK Filling help tables... 161117 14:18:21 [Note] /application/mysql/bin/mysqld (mysqld 5.5.52) starting as process 46740 ... OK 如果有兩個(gè)ok,就表示初始化成功
7、啟動(dòng)多實(shí)例:
1)查看數(shù)據(jù)庫狀態(tài)
mysqld_multi --defaults-extra-file=/etc/mysqld_multi.cnf report [root@db01 ~]# mysqld_multi --defaults-extra-file=/etc/mysqld_multi.cnf report Reporting MySQL servers MySQL server from group: mysqld1 is not running MySQL server from group: mysqld2 is not running
2)啟動(dòng)數(shù)據(jù)庫
[root@db01 ~]# mysqld_multi --defaults-extra-file=/etc/mysqld_multi.cnf start 1,2 [root@db01 ~]# mysqld_multi --defaults-extra-file=/etc/mysqld_multi.cnf report Reporting MySQL servers MySQL server from group: mysqld1 is running MySQL server from group: mysqld2 is running
3)查看端口
[root@db01 ~]# ss -nlutp|grep 330 tcp LISTEN 0 50 *:3306 *:* users:(("mysqld",47045,10)) tcp LISTEN 0 50 *:3307 *:* users:(("mysqld",47041,10))
8、停止數(shù)據(jù)庫
[root@db01 ~]# mysqld_multi --defaults-extra-file=/etc/mysqld_multi.cnf stop 1,2 [root@db01 ~]# mysqld_multi --defaults-extra-file=/etc/mysqld_multi.cnf report Reporting MySQL servers MySQL server from group: mysqld1 is not running MySQL server from group: mysqld2 is not running
9、登陸數(shù)據(jù)庫
1)啟動(dòng)數(shù)據(jù)庫
[root@db01 ~]# mysqld_multi --defaults-extra-file=/etc/mysqld_multi.cnf start 1,2
2)登錄數(shù)據(jù)庫
方法一:指定端口和主機(jī)IP,適合遠(yuǎn)程連接
mysql -uroot -h227.0.0.1 -P3306
方法二:指定socket登陸,適合在本機(jī)連接
mysql -S /data/3307/mysql.sock
操作演示
方法一:指定端口和主機(jī)IP,適合遠(yuǎn)程連接
[root@db01 ~]# mysql -uroot -h227.0.0.1 -P3306 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.5.52 Source distribution Copyright (c) 2000, 2016, 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>
方法二:指定socket登陸,適合在本機(jī)連接
[root@db01 ~]# 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.52 Source distribution Copyright (c) 2000, 2016, 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多實(shí)例就配置完成啦O(∩_∩)O~~?。?!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。