一、mariadb主从同步
主从多用于网站架构,因为主从的同步机制是异步的,数据的同步有一定延迟,也就是说有可能会造成数据的丢失,但是性能比较好,因此网站大多数用的是主从架构的数据库,读写分离必须基于主从架构来搭建
1.原理过程:
master记录二进制日志。在每个事务更新数据完成之前,master在二进制日志记录这些改变。MySQL将事务串行的写入二进制日志,即使事务中的语句都是交叉执行的。在事件写入二进制日志完成后,master通知存储引擎提交事务。
slave将master的binary log拷贝到它自己的中继日志。首先,slave开始一个工作线程——I/O线程。I/O线程在master上打开一个普通的连接,然后开始binlog dump process。Binlog dump process从master的二进制日志中读取事件,如果已经跟上master,它会睡眠并等待master产生新的事件。I/O线程将这些事件写入中继日志。
SQL slave thread(SQL从线程)处理该过程的最后一步。SQL线程从中继日志读取事件,并重放其中的事件而更新slave的数据,使其与master中的数据一致。只要该线程与I/O线程保持一致,中继日志通常会位于OS的缓存中,所以中继日志的开销很小。
此外,在master中也有一个工作线程:和其它MySQL的连接一样,slave在master中打开一个连接也会使得master开始一个线程。复制过程有一个很重要的限制–>复制在slave上是串行化的,也就是说master上的并行更新操作不能在slave上并行操作。
2.mariadb主从配置
环境准备:
mariadb01:192.168.30.148
mariadb02:192.168.30.149
操作系统:CentOS-7-x86_64
数据库版本:Mariadb-10.3.7(数据库版本要一致)
主从关系:mariadb01为主,mariadb02为从
注意:
版本
(1)双方的MySQL版本要一致;
(2)如果不一致:主的要低于从的;
12
从什么位置开始复制:
(1)都从0开始:两台新服务器,没有任何数据;
(2)主服务器已经运行一段时间,并且存在不小的数据集:把主服务器备份,然后在从服务恢复,从主服务器上备份时所处的位置开始复制。
12
配置mariadb01:
1.修改配置文件
在[server]或[mysqld]下添加以下配置
server-id=1
log-bin=mysql-bin
1
2
2.重启mariadb服务
systemctl restart mariadb
1
3.创建主从连接账号与授权
4.查看主服务器数据库状态
配置mariadb02:
1.修改配置文件
vim /etc/my.cnf
同样的位置添加如下配置
server-id=2
2.重启服务
systemctl restart mariadb
1
3.登录数据库建立主从连接
MariaDB [(none)]> change master to master_host=‘192.168.30.148’,
-> master_user=‘slave’,
-> master_password=‘slave’,
-> master_log_file=‘mysql-bin.000001’,
-> master_log_pos=765;
12345
注意:
MASTER_LOG_FILE
MASTER_LOG_POS
此两项必须与master一致
4.启用复制线程
MariaDB [(none)]> start slave;
MariaDB [ryan]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.30.148
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 550323
Relay_Log_File: c1-relay-bin.000002
Relay_Log_Pos: 550113
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 550323
Relay_Log_Space: 550419
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: No
Gtid_IO_Pos:
Replicate_Do_Domain_Ids:
Replicate_Ignore_Domain_Ids:
Parallel_Mode: conservative
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Slave_DDL_Groups: 134
Slave_Non_Transactional_Groups: 6
Slave_Transactional_Groups: 10
1 row in set (0.000 sec)
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
5.测试
a.在master上创建库
MariaDB [(none)]> show databases;
±-------------------+
| Database |
±-------------------+
| information_schema |
| mysql |
| performance_schema |
| ryan |
±-------------------+
4 rows in set (0.001 sec)
MariaDB [(none)]> create database test;
Query OK, 1 row affected (0.001 sec)
12345678910111213
再去slave查看
MariaDB [(none)]> show databases;
±-------------------+
| Database |
±-------------------+
| information_schema |
| mysql |
| performance_schema |
| ryan |
| test |
±-------------------+
5 rows in set (0.001 sec)
1234567891011
b.在test库中简单创建一个表
MariaDB [(none)]> use test
Database changed
MariaDB [test]> create table students(id int(10),name varchar(20));
Query OK, 0 rows affected (0.030 sec)
MariaDB [test]> show tables;
±---------------+
| Tables_in_test |
±---------------+
| students |
±---------------+
1 row in set (0.000 sec)
123456789101112
去slave上查看
MariaDB [(none)]> use test
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [test]> show tables;
±---------------+
| Tables_in_test |
±---------------+
| students |
±---------------+
1 row in set (0.000 sec)
123456789101112
补充6.如果master已经运行很久,新加入的slave数据与master不一致需要进行同步,补充手动同步过程
现在slave上进行备份,复制给master
[[email protected] ~]# mysqldump -uroot -proot --all-database > all.sql
[[email protected] ~]# scp all.sql [email protected]:/root/
12
master同步,备份一定要在当前目录下
MariaDB [(none)]> source all.sql
1
再备份master数据到slave,步骤同上,这样就实现主从完全相同。
二、mariadb galera 集群搭建(多主)
Galera Cluster是由第三方公司Codership所研发的一套免费开源的集群高可用方案,实现了数据零丢失
mariadb galera实现数据库的多主模式,该模式是通过在写数据的时候,确保数据写入到所有服务器中之后才认为该写入操作成功,所以其能够基本保持数据的一致性以及数据操作的原子性。并且其不存在mysql主从中的不能在从上面写入数据的原则,在所有服务器上面都可以写入数据,其会自动同步到所有服务器中。当然,不能只谈mariadb galera的好处不谈它的缺点,它的缺点就是其写入数据的性能是由集群中最差的一台服务器来决定的,所以在生产环境中需要尽量保持集群中的所有服务器软硬件配置一样,从而避免所谓的木桶原理影响性能。还有就是mariadb galera只能使用innodb存储引擎,而不能使用其他存储引擎,并且不支持锁表操作。
1.准备环境
准备三台数据库主机:
数据库版本:Mariadb-10.3.7
mariadb-node1:192.168.30.148
mariadb-node1:192.168.30.149
mariadb-node1:192.168.30.150
操作系统:CentOS-7-x86_64
12345
2.说明
数据库从MariaDB 10.1.20 版本开始,Galera就已经包含在MariaDB包里面了,不需要单独部署MariaDB-Galera-server 和galera 包。
如果使用iso镜像的本地yum源下载版本较低需要另为下载Galera等一些包(yum install -y MariaDB-server MariaDB-client galera)
安装galera软件时需要解决它的依赖包:boost-program-options.x86_64 (直接yum源安装即可)
3.配置网络yum源下载
参考文章mariadb数据库(一)
4.mariadb初始化 (三个节点都需要执行),并使三个数据库数据保持一致
安装完成后会提示需要对mariadb进行初始化(设置密码)
systemctl start mariadb
mysql_secure_installation (按提示设置mysql密码)
systemctl stop mariadb
123
5.主机之间互相解析(可不配,配的话三台主机都得配)
vim /etc/hosts
192.168.30.148 node1
192.168.30.149 node2
192.168.30.150 node3
1234
6.配置mariadb galera,修改三个节点的/etc/my.cnf.d/server.cnf配置文件,具体配置如下:
mariadb-node1
[[email protected] ~]# vim /etc/my.cnf.d/server.cnf
[server]
this is only for the mysqld standalone daemon
[mysqld]
* Galera-related settings
[galera]
wsrep_on=ON
wsrep_provider=/usr/lib64/galera/libgalera_smm.so #galera的库文件的地址
wsrep_cluster_address=“gcomm://192.168.30.148,192.168.30.149,192.168.30.150” #各节点的ip
wsrep_node_name=node1 #节点主机名
wsrep_node_address=192.168.30.148 #节点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-node2、mariadb-node3,注意要把 wsrep_node_name 和 wsrep_node_address 改成相应节点的 hostname 和 ip。
123456789101112131415161718192021222324252627
7.启用集群服务
启动 MariaDB Galera Cluster 服务:
第一次启动选定一台要用初始化:
[[email protected] ~]# mysqld_safe --wsrep_cluster_address=gcomm://192.168.30.148,192.168.30.149,192.168.30.150
190515 19:01:26 mysqld_safe Logging to ‘/var/lib/mysql/node-1.err’.
190515 19:01:26 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
123
注意:一定要等待执行完成
[[email protected] ~]# /bin/galera_new_cluster
1
剩余两节点启动方式为:
[[email protected] ~]# systemctl start mariadb
查看端口状态:(集群服务使用了4567wsrep协议端口和3306数据库端口))
[[email protected] ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 :22 :
LISTEN 0 128 :4567 :
LISTEN 0 80 :::3306 :::
LISTEN 0 128 :::22 :::
12345678910
8.验证集群状态
登录数据库
[[email protected] ~]# mysql -uroot -p
Enter password:
12
查看是否启用galera插件
MariaDB [(none)]> show status like “wsrep_ready”;
±--------------±------+
| Variable_name | Value |
±--------------±------+
| wsrep_ready | ON |
±--------------±------+
1 row in set (0.003 sec)
1234567
目前集群机器数
MariaDB [(none)]> show status like “wsrep_cluster_size”;
±-------------------±------+
| Variable_name | Value |
±-------------------±------+
| wsrep_cluster_size | 3 |
±-------------------±------+
1 row in set (0.001 sec)
1234567
查看集群状态
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 | 1 |
| wsrep_cert_deps_distance | 0.000000 |
| wsrep_cert_index_size | 0 |
| wsrep_cert_interval | 0.000000 |
| wsrep_cluster_conf_id | 3 |
| wsrep_cluster_size | 3 #集群成员数 |
| wsrep_cluster_state_uuid | e956dd7e-7700-11e9-b509-669feea205a8 #UUID 集群唯一标记 |
| wsrep_cluster_status | Primary #主服务器 |
| wsrep_cluster_weight | 3 |
| 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 | e954a61e-7700-11e9-ad2e-e6f05252729c |
| wsrep_incoming_addresses | 192.168.30.149:3306,192.168.30.150:3306,192.168.30.148:3306#连接中的数据库 |
| wsrep_last_committed | 0 #sql 提交记录 |
| wsrep_local_bf_aborts | 0 #从执行事务过程被本地中断 |
| wsrep_local_cached_downto | 18446744073709551615 |
| wsrep_local_cert_failures | 0 #本地失败事务 |
| wsrep_local_commits | 0 #本地执行的sql |
| wsrep_local_index | 2 |
| 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 | e956dd7e-7700-11e9-b509-669feea205a8 |
| wsrep_open_connections | 0 |
| wsrep_open_transactions | 0 |
| wsrep_protocol_version | 9 |
| wsrep_provider_name | Galera |
| wsrep_provider_vendor | Codership Oy [email protected] |
| wsrep_provider_version | 25.3.26(r3857) |
| wsrep_ready | ON #插件是否应用中 |
| wsrep_received | 10 #数据复制接收次数 |
| wsrep_received_bytes | 758 |
| 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 |
±-----------------------------±------------------------------------------------------------+
61 rows in set (0.001 sec)
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
查看连接的主机
MariaDB [(none)]> show status like “wsrep_incoming_addresses”;
±-------------------------±------------------------------------------------------------+
| Variable_name | Value |
±-------------------------±------------------------------------------------------------+
| wsrep_incoming_addresses | 192.168.30.149:3306,192.168.30.150:3306,192.168.30.148:3306 |
±-------------------------±------------------------------------------------------------+
1 row in set (0.001 sec)
1234567
9.测试
1)在node1上创建数据库
MariaDB [(none)]> create database glar;
Query OK, 1 row affected (0.011 sec)
12
在node2和node3上可以看到
2)在node2上的glar库里创建表
MariaDB [(none)]> use glar
Database changed
MariaDB [glar]> create table test_table(id int ,name varchar(20));
Query OK, 0 rows affected (0.020 sec)
MariaDB [glar]> show tables;
±---------------+
| Tables_in_glar |
±---------------+
| test_table |
±---------------+
1 row in set (0.001 sec)
123456789101112
在node2和node3上可以看到
3)在test_table表里插入数据
MariaDB [glar]> insert into test_table values(1,‘xiaoming’);
Query OK, 1 row affected (0.010 sec)
MariaDB [glar]> select * from test_table;
±-----±---------+
| id | name |
±-----±---------+
| 1 | xiaoming |
±-----±---------+
1 row in set (0.004 sec)
12345678910
在node2和node3上可以看到