mysql多實例介紹及配置 html
一、mysql多實例介紹 mysql
1.1 什麼是mysql多實例 sql
mysql多實例就是在一臺機器上開啓多個不一樣的服務端口(如:3306,3307),運行多個MySQL服務進程,經過不一樣的socket監聽不一樣的服務端口來提供各自的服務:; 數據庫
1.2 mysql多實例的特色有如下幾點 vim
1:有效利用服務器資源,當單個服務器資源有剩餘時,能夠充分利用剩餘的資源提供更多的服務。 服務器
2:節約服務器資源 併發
3:資源互相搶佔問題,當某個服務實例服務併發很高時或者開啓慢查詢時,會消耗更多的內存、CPU、磁盤IO資源,致使服務器上的其餘實例提供服務的質量降低; app
1.3 部署mysql多實例的兩種方式 ssh
第一種是使用多個配置文件啓動不一樣的進程來實現多實例,這種方式的優點邏輯簡單,配置簡單,缺點是管理起來不太方便; socket
第二種是經過官方自帶的mysqld_multi使用單獨的配置文件來實現多實例,這種方式定製每一個實例的配置不太方面,優勢是管理起來很方便,集中管理;
1.4 同一開發環境下安裝兩個數據庫,必須處理如下問題
配置文件安裝路徑不能相同
數據庫目錄不能相同
啓動腳本不能同名
端口不能相同
socket文件的生成路徑不能相同
二、mysqld_multi配置mysql多實例
在進行此操做前已經編譯安裝好了mysql,安裝位置在/application/mysql/下
2.1 初始化數據目錄
mkdir /usr/local/var -p
/application/mysql/scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/usr/local/var/mysql1 --user=mysql
/application/mysql/scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/usr/local/var/mysql2 --user=mysql
/application/mysql/scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/usr/local/var/mysql3 --user=mysql
/application/mysql/scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/usr/local/var/mysql4 --user=mysql
修改受權
chown -R mysql.mysql /usr/local/var/mysql*
2.2 配置多實例啓動腳本
cp /application/mysql/support-files/mysqld_multi.server /etc/init.d/
#修改basedir和bindir爲安裝路徑
basedir=/application/mysql
bindir=/application/mysql/bin
2.3 配置多實例數據庫配置文件
在/etc/目錄下建立建立文件/etc/mysqld_multi.cnf,內容以下
[mysqld_multi]
mysqld = /application/mysql/bin/mysqld_safe
mysqladmin = /application/mysql/bin/mysqladmin
#user = mysql
#password = my_password
[mysqld1]
socket = /usr/local/var/mysql1/mysql1.sock
port = 3306
pid-file = /usr/local/var/mysql1/mysql1.pid
datadir = /usr/local/var/mysql1
#language = /usr/local/mysql/share/mysql/english
user = mysql
[mysqld2]
socket = /usr/local/var/mysql2/mysql2.sock
port = 3307
pid-file = /usr/local/var/mysql2/mysql2.pid
datadir = /usr/local/var/mysql2
#language = /usr/local/mysql/share/mysql/english
user = mysql
[mysqld3]
socket = /usr/local/var/mysql3/mysql3.sock
port = 3308
pid-file = /usr/local/var/mysql3/mysql3.pid
datadir = /usr/local/var/mysql3
#language = /usr/local/mysql/share/mysql/english
user = mysql
[mysqld4]
socket = /usr/local/var/mysql4/mysql4.sock
port = 3309
pid-file = /usr/local/var/mysql4/mysql4.pid
datadir = /usr/local/var/mysql4
#language = /usr/local/mysql/share/mysql/english
user = mysql
2.4 啓動多實例數據庫
mysqld_multi --defaults-extra-file=/etc/mysqld_multi.cnf start
#啓動或中止具體某一個實例可在start、stop後面加上具體數據1,2,3等
mysqld_multi進行多實例管理
啓動所有實例:/usr/local/mysql/bin/mysqld_multi --defaults-extra-file=/etc/mysqld_multi.cnf start
查看所有實例狀態:/usr/local/mysql/bin/mysqld_multi report
啓動單個實例:/usr/local/mysql/bin/mysqld_multi --defaults-extra-file=/etc/mysqld_multi.cnf start 3306
中止單個實例:/usr/local/mysql/bin/mysqld_multi --defaults-extra-file=/etc/mysqld_multi.cnf stop 3306
查看單個實例狀態:/usr/local/mysql/bin/mysqld_multi --defaults-extra-file=/etc/mysqld_multi.cnf report 3306
檢查
[root@db02 ~]# netstat -lntup
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 5016/mysqld
tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 5026/mysqld
tcp 0 0 0.0.0.0:3308 0.0.0.0:* LISTEN 5036/mysqld
tcp 0 0 0.0.0.0:3309 0.0.0.0:* LISTEN 5042/mysqld
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1317/sshd
tcp 0 0 :::22 :::* LISTEN 1317/sshd
2.5 登陸相應數據庫
#進入端口爲3306的數據庫
mysql -uroot -p -h127.0.0.1 -P3306 ####密碼爲空
或者
mysql -S /usr/local/var/mysql1/mysql1.sock
三、多配置文件實現MySQL多實例
在進行此操做前已經編譯安裝好了mysql,安裝位置在/application/mysql/下
3.1 建立目錄和配置文件
mkdir -p /data/{3306,3307}/data
vim /data/3306/my.cnf
[client]
port = 3306
socket = /data/3306/mysql.sock
[mysql]
no-auto-rehash
[mysqld]
user = mysql
port = 3306
socket = /data/3306/mysql.sock
basedir = /application/mysql
datadir = /data/3306/data
pid-file = /data/3306/mysql.pid
log-bin = /data/3306/mysql-bin
relay-log = /data/3306/relay-bin
relay-log-info-file = /data/3306/relay-log.info
server-id = 1
[mysqld_safe]
log-error=/data/3306/mysql_3306.err
pid-file=/data/3306/mysqld.pid
3.2 建立啓動腳本
vim /data/3306/mysql
#!/bin/sh
#init
port=3306
CmdPath="/application/mysql/bin"
mysql_sock="/data/${port}/mysql.sock"
#startup function
function_start_mysql()
{
if [ ! -e "$mysql_sock" ];then
printf "Starting MySQL...\n"
/bin/sh ${CmdPath}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 > /dev/null &
else
printf "MySQL is running...\n"
exit
fi
}
#stop function
function_stop_mysql()
{
if [ ! -e "$mysql_sock" ];then
printf "MySQL is stopped...\n"
exit
else
printf "Stoping MySQL...\n"
${CmdPath}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S /data/${port}/mysql.sock shutdown
fi
}
#restart function
function_restart_mysql()
{
printf "Restarting MySQL...\n"
function_stop_mysql
sleep 2
function_start_mysql
}
case $1 in
start)
function_start_mysql
;;
stop)
function_stop_mysql
;;
restart)
function_restart_mysql
;;
*)
printf "Usage: /data/${port}/mysql {start|stop|restart}\n"
esac
3.4 增長3307實例
sed 's#3306#3307#g;s#server-id = 1#server-id = 3#g' /data/3306/my.cnf >/data/3307/my.cnf
sed 's#3306#3307#g' /data/3306/mysql >/data/3307/mysql
3.5 修改權限
chown -R mysql.mysql /data
chmod +x /data/{3306,3307}/mysql
3.6 初始化
/application/mysql/scripts/mysql_install_db --user=mysql --basedir=/application/mysql --datadir=/data/3306/data
/application/mysql/scripts/mysql_install_db --user=mysql --basedir=/application/mysql --datadir=/data/3307/data
3.7 啓動
/data/3306/mysql start
/data/3307/mysql start
啓動的時候報錯
[root@db01 ~]# 180707 16:30:54 mysqld_safe error: log-error set to '/data/3306/mysql_3306.err', however file don't exists. Create writable for user 'mysql'.
手動建立文件並受權,再次啓動成功
touch /data/3306/mysql_3306.err
chown mysql:mysql /data/3306/mysql_3306.err
檢查
[root@db01 ~]# netstat -lntup|grep mysql
tcp6 0 0 :::3306 :::* LISTEN 2023/mysqld
tcp6 0 0 :::3307 :::* LISTEN 2505/mysqld
3.8 初始化密碼登陸
mysqladmin -uroot -S /data/3306/mysql.sock password '123456'
mysqladmin -uroot -S /data/3307/mysql.sock password '123456'
參考來源:
http://www.javashuo.com/article/p-yojoeuat-o.html