利用Galera Cluster解決主主復制單點問題
關於Galera Cluster
Galera Cluster:集成了Galera插件的MySQL集群,是一種新型的,數據不共享的,高度冗余的高可用方案,目前Galera Cluster有兩個版本,分別是Percona Xtradb Cluster及MariaDB Cluster,Galera本身是具有多主特性的,即采用multi-master的集群架構,是一個既穩健,又在數據一致性、完整性及高性能方面有出色表現的高可用解決方案
Galera Cluster特點
- 多主架構:真正的多點讀寫的集群,在任何時候讀寫數據,都是最新的
- 同步復制:集群不同節點之間數據同步,沒有延遲,在數據庫掛掉之後,數據不會丟失
- 並發復制:從節點APPLY數據時,支持並行執行,更好的性能
- 故障切換:在出現數據庫故障時,因支持多點寫入,切換容易
- 熱插拔:在服務期間,如果數據庫掛了,只要監控程序發現的夠快,不可服務時間就會非常少。在節點故障期間,節點本身對集群的影響非常小
- 自動節點克隆:在新增節點,或者停機維護時,增量數據或者基礎數據不需要人工手動備份提供,Galera Cluster會自動拉取在線節點數據,最終集群會變為一致
- 對應用透明:集群的維護,對應用程序是透明的
Galera Cluster工作過程
Galera Cluster場景模擬
三個節點組成了一個集群,與普通的主從架構不同,它們都可以作為主節點,三個節點是對等的,稱為multi-master架構,當有客戶端要寫入或者讀取數據時,連接哪個實例都是一樣的,讀到的數據是相同的,寫入某一個節點之後,集群自己會將新數據同步到其它節點上面,這種架構不共享任何數據,是一種高冗余架構
IP地址分配
主機 | IP地址 | 類型 |
---|---|---|
CentOS7.6 | 192.168.36.101 | Master |
CentOS7.6 | 192.168.36.103 | Master |
CentOS7.6 | 192.168.36.104 | Master |
創建Galera源
[[email protected] ~]#cd /etc/yum.repos.d/ [[email protected] yum.repos.d]#vim galera.repo [galera] name=galera baseurl=https://mirrors.tuna.tsinghua.edu.cn/mariadb/mariadb-5.5.64/yum/centos7-amd64/ gpgcheck=0
將Galera源scp至其余兩個節點
[[email protected] yum.repos.d]#scp galera.repo 192.168.36.104:/etc/yum.repos.d/
[email protected]‘s password:
galera.repo 100% 119 3.6KB/s 00:00
[[email protected] yum.repos.d]#scp galera.repo 192.168.36.103:/etc/yum.repos.d/
[email protected]‘s password:
galera.repo 100% 119 40.2KB/s 00:00
安裝MariaDB-Galera-server
[[email protected] ~]#yum install MariaDB-Galera-server
修改Galera配置文件
[[email protected] ~]#vim /etc/my.cnf.d/server.cnf
[galera]
# Mandatory settings
wsrep_provider=/usr/lib64/galera/libgalera_smm.so # 模塊庫文件所在路徑
wsrep_cluster_address="gcomm://192.168.36.101,192.168.36.103,192.168.36.104" # 集群IP地址
binlog_format=row # 基於行語句
#default_storage_engine=InnoDB
#innodb_autoinc_lock_mode=2
#bind-address=0.0.0.0
將Galera配置文件同步到所有機器上
[[email protected] ~]#scp /etc/my.cnf.d/server.cnf 192.168.36.103:/etc/my.cnf.d/
[email protected]‘s password:
server.cnf 100% 1111 92.4KB/s 00:00
[[email protected] ~]#scp /etc/my.cnf.d/server.cnf 192.168.36.104:/etc/my.cnf.d/
[email protected]‘s password:
server.cnf 100% 1111 42.0KB/s 00:00
啟動服務:要求先啟動一個,初始化集群,然後再啟用其余節點(--wsrep-new-cluster啟動一個新的集群節點)
[[email protected] ~]#service mysql start --wsrep-new-cluster
Starting MariaDB.190511 10:53:55 mysqld_safe Logging to ‘/var/lib/mysql/CentOS7.magedu36.com.err‘.
190511 10:53:55 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
...... SUCCESS!
[[email protected] ~]#service mysql start
Starting MariaDB.190510 10:54:29 mysqld_safe Logging to ‘/var/lib/mysql/CentOS7-1.err‘.
190510 10:54:29 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
...SST in progress, setting sleep higher.. SUCCESS!
[[email protected] ~]#service mysql start
Starting MariaDB.190510 10:54:37 mysqld_safe Logging to ‘/var/lib/mysql/CentOS7-2.err‘.
190510 10:54:37 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
...SST in progress, setting sleep higher.... SUCCESS!
此時已經實現多主復制
安全初始化
[[email protected] ~]#mysql_secure_installation
/usr/bin/mysql_secure_installation: line 393: find_mysql_client: command not found
....
Set root password? [Y/n]
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
Remove anonymous users? [Y/n]
... Success!
Disallow root login remotely? [Y/n]
... Success!
Remove test database and access to it? [Y/n]
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reload privilege tables now? [Y/n]
... Success!
Thanks for using MariaDB!
同時創建表,模仿沖突案例
成功解決沖突問題
MySQL壓力測試
簡介
Mysqlslap:來自於mariadb包,測試的過程默認生成一個mysqlslap的schema,生成測試表t1,查詢和插入測試數據,mysqlslap庫自動生成,如果已經存在則先刪除。用--only-print來打印實際的測試過程,整個測試完成後不會在數據庫中留下痕跡
使用格式
mysqlslap [options]
常用參數
--auto-generate-sql, -a 自動生成測試表和數據,表示用mysqlslap工具自己生成的SQL腳本來測試並發壓力
--auto-generate-sql-load-type=type 測試語句的類型。代表要測試的環境是讀操作還是寫操作還是兩者混合的。取值包括:read,key,write,update和mixed(默認)
--auto-generate-sql-add-auto-increment 代表對生成的表自動添加auto_increment列,從5.1.18版本開始支持
--number-char-cols=N, -x N 自動生成的測試表中包含多少個字符類型的列,默認1
--number-int-cols=N, -y N 自動生成的測試表中包含多少個數字類型的列,默認1
--number-of-queries=N 總的測試查詢次數(並發客戶數×每客戶查詢次數)
--query=name,-q 使用自定義腳本執行測試,例如可以調用自定義的存儲過程或者sql語句來執行測試
--create-schema 代表自定義的測試庫名稱,測試的schema
--commint=N 多少條DML後提交一次
--compress, -C 如服務器和客戶端都支持壓縮,則壓縮信息
--concurrency=N, -c N 表示並發量,即模擬多少個客戶端同時執行select。可指定多個值,以逗號或者--delimiter參數指定值做為分隔符如:--concurrency=100,200,500
--engine=engine_name, -e engine_name 代表要測試的引擎,可以有多個,用分隔符隔開。例如:--engines=myisam,innodb
--iterations=N, -i N 測試執行的叠代次數,代表要在不同並發環境下,各自運行測試多少次
--only-print 只打印測試語句而不實際執行。
--detach=N 執行N條語句後斷開重連
--debug-info, -T 打印內存和CPU的相關信息
mysqlslap示例
單線程測試
mysqlslap -a -uroot -pmagedu
多線程測試。使用–concurrency來模擬並發連接
mysqlslap -a -c 100 -uroot -pmagedu
叠代測試。用於需要多次執行測試得到平均值
mysqlslap -a -i 10 -uroot -pmagedu mysqlslap ---auto-generate-sql-add-autoincrement -a mysqlslap -a --auto-generate-sql-load-type=read mysqlslap -a --auto-generate-secondary-indexes=3 mysqlslap -a --auto-generate-sql-write-number=1000 mysqlslap --create-schema world -q "select count(*) from City” mysqlslap -a -e innodb -uroot -pmagedu mysqlslap -a --number-of-queries=10 -uroot -pmagedu
測試同時不同的存儲引擎的性能進行對比
mysqlslap -a --concurrency=50,100 --number-of-queries 1000 --iterations=5 --engine=myisam,innodb --debug-info -uroot -pmagedu
執行一次測試,分別50和100個並發,執行1000次總查詢
mysqlslap -a --concurrency=50,100 --number-of-queries 1000 --debuginfo -uroot -pmagedu
50和100個並發分別得到一次測試結果(Benchmark),並發數越多,執行完所有查詢的時間越長。為了準確起見,可以多叠代測試幾次
mysqlslap -a --concurrency=50,100 --number-of-queries 1000 --iterations=5 --debug-info -uroot -pmagedu
利用Galera Cluster解決主主復制單點問題