1. 背景mysql
MySQL數據庫的集中化運維,能夠經過在一臺服務器上,部署運行多個MySQL服務進程,經過不一樣的socket監聽不一樣的服務端口來提供各自的服務。各個實例之間是相互獨立的,每一個實例的datadir, port, socket, pid都是不一樣的。linux
2. 多實例特色sql
* 有效利用服務器資源,當單個服務器資源有剩餘時,能夠充分利用剩餘的資源提供更多的服務。數據庫
* 資源互相搶佔問題,當某個服務實例服務併發很高時或者開啓慢查詢時,會消耗更多的內存、CPU、磁盤IO資源,致使服務器上的其餘實例提供服務的質量降低。緩存
3. 環境 [ 關閉SeLinux ]bash
[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 軟連接socket
[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. 總結
以需求驅動技術,技術自己沒有優略之分,只有業務之分。