1. 程式人生 > >MySQL讀寫分離和多例項詳解

MySQL讀寫分離和多例項詳解

mysql讀寫分離的優缺點
資料庫讀寫分離的好處:
– 減輕單臺伺服器的壓力,增加併發量,提高了硬體利用率

資料庫讀寫分離的缺點:
– 單點故障
– 當訪問量大時,排程器的效能會成為瓶錦

讀寫分離環境: 51為主 52為從 57為排程器

1 部署Mysql主從同步結構,一主一從
2 部署192.168.4.57 作為讀寫分離伺服器
2.1 裝包
2.2 配置
2.3 根據配置檔案設定新增對應的使用者
2.4 啟動讀寫分離服務
3 客戶端驗證讀寫分離服務的配置

mysql中介軟體:架設在資料庫和客戶端之間的軟體
mycat : mysql叢集,mysql資料分片,mysql物件分離
maxscale
mysql-proxy
這三個軟體都可以做mysql資料庫讀寫分離。

一 部署mysql代理伺服器(maxscale)
1 安裝軟體包(中介軟體)

[[email protected] ~]# rpm -ivh maxscale-2.1.2-1.rhel.7.x86_64.rpm

2 修改maxscale配置檔案

 [[email protected] ~]# vim /etc/maxscale.cnf.template 

配置項詳解:

 [maxscale]          配置項名
 threads=auto       開啟的執行緒數,根據cpu的核數設定

 18 [server1]        資料庫伺服器的Ip地址和埠號 
 19 type=server           
 20 address=192.168.4.51             指定master伺服器ip 51
 21 port=3306
 22 protocol=MySQLBackend
 
 24 [server2]                         定義資料庫伺服器
 25 type=server
 26 address=192.168.4.52               指定slave伺服器ip  52
 27 port=3306



28 protocol=MySQLBackend

 35 [MySQL Monitor]            監控資訊 定義要監視的資料庫伺服器
 36 type=monitor                 型別為監控
 37 module=mysqlmon         指定模組
 38 servers=server1, server2    具體監視的伺服器  
 39 user=scalemon           
 40 passwd=123456
 41 monitor_interval=10000  	#監控心跳為1秒
detect_stale_master=true #即使從全掛掉,保證主擔任讀寫
  使用者名稱和密碼是給57連線資料庫用的(1監視服務的執行狀態,主從結構是否正常 2監視誰是主庫,誰是從庫)
  
 #52 [Read-Only Service]        只讀服務  全部註釋
 #53 type=service
 #54 router=readconnroute
 #55 servers=server1
 #56 user=myuser
 #57 passwd=mypwd
 #58 router_options=slave




 63 [Read-Write Service]   定義讀寫分離服務
 64 type=service
 65 router=readwritesplit  呼叫的外掛功能
 66 servers=server1, server2 讀寫分離在server1和server2上進行
 67 user=pljsdmin
 68 passwd=123456
 69 max_slave_connections=100%
 配置好後,50訪問資料時,把請求給57主機,57上沒有資料,也沒有使用者,
 57接受請求後,根據select 請求,將請求轉給52,同時檢查客戶端訪問時候的使用者名稱pljadmin和密碼123456在資料庫伺服器上是否存在,讀取自己的配置檔案的使用者名稱和使用者名稱密碼連上資料庫進行檢查

 75 [MaxAdmin Service]           管理服務
 76 type=service
 77 router=cli                     呼叫命令列
 
 #[Read-Only Listener]               沒有定義只讀,全部註釋掉
 #86 type=listener
 #87 service=Read-Only Service

#88 protocol=MySQLClient
 #89 port=4008

 91 [Read-Write Listener]         定義讀寫分離服務使用的埠號
 92 type=listener                      
 93 service=Read-Write Service
 94 protocol=MySQLClient
 95 port=4006                             指定埠號
 
  97 [MaxAdmin Listener]          指定管理服務使用的埠號
 98 type=listener
 99 service=MaxAdmin Service
100 protocol=maxscaled
101 socket=default
102 port=4026                

二 主伺服器mysql51授權
1 用於客戶端登入的授權

grant replication slave,replication client on *.* to [email protected]"%" identified by "123456";

2 用於讀寫分離排程器驗證客戶端登入資訊連線資料庫的授權

 grant select on mysql.* to [email protected]"%" identified by "123456";

3 從資料庫伺服器mysql52檢視具體授權資訊,發現授權資訊已經同步

 mysql> select user,host from mysql.user;
+-----------+-------------+
| user      | host        |
+-----------+-------------+
| lily      | %           |
| pljadmin  | %           |
| repluser  | %           |
| scalemon  | %           |
| root      | 192.168.4.% |
| mysql.sys | localhost   |
| root      | localhost   |

mysql> show grants for 
[email protected]
"%"; +----------------------------------------------------------------------+ | Grants for [email protected]% | +----------------------------------------------------------------------+ | GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'scalemon'@'%' | +----------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql> show grants for [email protected]"%"; +---------------------------------------------+ | Grants for [email protected]% | +---------------------------------------------+ | GRANT USAGE ON *.* TO 'pljadmin'@'%' | | GRANT SELECT ON `mysql`.* TO 'pljadmin'@'%' | +---------------------------------------------+ 2 rows in set (0.00 sec)

三 排程器mycat57啟動maxscale 服務

[[email protected] ~]# vim /etc/maxscale.cnf
[[email protected] ~]# systemctl restart maxscale.service 
[[email protected] ~]# ss -antulp | grep maxscale
tcp    LISTEN     0      128      :::4026                 :::*                   users:(("maxscale",pid=30747,fd=12))
tcp    LISTEN     0      128      :::4006                 :::*                   users:(("maxscale",pid=30747,fd=11))

四:驗證

[[email protected] ~]# maxadmin -uadmin -pmariadb -P4026
MaxScale> list 
Unknown or missing option for the list command. Valid sub-commands are:
    clients    List all clients
    dcbs       List all DCBs
    filters    List all filters
    listeners  List all listeners
    modules    List all currently loaded modules
    monitors   List all monitors
    services   List all the services
    servers    List all servers
    sessions   List all sessions
    threads    List polling threads
    commands   List registered commands
MaxScale> list servers
Servers.
-------------------+-----------------+-------+-------------+--------------------
Server             | Address         | Port  | Connections | Status              
-------------------+-----------------+-------+-------------+--------------------
server1            | 192.168.4.51    |  3306 |           0 | Master, Running
server2            | 192.168.4.52    |  3306 |           0 | Slave, Running
-------------------+-----------------+-------+-------------+-------------------

五,客戶端訪問192.168.4.50

[ [email protected] aa]# mysql -h192.168.4.57 -P4006 -uyaya100 -p123456
 mysql> insert into a values(888);
Query OK, 1 row affected (0.08 sec)

mysql> select * from a;
+------+
| id   |
+------+
|  888 |
+------+

51  mysql> select * from db1.a;
+------+
| id   |
+------+
|  888 |

+------+
1 row in set (0.00 sec)

52 mysql> select * from a;
+------+
| id   |
+------+
|  888 |
+------+
1 row in set (0.00 sec)

寫入資料同步,那麼如何確定讀取資料時在從伺服器52上讀取的呢
在52上寫入資料,如果在51上查詢不到這個新資料,而在50上查不到,說明讀寫分離功能實現。

  mysql> insert into a values (999);
  mysql> select * from a;
+------+
| id   |
+------+
|  888 |
|  999 |
+------+
2 rows in set (0.00 sec)

51 查詢不到新資料

mysql> select * from db1.a;
+------+
| id   |
+------+
|  888 |
+------+
1 row in set (0.00 sec)

50上可以查詢到新資料

mysql> select * from a;
+------+
| id   |
+------+
|  888 |
|  999 |
+------+
2 rows in set (0.01 sec)

××××××××××××××××××××××××××××××××××××××××××

多例項概述
什麼是多例項:在一臺物理機上執行多個數據庫服務程式
為什麼要使用多例項: 節約運維成本,提高硬體利用率
缺點:單點故障,一臺伺服器掛了,就全掛了
訪問時通過mysql.sock檔案來傳遞資料

每個例項必須要有自己的資料庫目錄和socket套接字檔案以及埠號,錯誤日誌log-error,pid檔案 pid-file

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

5.7.17版本的Mysql不帶多例項的功能。
停止mysqld服務
1 重新裝包:mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz

[[email protected] ~]# mkdir bb
[[email protected] ~]# tar -xf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz -C bb
[[email protected] ~]# cd bb/mysql-5.7.20-linux-glibc2.12-x86_64/

將整個資料庫目錄移動到/usr/local下改名為mysql

[[email protected] ~]# mv bb/mysql-5.7.20-linux-glibc2.12-x86_64/ /usr/local/mysq
[[email protected] ~]# vim /etc/profilel
#/etc/profile
PATH=/usr/local/mysql/bin:$PATH
[[email protected] ~]# source /etc/profile

2 修改配置檔案
每個例項要有獨立的資料庫目錄、監聽埠號、例項名稱和獨立的sock檔案

[[email protected] mysql]# vim /etc/my.cnf
[mysqld_multi]         //啟用多例項
mysqld = /usr/local/mysql/bin/mysqld_safe   // 指定程序檔案路徑
mysqladmin = /usr/local/mysql/bin/mysqladmin  //指定管理命令路徑
user = root               //指定程序使用者
 
[mysql1i]                   //例項程序名稱
port = 3307              //埠號
datadir = /data3307              //資料庫目錄,要手動建立
socket = /data3307/mysql3307.sock       //指定sock檔案的路徑和名稱
pid-file = /data3307/mysql3307.pid         //程序Pid號檔案位置
log-error = /data3307/mysql3307.log      //錯誤日誌檔案

[mysqld2]
port = 3308
datadir = /data3308
socket = /data3308/mysql3308.sock
pid-file = /data3308/mysql3308.pid
log-error = /data3308/mysql3308.log

3 建立資料庫目錄

[[email protected] mysql]# mkdir /data3307
[[email protected] mysql]# mkdir /data3308

4 建立程序執行的所有者和組 mysql

[[email protected] mysql]# useradd mysql
[[email protected] mysql]# chown  mysql:mysql  /data*
[[email protected] mysql]# ls -ld /data3307
drwxr-xr-x. 2 mysql mysql 6 11月 24 17:01 /data3307
[[email protected] mysql]# ls -ld /data3308
drwxr-xr-x. 2 mysql mysql 6 11月 24 17:02 /data3308

5 初始化授權庫

[[email protected] mysql]# mysql --user=mysql --basedir=/usr/local/mysql --datadir=/data3307 --initialize     
(此步驟可以省略,在啟動例項程序時會自動建立)
初始化
[[email protected] mysql]# mysqld_multi start 1
2018-11-24T09:16:37.396125Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-11-24T09:16:37.397881Z 1 [Note] A temporary password is generated for [email protected]: z>qlsf_Nh6K+
[[email protected] mysql]# mysqld_multi start 2
2018-11-24T09:17:54.081693Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-11-24T09:17:54.083617Z 1 [Note] A temporary password is generated for [email protected]: 7NUdtqrK6y-_

6 連線資料庫3307

[[email protected] mysql]# mysql -uroot -p'z>qlsf_Nh6K+' -S /data3307/mysql3307.sock
修改密碼
mysql> alter user [email protected] identified by "123456";
檢視資料庫
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
mysql> create database db3307;
mysql> create table db3307.b(id int);
mysql> insert into db3307.b values(3307);

7 連線資料庫3308

[[email protected] mysql]# mysql -uroot -p'7NUdtqrK6y-_' -S /data3308/mysql3308.sock
mysql> alter user [email protected] identified by "123456";
mysql> create database db3308;
mysql> create table db3308.a(id int);
mysql> insert into db3308.a values(888);

8 檢視服務埠

[[email protected] mysql]# ss -antulp | grep mysqld
tcp    LISTEN     0      80       :::3307                 :::*                   users:(("mysqld",pid=32510,fd=16))
tcp    LISTEN     0      80       :::3308                 :::*                   users:(("mysqld",pid=32706,fd=16))

9 關閉3307的埠

[[email protected] mysql]# mysqld_multi --user=root --password=123456 stop 1
[[email protected] mysql]# ss -antulp | grep mysqld
tcp    LISTEN     0      80       :::3308                 :::*                   users:(("mysqld",pid=32706,fd=16))