MariaDB集羣是MariaDB同步多主機集羣。它僅支持XtraDB/ InnoDB存儲引擎。node
Galera集羣的複製功能基於Galeralibrary實現,爲了讓MySQL與Galera library通信,特別針對MySQL開發了wsrep API。mysql
Galera插件保證集羣同步數據,保持數據的一致性,靠的就是可認證的複製,工做原理以下圖:sql
當客戶端發出一個commit的指令,在事務被提交以前,全部對數據庫的更改都會被 write-set 收集起來,而且將 write-set 紀錄的內容發送給其餘節點。數據庫
write-set 將在每一個節點進行認證測試,測試結果決定着節點是否應用write-set更改數據。vim
若是認證測試失敗,節點將丟棄 write-set ;若是認證測試成功,則事務提交。centos
準備三臺服務器服務器
操做系統版本: centos7app
數據庫版本:5.5.56-MariaDBdom
主節點:192.168.64.4異步
節點1:192.168.64.5
節點2:192.168.64.7
各個節點之間須要解析
[root@ken-node1 ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.64.4 ken-node1 192.168.64.5 ken-node2 192.168.64.7 ken-node3
配置yum源
[mariadb] name = MariaDB baseurl = http://yum.mariadb.org/10.3.5/centos74-amd64 gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB gpgcheck=1 enabled=0
第一步:在各個節點下載下面的軟件包
[root@ken ~]# yum install -y MariaDB-server MariaDB-client galera -y
第二步: 初始化數據庫
只須要在一個節點執行便可192.168.64.4
[root@ken ~]# systemctl restart mariadb [root@ken ~]# mysql_secure_installation NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! In order to log into MariaDB to secure it, we'll need the current password for the root user. If you've just installed MariaDB, and you haven't set the root password yet, the password will be blank, so you should just press enter here. Enter current password for root (enter for none): OK, successfully used password, moving on... Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorisation. Set root password? [Y/n] y New password: Re-enter new password: Password updated successfully! Reloading privilege tables.. ... Success! By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? [Y/n] y ... Success! Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? [Y/n] n ... skipping. By default, MariaDB comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? [Y/n] n ... skipping. Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? [Y/n] y ... Success! Cleaning up... All done! If you've completed all of the above steps, your MariaDB installation should now be secure. Thanks for using MariaDB!
第三步:關閉數據庫,修改/etc/my.cnf.d/server.cnf
在主配置節點配置以下
[root@ken ~]# systemctl stop mariadb [root@ken ~]# vim /etc/my.cnf.d/server.cnf ... [galera] wsrep_on=ON wsrep_provider=/usr/lib64/galera/libgalera_smm.so wsrep_cluster_address="gcomm://192.168.64.4,192.168.64.5,192.168,64.7" #節點IP地址 wsrep_node_name= ken-node1 #主節點主機名 wsrep_node_address=192.168.64.4 #主節點IP binlog_format=row default_storage_engine=InnoDB innodb_autoinc_lock_mode=2 wsrep_slave_threads=1 innodb_flush_log_at_trx_commit=0 innodb_buffer_pool_size=120M wsrep_sst_method=rsync wsrep_causal_reads=ON
第四步:將此文件複製到mariadb-二、mariadb-3,注意要把 wsrep_node_name 和 wsrep_node_address 改爲相應節點的 hostname 和 ip。
第五步:啓動集羣
主節點192.168.64.4啓動命令以下:
[root@ken-node1 ~]# galera_new_cluster
各個節點監聽的端口
集羣服務監聽了4567和3306端口(每一個節點都監聽了4567和3306端口)
[root@ken-node1 ~]# ss -tnl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* LISTEN 0 128 *:4567 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 80 :::3306 :::* LISTEN 0 128 :::80 :::* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::*
其餘節點啓動命令以下:
[root@ken-node2 ~]# systemctl restart mariadb
在主節點執行以下操做
顯示ON表示開啓
MariaDB [(none)]> show status like "wsrep_ready"; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | wsrep_ready | ON | +---------------+-------+ 1 row in set (0.005 sec)
機器數爲3個顯示正確
MariaDB [(none)]> show status like "wsrep_cluster_size"; +--------------------+-------+ | Variable_name | Value | +--------------------+-------+ | wsrep_cluster_size | 3 | +--------------------+-------+ 1 row in set (0.003 sec)
MariaDB [(none)]> show status like "wsrep%"; +------------------------------+-------------------------------------------------------+ | Variable_name | Value | +------------------------------+-------------------------------------------------------+ | wsrep_apply_oooe | 0.000000 | | wsrep_apply_oool | 0.000000 | | wsrep_apply_window | 0.000000 | | wsrep_causal_reads | 2 | | wsrep_cert_deps_distance | 0.000000 | | wsrep_cert_index_size | 0 | | wsrep_cert_interval | 0.000000 | | wsrep_cluster_conf_id | 14 | | wsrep_cluster_size | 3 | | wsrep_cluster_state_uuid | 541248ce-56d0-11e9-b41b-3a9775d73ca7 | | wsrep_cluster_status | Primary #主服務器 | | wsrep_commit_oooe | 0.000000 | | wsrep_commit_oool | 0.000000 | | wsrep_commit_window | 0.000000 | | wsrep_connected | ON #當前是否鏈接中 | | wsrep_desync_count | 0 | | wsrep_evs_delayed | | | wsrep_evs_evict_list | | | wsrep_evs_repl_latency | 0/0/0/0/0 | | wsrep_evs_state | OPERATIONAL | | wsrep_flow_control_paused | 0.000000 | | wsrep_flow_control_paused_ns | 0 | | wsrep_flow_control_recv | 0 | | wsrep_flow_control_sent | 0 | | wsrep_gcomm_uuid | 540ee869-56d0-11e9-955e-9b7f30e437a6 | | wsrep_incoming_addresses | 192.168.64.5:3306,192.168.64.4:3306,192.168.64.7:3306 | #鏈接中的數據庫 | wsrep_last_committed | 0 | | wsrep_local_bf_aborts | 0 | | wsrep_local_cached_downto | 18446744073709551615 | | wsrep_local_cert_failures | 0 | | wsrep_local_commits | 0 | | wsrep_local_index | 1 | | wsrep_local_recv_queue | 0 | | wsrep_local_recv_queue_avg | 0.100000 | | wsrep_local_recv_queue_max | 2 | | wsrep_local_recv_queue_min | 0 | | wsrep_local_replays | 0 | | wsrep_local_send_queue | 0 | | wsrep_local_send_queue_avg | 0.000000 | | wsrep_local_send_queue_max | 1 | | wsrep_local_send_queue_min | 0 | | wsrep_local_state | 4 | | wsrep_local_state_comment | Synced | | wsrep_local_state_uuid | 541248ce-56d0-11e9-b41b-3a9775d73ca7 | | wsrep_protocol_version | 8 | | wsrep_provider_name | Galera | | wsrep_provider_vendor | Codership Oy <info@codership.com> | | wsrep_provider_version | 25.3.23(r3789) | | wsrep_ready | ON | | wsrep_received | 20 | | wsrep_received_bytes | 3651 | | wsrep_repl_data_bytes | 0 | | wsrep_repl_keys | 0 | | wsrep_repl_keys_bytes | 0 | | wsrep_repl_other_bytes | 0 | | wsrep_replicated | 0 | | wsrep_replicated_bytes | 0 | | wsrep_thread_count | 2 | +------------------------------+-------------------------------------------------------+ 58 rows in set (0.002 sec)
MariaDB [(none)]> show status like "wsrep_incoming_addresses"; +--------------------------+-------------------------------------------------------+ | Variable_name | Value | +--------------------------+-------------------------------------------------------+ | wsrep_incoming_addresses | 192.168.64.5:3306,192.168.64.4:3306,192.168.64.7:3306 | +--------------------------+-------------------------------------------------------+ 1 row in set (0.001 sec)
在各個節點建立數據庫,能夠發現每一個節點均可以同步
我在192.168.64.4建立ken數據庫
[root@ken-node1 ~]# mysql -uroot -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 12 Server version: 10.3.5-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> create database ken; Query OK, 1 row affected (0.041 sec) MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | ken | | ken1 | | mysql | | performance_schema | | test | +--------------------+ 6 rows in set (0.001 sec)
在192.168.64.5節點上查看數據庫
[root@ken-node2 ~]# mysql -uroot -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 11 Server version: 10.3.5-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | ken | #ken數據庫已經同步過來 | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.002 sec)
192.168.64.7節點上查看數據庫
[root@ken-node3 ~]# mysql -uroot -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 10 Server version: 10.3.5-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | ken | #ken數據庫已經同步 | | mysql | | performance_schema | | test | +--------------------+ 6 rows in set (0.002 sec)