您好,登錄后才能下訂單哦!
1. 背景
* MySQL數(shù)據(jù)庫(kù)的集中化運(yùn)維,可以通過(guò)在一臺(tái)服務(wù)器上,部署運(yùn)行多個(gè)MySQL服務(wù)進(jìn)程,通過(guò)不同的socket監(jiān)聽(tīng)不同的服務(wù)端口來(lái)提供各自的服務(wù)。各個(gè)實(shí)例之間是相互獨(dú)立的,每個(gè)實(shí)例的datadir, port, socket, pid都是不同的。
* 網(wǎng)上多實(shí)例一般通過(guò)實(shí)例版本相同實(shí)現(xiàn),此次以不同版本來(lái)實(shí)現(xiàn)多實(shí)例部署(5.5、5.6、5.7)。
2. 多實(shí)例特點(diǎn)
* 有效利用服務(wù)器資源,當(dāng)單個(gè)服務(wù)器資源有剩余時(shí),可以充分利用剩余的資源提供更多的服務(wù)。
* 資源互相搶占問(wèn)題,當(dāng)某個(gè)服務(wù)實(shí)例服務(wù)并發(fā)很高時(shí)或者開(kāi)啟慢查詢時(shí),會(huì)消耗更多的內(nèi)存、CPU、磁盤(pán)IO資源,導(dǎo)致服務(wù)器上的其他實(shí)例提供服務(wù)的質(zhì)量下降。
3. 環(huán)境 [ 關(guān)閉SeLinux ]
[root@MySQL ~]# cat /etc/redhat-release CentOS release 6.9 (Final) [root@MySQL ~]# uname -r 2.6.32-504.el6.x86_64 [root@MySQL ~]# getenforce Disabled
4. MySQL 二進(jìn)制包準(zhǔn)備
* 下載官方5.5二進(jìn)制安裝包
[root@MySQL ~]# wget https://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.57-linux-glibc2.12-x86_64.tar.gz
* 下載官方5.6二進(jìn)制安裝包
[root@MySQL ~]# wget https://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.37-linux-glibc2.12-x86_64.tar.gz
* 下載官方5.7二進(jìn)制安裝包
[root@MySQL ~]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.19-linux-glibc2.12-x86_64.tar
5. mysql 版本初始化并統(tǒng)一修改密碼
* 創(chuàng)建 MySQL 用戶
[root@MySQL ~]# useradd -r -s /sbin/nologin mysql
* 創(chuàng)建MySQL數(shù)據(jù)目錄
[root@MySQL ~]# mkdir -vp /data/mysql_data_{5..7} mkdir: created directory `/data' mkdir: created directory `/data/mysql_data_5' mkdir: created directory `/data/mysql_data_6' mkdir: created directory `/data/mysql_data_7'
* 修改MySQL 數(shù)據(jù)目錄所屬用戶與所屬組
[root@MySQL ~]# chown mysql.mysql -R /data/mysql_data_*
* 解壓MySQL 各版本至 /usr/local 目錄
[root@MySQL ~]# tar zxf mysql-5.5.57-linux-glibc2.12-x86_64.tar.gz -C /usr/local/ [root@MySQL ~]# tar zxf mysql-5.6.37-linux-glibc2.12-x86_64.tar.gz -C /usr/local/ [root@MySQL ~]# tar xf mysql-5.7.19-linux-glibc2.12-x86_64.tar -C /usr/local/
* MySQL 5.5 初始化
[root@MySQL ~]# chown mysql.mysql -R /usr/local/mysql-5.5.57-linux-glibc2.12-x86_64 [root@MySQL ~]# /usr/local/mysql-5.5.57-linux-glibc2.12-x86_64/scripts/mysql_install_db --user=mysql --datadir=/data/mysql_data_5 --basedir=/usr/local/mysql-5.5.57-linux-glibc2.12-x86_64
* MySQL 5.5 修改密碼
[root@MySQL ~]# /usr/local/mysql-5.5.57-linux-glibc2.12-x86_64/bin/mysqld_safe --datadir=/data/mysql_data_5 & [root@MySQL ~]# /usr/local/mysql-5.5.57-linux-glibc2.12-x86_64/bin/mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.5.57 MySQL Community Server (GPL) Copyright (c) 2000, 2017, 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> set password = password('123'); Query OK, 0 rows affected (0.00 sec) mysql> quit Bye [root@MySQL ~]# killall mysqld
* MySQL 5.6 初始化
[root@MySQL ~]# chown mysql.mysql -R /usr/local/mysql-5.6.37-linux-glibc2.12-x86_64 [root@MySQL ~]# /usr/local/mysql-5.6.37-linux-glibc2.12-x86_64/scripts/mysql_install_db --user=mysql --datadir=/data/mysql_data_6 --basedir=/usr/local/mysql-5.6.37-linux-glibc2.12-x86_64
* MySQL 5.6修改密碼
[root@MySQL ~]# /usr/local/mysql-5.6.37-linux-glibc2.12-x86_64/bin/mysqld_safe --datadir=/data/mysql_data_6 & [root@MySQL ~]# /usr/local/mysql-5.6.37-linux-glibc2.12-x86_64/bin/mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.6.37 MySQL Community Server (GPL) Copyright (c) 2000, 2017, 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> set password = password('123'); Query OK, 0 rows affected (0.00 sec) mysql> quit Bye [root@MySQL ~]# killall mysqld
* MySQL 5.7 初始化 [ 注意初始化提示的隨機(jī)密碼 ]
[root@MySQL ~]# mkdir /usr/local/mysql-5.7.19-linux-glibc2.12-x86_64/mysql-files [root@MySQL ~]# chown root.mysql -R /usr/local/mysql-5.7.19-linux-glibc2.12-x86_64 [root@MySQL ~]# chown mysql.mysql -R /data/mysql_data_7 /usr/local/mysql-5.7.19-linux-glibc2.12-x86_64/mysql-files [root@MySQL ~]# /usr/local/mysql-5.7.19-linux-glibc2.12-x86_64/bin/mysqld --initialize --user=mysql --datadir=/data/mysql_data_7 --basedir=/usr/local/mysql-5.7.19-linux-glibc2.12-x86_64
* MySQL 5.7修改密碼
[root@MySQL ~]# /usr/local/mysql-5.7.19-linux-glibc2.12-x86_64/bin/mysqld_safe --datadir=/data/mysql_data_7 & [root@MySQL ~]# /usr/local/mysql-5.7.19-linux-glibc2.12-x86_64/bin/mysql -p'INoGk(hoj9>/' mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.7.18 Copyright (c) 2000, 2017, 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> set password = '123'; Query OK, 0 rows affected (0.00 sec) mysql> quit Bye [root@MySQL ~]# killall mysqld
6. 多版本部署
* 編輯/etc/my.cnf
[client] # 設(shè)置登陸用戶 user = root # 設(shè)置登陸用戶密碼 password = 123 [mysqld] # mysql 運(yùn)行用戶 user = mysql # 設(shè)置 mysql 監(jiān)聽(tīng) IP 地址 bind_address = 0.0.0.0 # 關(guān)閉 DNS 反解析 skip-name-resolve = 0 # 關(guān)閉監(jiān)聽(tīng) performance_schema = off # 設(shè)置buffer pool 大小 innodb_buffer_pool_size = 32M # 設(shè)置錯(cuò)誤日志文件名 log_error = error.log [mysqld_multi] # 設(shè)置multi 日志 log = /tmp/mysql_multi.log [mysqld5] # 設(shè)置實(shí)例所在目錄 basedir = /usr/local/mysql-5.5.57-linux-glibc2.12-x86_64 # 設(shè)置mysql 運(yùn)行程序所在路徑 mysqld = /usr/local/mysql-5.5.57-linux-glibc2.12-x86_64/bin/mysqld # 設(shè)置mysql 管理運(yùn)行程序所在路徑 mysqladmin = /usr/local/mysql-5.5.57-linux-glibc2.12-x86_64/bin/mysqladmin # 設(shè)置實(shí)例數(shù)據(jù)目錄 -- 多實(shí)例中一定要不同 datadir = /data/mysql_data_5 # 設(shè)置socket 文件路徑 -- 多實(shí)例中一定要不同 socket = /tmp/mysql.sock5 # 設(shè)置實(shí)例監(jiān)聽(tīng)端口 -- 多實(shí)例中一定要不同 port = 3305 [mysqld6] basedir = /usr/local/mysql-5.6.37-linux-glibc2.12-x86_64 mysqld = /usr/local/mysql-5.6.37-linux-glibc2.12-x86_64/bin/mysqld mysqladmin = /usr/local/mysql-5.6.37-linux-glibc2.12-x86_64/bin/mysqladmin datadir = /data/mysql_data_6 socket = /tmp/mysql.sock6 port = 3306 [mysqld7] basedir = /usr/local/mysql-5.7.19-linux-glibc2.12-x86_64 datadir = /data/mysql_data_7 socket = /tmp/mysql.sock7 port = 3307
* 從隨意版本二進(jìn)制包中support-files目錄下復(fù)制mysqld_multi.server啟動(dòng)腳本至 /etc/init.d/
[root@MySQL ~]# cp /usr/local/mysql-5.7.19-linux-glibc2.12-x86_64/support-files/mysqld_multi.server /etc/init.d/mysqld_multi [root@MySQL ~]# chmod +x /etc/init.d/mysqld_multi
* 隨意版本創(chuàng)始軟鏈接,并設(shè)置環(huán)境變量
[root@MySQL ~]# ln -s /usr/local/mysql-5.7.19-linux-glibc2.12-x86_64 /usr/local/mysql [root@MySQL ~]# export PATH=/usr/local/mysql/bin:$PATH
7. 測(cè)試
* 查看多實(shí)例狀態(tài)
[root@MySQL ~]# /etc/init.d/mysqld_multi report Reporting MySQL servers MySQL server from group: mysqld5 is not running MySQL server from group: mysqld6 is not running MySQL server from group: mysqld7 is not running
* 啟動(dòng)多實(shí)例 [ 需等候幾秒 ]
[root@MySQL ~]# /etc/init.d/mysqld_multi start [root@MySQL ~]# /etc/init.d/mysqld_multi report Reporting MySQL servers MySQL server from group: mysqld5 is running MySQL server from group: mysqld6 is running MySQL server from group: mysqld7 is running [root@MySQL ~]# netstat -lntp | grep mysqld tcp 0 0 0.0.0.0:3305 0.0.0.0:* LISTEN 43750/mysqld tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 43753/mysqld tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 43756/mysqld
* 分別連接實(shí)例
[root@MySQL ~]# mysql -S /tmp/mysql.sock5 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.7.18 MySQL Community Server (GPL) Copyright (c) 2000, 2017, 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> quit Bye [root@MySQL ~]# mysql -S /tmp/mysql.sock6 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.7.18 MySQL Community Server (GPL) Copyright (c) 2000, 2017, 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> quit Bye [root@MySQL ~]# mysql -S /tmp/mysql.sock7 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.7.18 MySQL Community Server (GPL) Copyright (c) 2000, 2017, 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> quit Bye
* 停止多實(shí)例
[root@MySQL ~]# /etc/init.d/mysqld_multi stop [root@MySQL ~]# /etc/init.d/mysqld_multi report Reporting MySQL servers MySQL server from group: mysqld5 is not running MySQL server from group: mysqld6 is not running MySQL server from group: mysqld7 is not running
8. 總結(jié)
以需求驅(qū)動(dòng)技術(shù),技術(shù)本身沒(méi)有優(yōu)略之分,只有業(yè)務(wù)之分。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。