MySQL--------多版本多實例混合部署

1. 背景

  * MySQL數據庫的集中化運維,可以通過在一臺服務器上,部署運行多個MySQL服務進程,通過不同的socket監聽不同的服務端口來提供各自的服務。各個實例之間是相互獨立的,每個實例的datadir, port, socket, pid都是不同的。

  * 網上多實例一般通過實例版本相同實現,此次以不同版本來實現多實例部署(5.5、5.6、5.7)。


2. 多實例特點

  * 有效利用服務器資源,當單個服務器資源有剩餘時,可以充分利用剩餘的資源提供更多的服務。

  * 資源互相搶佔問題,當某個服務實例服務併發很高時或者開啓慢查詢時,會消耗更多的內存、CPU、磁盤IO資源,導致服務器上的其他實例提供服務的質量下降。

wKioL1lPymPxK5ulAADAhFfCdKg045.jpg


3. 環境 [ 關閉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 二進制包準備

  * 下載官方5.5二進制安裝包

[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二進制安裝包

[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二進制安裝包

[root@MySQL ~]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.19-linux-glibc2.12-x86_64.tar


5. mysql 版本初始化並統一修改密碼

  * 創建 MySQL 用戶

[root@MySQL ~]# useradd -r -s /sbin/nologin mysql


  * 創建MySQL數據目錄

[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 數據目錄所屬用戶與所屬組

[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 初始化 [ 注意初始化提示的隨機密碼 ]

[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]
# 設置登陸用戶
user = root
# 設置登陸用戶密碼
password = 123

[mysqld]
# mysql 運行用戶
user = mysql
# 設置 mysql 監聽 IP 地址
bind_address = 0.0.0.0
# 關閉 DNS 反解析
skip-name-resolve = 0
# 關閉監聽
performance_schema = off
# 設置buffer pool 大小
innodb_buffer_pool_size = 32M
# 設置錯誤日誌文件名
log_error = error.log

[mysqld_multi]
# 設置multi 日誌
log = /tmp/mysql_multi.log

[mysqld5]
# 設置實例所在目錄
basedir = /usr/local/mysql-5.5.57-linux-glibc2.12-x86_64
# 設置mysql 運行程序所在路徑
mysqld = /usr/local/mysql-5.5.57-linux-glibc2.12-x86_64/bin/mysqld
# 設置mysql 管理運行程序所在路徑
mysqladmin = /usr/local/mysql-5.5.57-linux-glibc2.12-x86_64/bin/mysqladmin
# 設置實例數據目錄 -- 多實例中一定要不同
datadir = /data/mysql_data_5
# 設置socket 文件路徑 -- 多實例中一定要不同
socket = /tmp/mysql.sock5
# 設置實例監聽端口 -- 多實例中一定要不同
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


  * 從隨意版本二進制包中support-files目錄下複製mysqld_multi.server啓動腳本至 /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


  * 隨意版本創始軟鏈接,並設置環境變量

[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. 測試

   * 查看多實例狀態

[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


  * 啓動多實例 [ 需等候幾秒 ]

[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


  * 分別連接實例

[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


  * 停止多實例

[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. 總結


以需求驅動技術,技術本身沒有優略之分,只有業務之分。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章