您好,登錄后才能下訂單哦!
1. 背景
MySQL數據庫的集中化運維,可以通過在一臺服務器上,部署運行多個MySQL服務進程,通過不同的socket監聽不同的服務端口來提供各自的服務。各個實例之間是相互獨立的,每個實例的datadir, port, socket, pid都是不同的。
2. 多實例特點
* 有效利用服務器資源,當單個服務器資源有剩余時,可以充分利用剩余的資源提供更多的服務。
* 資源互相搶占問題,當某個服務實例服務并發很高時或者開啟慢查詢時,會消耗更多的內存、CPU、磁盤IO資源,導致服務器上的其他實例提供服務的質量下降。
3. 環境 [ 關閉SeLinux ]
[root@MySQL ~]# cat /etc/redhat-release CentOS release 6.9 (Final) [root@MySQL ~]# uname -r 2.6.32-696.3.2.el6.x86_64 [root@MySQL ~]# getenforce Disabled
4. 部署 [ 4個實例 ]
* 下載 MySQL 5.7 二制包 [ 推薦官方下載 ] 此下載版本大于5.7.5
[root@MySQL ~]# wget wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz
* 解壓 MySQL 5.7 二進制包到指定目錄
[root@MySQL ~]# tar zxvf mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz -C /usr/local/
* 創建 MySQL 軟鏈接
[root@MySQL ~]# ln -s /usr/local/mysql-5.7.18-linux-glibc2.5-x86_64 /usr/local/mysql
* 創建 MySQL 用戶
[root@MySQL ~]# useradd -r -s /sbin/nologin mysql
* 在 MySQL 二進制包目錄中創建 mysql-files 目錄 [MySQL 數據導入/導出數據專放目錄]
[root@MySQL ~]# mkdir -v /usr/local/mysql/mysql-files mkdir: created directory `/usr/local/mysql/mysql-files'
* 創建多實例數據目錄
[root@MySQL ~]# mkdir -vp /data/mysql_data{1..4} mkdir: created directory `/data' mkdir: created directory `/data/mysql_data1' mkdir: created directory `/data/mysql_data2' mkdir: created directory `/data/mysql_data3' mkdir: created directory `/data/mysql_data4'
* 修改 MySQL 二進制包目錄的所屬用戶與所屬組
[root@MySQL ~]# chown root.mysql -R /usr/local/mysql-5.7.18-linux-glibc2.5-x86_64
* 修改 MySQL 多實例數據目錄與 數據導入/導出專放目錄的所屬用戶與所屬組
[root@MySQL ~]# chown mysql.mysql -R /usr/local/mysql/mysql-files /data/mysql_data{1..4}
* 配置 MySQL 配置文件 /etc/my.cnf
[mysqld_multi] mysqld = /usr/local/mysql/bin/mysqld mysqladmin = /usr/local/mysql/bin/mysqladmin log = /tmp/mysql_multi.log [mysqld1] # 設置數據目錄 [多實例中一定要不同] datadir = /data/mysql_data1 # 設置sock存放文件名 [多實例中一定要不同] socket = /tmp/mysql.sock1 # 設置監聽開放端口 [多實例中一定要不同] port = 3306 # 設置運行用戶 user = mysql # 關閉監控 performance_schema = off # 設置innodb 緩存大小 innodb_buffer_pool_size = 32M # 設置監聽IP地址 bind_address = 0.0.0.0 # 關閉DNS 反向解析 skip-name-resolve = 0 [mysqld2] datadir = /data/mysql_data2 socket = /tmp/mysql.sock2 port = 3307 user = mysql performance_schema = off innodb_buffer_pool_size = 32M bind_address = 0.0.0.0 skip-name-resolve = 0 [mysqld3] datadir = /data/mysql_data3 socket = /tmp/mysql.sock3 port = 3308 user = mysql performance_schema = off innodb_buffer_pool_size = 32M bind_address = 0.0.0.0 skip-name-resolve = 0 [mysqld4] datadir = /data/mysql_data4 socket = /tmp/mysql.sock4 port = 3309 user = mysql performance_schema = off innodb_buffer_pool_size = 32M bind_address = 0.0.0.0 skip-name-resolve = 0
* 初始化各個實例 [ 初始化完成后會自帶隨機密碼在輸出日志中 ]
[root@MySQL ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql_data1 [root@MySQL ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql_data2 [root@MySQL ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql_data3 [root@MySQL ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql_data4
* 各實例開啟 SSL 連接
[root@MySQL ~]# /usr/local/mysql/bin/mysql_ssl_rsa_setup --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql_data1 [root@MySQL ~]# /usr/local/mysql/bin/mysql_ssl_rsa_setup --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql_data2 [root@MySQL ~]# /usr/local/mysql/bin/mysql_ssl_rsa_setup --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql_data3 [root@MySQL ~]# /usr/local/mysql/bin/mysql_ssl_rsa_setup --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql_data4
* 復制多實例腳本到服務管理目錄下 [ /etc/init.d/ ]
[root@MySQL ~]# cp /usr/local/mysql/support-files/mysqld_multi.server /etc/init.d/mysqld_multi
* 添加腳本執行權限
[root@MySQL ~]# chmod +x /etc/init.d/mysqld_multi
* 添加進service服務管理
[root@MySQL ~]# chkconfig --add mysqld_multi
5. 啟動測試
* 查個多實例狀態
[root@MySQL ~]# /etc/init.d/mysqld_multi report Reporting MySQL servers MySQL server from group: mysqld1 is not running MySQL server from group: mysqld2 is not running MySQL server from group: mysqld3 is not running MySQL server from group: mysqld4 is not running
* 啟動多實例
[root@MySQL ~]# /etc/init.d/mysqld_multi start
* 查看多實例狀態
Reporting MySQL servers MySQL server from group: mysqld1 is running MySQL server from group: mysqld2 is running MySQL server from group: mysqld3 is running MySQL server from group: mysqld4 is running
* 查看實例監聽端口
[root@MySQL ~]# netstat -lntp | grep mysqld tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2673/mysqld tcp 0 0 0.0.0.0:3307 0.0.0.0:* LISTEN 2676/mysqld tcp 0 0 0.0.0.0:3308 0.0.0.0:* LISTEN 2679/mysqld tcp 0 0 0.0.0.0:3309 0.0.0.0:* LISTEN 2682/mysqld
6. 連接測試
* 實例1
[root@MySQL ~]# /usr/local/mysql/bin/mysql -S /tmp/mysql.sock1 -p'z+Ilo*>s:3kw' 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 6 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 = '123456'; Query OK, 0 rows affected (0.00 sec)
* 實例2
[root@MySQL ~]# /usr/local/mysql/bin/mysql -S /tmp/mysql.sock2 -p'b*AHUrTgu1rl' 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 7 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 = '123456'; Query OK, 0 rows affected (0.00 sec)
7. 總結
以需求驅動技術,技術本身沒有優略之分,只有業務之分。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。