mariadb主從
主從多用於網站架構,由於主從的同步機制是異步的,數據的同步有必定延遲,也就是說有可能會形成數據的丟失,可是性能比較好,所以網站大多數用的是主從架構的數據庫,讀寫分離必須基於主從架構來搭建html
mysql主從配置 yum配置 [mariadb] name=MariaDB baseurl=http://mirrors.ustc.edu.cn/mariadb/yum/10.3/centos7-amd64/ gpgkey=http://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB gpgcheck=1 服務器1: MariaDB01 192.168.254.28 服務器2: MariaDB02 192.168.254.29 操做系統: CentOS7.3 數據庫版本: MariaDB-10.3.7 主從關係: MariaDB01爲主,MariaDB02爲從 MariaDB01 1. 修改配置文件 vi /etc/my.cnf.d/server.cnf 在[mysqld]下加入如下 server-id=1 log-bin=mysql-bin 2. 重啓mysql服務 service mysqld restart 3.建立主從鏈接賬號與受權 CREATE USER 'slave'@'%' IDENTIFIED BY 'slave'; GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%'; flush privileges; 4.show master status MariaDB02 1. 修改配置文件 vi /etc/my.cnf.d/server.cnf
server-id=2
2. 重啓mysql服務 service mysqld restart 3.登陸數據庫 mysql -uroot -proot4. 創建主從鏈接 CHANGE MASTER TO MASTER_HOST='192.168.254.28', MASTER_USER='slave', MASTER_PASSWORD='slave', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=1443; start slave; 5.驗證!!! create database test01 use test01 create table students(id int(10), name char(20), age int(10)) 以後查看從節點是否有test01數據庫和students表 6.查看mysql二進制log mysqlbinlog /var/lib/mysql/mysql-bin.000001 #若是出現不一樣步能夠執行如下步驟 stop slave set GLOBAL SQL_SLAVE_SKIP_COUNTER=1; start slave 或者 stop slave; mysql> change master to master_host='192.168.254.28', master_user='user', master_password='pwd', master_port=3306, master_log_file='mysql-bin.000008', master_log_pos=483; start slave
mariadb galera集羣(多主)
galera集羣多用於關鍵性業務,由於galera集羣爲了數據的一致性,採用的是同步的機制,這就使galera犧牲了一部分性能來換取數據一致性。node
實戰Mariadb10.3(10.3自帶galera軟件) galera Cluster集羣架構 Mariadb galera Cluster安裝: 操做系統:Centos7.3版本 集羣數量:3個節點 主機信息: 192.168.153.142 node1 selinux=disabled firewalld關閉 192.168.153.143 node2 selinux=disabled firewalld關閉 192.168.153.144 node3 selinux=disabled firewalld關閉 搭建步驟 1.主機之間互相解析:三臺節點都要執行 vim /etc/hosts 192.168.153.142 node1 192.168.153.143 node2 192.168.153.144 node3 2.安裝軟件包 第一種方法:(yum install -y MariaDB-server MariaDB-client galera) 配置yum安裝源和配置mariadb galera安裝源 yum源配置掛iso 設置mariadb的yum源並安裝(全部節點都要) 修改yum源文件 vi /etc/yum.repos.d/mariadb.repo [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 安裝galera軟件時須要解決它的依賴包:boost-program-options.x86_64 (直接yum源安裝便可) 3.mariadb初始化 (三個節點都須要執行) 安裝完成後會提示須要對mariadb進行初始化(設置密碼) systemctl start mariadb mysql_secure_installation (按提示設置mysql密碼) systemctl stop mariadb 4.配置galera 主節點配置文件server.cnf vim /etc/my.cnf.d/server.cnf [galera] wsrep_on=ON wsrep_provider=/usr/lib64/galera/libgalera_smm.so #galera的庫文件的地址 wsrep_cluster_address="gcomm://192.168.153.142,192.168.153.143,192.168.153.144" #各節點的ip wsrep_node_name=node1 #節點主機名 wsrep_node_address=192.168.153.142 #節點ip binlog_format=row #二進制日誌設置爲行模式 default_storage_engine=InnoDB #使用的默認引擎 innodb_autoinc_lock_mode=2 #性能最好 wsrep_slave_threads=1 #並行複製線程數 innodb_flush_log_at_trx_commit=0 #0.log buffer將每秒一次地寫入log file中,而且log file的flush(刷到磁盤)操做同時進行。該模式下在事務提交的時候,不會主動觸發寫入磁盤的操做。 #1:每次事務提交時MySQL都會把log buffer的數據寫入log file,而且flush(刷到磁盤)中去,該模式爲系統默認。 #2:每次事務提交時MySQL都會把log buffer的數據寫入log file,可是flush(刷到磁盤)操做並不會同時進行。該模式下,MySQL會每秒執行一次 flush(刷到磁盤)操做 innodb_buffer_pool_size=120M #設置緩存池大小 wsrep_sst_method=rsync #遠程同步 wsrep_causal_reads=ON #避免各個節點的數據不一致,這種狀況須要等待全同步複製 將此文件複製到mariadb-二、mariadb-3,注意要把 wsrep_node_name 和 wsrep_node_address 改爲相應節點的 hostname 和 ip。 5.啓動集羣服務: 啓動 MariaDB Galera Cluster 服務:
(第一次啓動要用初始化:mysqld_safe --wsrep_cluster_address=gcomm://192.168.254.24,192.168.254.27,192.168.254.36 >/dev/null &) [root@node1 ~]# /bin/galera_new_cluster 剩餘兩節點啓動方式爲: [root@node1 ~]# systemctl start mariadb 查看集羣狀態:(集羣服務使用了4567和3306端口)) [root@node1 ~]# netstat -tulpn | grep -e 4567 -e 3306 tcp 0 0 0.0.0.0:4567 0.0.0.0: LISTEN 3557/mysqld tcp6 0 0 :::3306 ::: LISTEN 3557/mysqld 6.驗證集羣狀態: 在node1上執行: [root@node1 ~]# mysql -uroot -p ##進入數據庫 查看是否啓用galera插件 鏈接mariadb,查看是否啓用galera插件 MariaDB [(none)]> show status like "wsrep_ready"; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | wsrep_ready | ON | +---------------+-------+ 1 row in set (0.004 sec) 目前集羣機器數 MariaDB [(none)]> show status like "wsrep_cluster_size"; +--------------------+-------+ | Variable_name | Value | +--------------------+-------+ | wsrep_cluster_size | 3 | +--------------------+-------+ 1 row in set (0.001 sec) 查看集羣狀態 MariaDB [(none)]> show status like "wsrep%"; +------------------------------+----------------------------------------------------------------+ | Variable_name | Value | +------------------------------+----------------------------------------------------------------+ | wsrep_apply_oooe | 0.000000 | | wsrep_apply_oool | 0.000000 | | wsrep_apply_window | 1.000000 | | wsrep_causal_reads | 14 | | wsrep_cert_deps_distance | 1.200000 | | wsrep_cert_index_size | 3 | | wsrep_cert_interval | 0.000000 | | wsrep_cluster_conf_id | 22 | | wsrep_cluster_size | 3 | ##集羣成員 | wsrep_cluster_state_uuid | b8ecf355-233a-11e8-825e-bb38179b0eb4 | ##UUID 集羣惟一標記 | wsrep_cluster_status | Primary | ##主服務器 | wsrep_commit_oooe | 0.000000 | | wsrep_commit_oool | 0.000000 | | wsrep_commit_window | 1.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 | 0eba3aff-2341-11e8-b45a-f277db2349d5 | | wsrep_incoming_addresses | 192.168.153.142:3306,192.168.153.143:3306,192.168.153.144:3306 | ##鏈接中的數據庫 | wsrep_last_committed | 9 | ##sql 提交記錄 | wsrep_local_bf_aborts | 0 | ##從執行事務過程被本地中斷 | wsrep_local_cached_downto | 5 | | wsrep_local_cert_failures | 0 | ##本地失敗事務 | wsrep_local_commits | 4 | ##本地執行的sql | wsrep_local_index | 0 | | wsrep_local_recv_queue | 0 | | wsrep_local_recv_queue_avg | 0.057143 | | 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 | b8ecf355-233a-11e8-825e-bb38179b0eb4 | ##集羣ID | 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 | 35 | ##數據複製接收次數 | wsrep_received_bytes | 5050 | | wsrep_repl_data_bytes | 1022 | | wsrep_repl_keys | 14 | | wsrep_repl_keys_bytes | 232 | | wsrep_repl_other_bytes | 0 | | wsrep_replicated | 5 | ##隨着複製發出的次數 | wsrep_replicated_bytes | 1600 | ##數據複製發出的字節數 | wsrep_thread_count | 2 | +------------------------------+----------------------------------------------------------------+ 58 rows in set (0.003 sec) 查看鏈接的主機 MariaDB [(none)]> show status like "wsrep_incoming_addresses"; +--------------------------+----------------------------------------------------------------+ | Variable_name | Value | +--------------------------+----------------------------------------------------------------+ | wsrep_incoming_addresses | 192.168.153.142:3306,192.168.153.143:3306,192.168.153.144:3306 | +--------------------------+----------------------------------------------------------------+ 1 row in set (0.002 sec) 7.測試集羣mariad數據是否同步 MariaDB [(none)]> create database lizk; Query OK, 1 row affected (0.010 sec) MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | china | | hello | | hi | | information_schema | | lizk | | mysql | | performance_schema | | test | +--------------------+ 8 rows in set (0.001 sec) 在其餘兩個節點上能夠查看lizk庫已經同步。 注意: 若是galera_new_cluster 報錯 vim /var/lib/mysql/grastate.dat 把safe_to_bootstrap更改成1 # GALERA saved state version: 2.1 uuid: a393feef-f639-11e8-9b89-4e75f9b8fb0f seqno: -1 safe_to_bootstrap: 1