Mariadb主從複製
環境配置:mysql
Master : 172.30.200.200sql
Slave : 172.30.200.204shell
架構圖,以下:數據庫
Master的配置:
1.binlog配置
[root@oradb u01]# su - mysql [mysql@oradb ~]$ mkdir -p /u01/data/binlog [mysql@oradb ~]$ vi /etc/my.cnf #*********** binlog related settings *********** log-bin = /u01/data/binlog/mysql-bin binlog_format= row binlog_cache_size=32m max_binlog_cache_size=64m max_binlog_size=512m server-id=30200
2.查看binlog的配置[可選看]
[root@oradb u01]# service mysqld restart [root@oradb u01]# mysql -uroot -predhat ## 查看binlog開啓 MariaDB [(none)]> show variables like '%log_bin%'; +---------------------------------+----------------------------------+ | Variable_name | Value | +---------------------------------+----------------------------------+ | log_bin | ON | | log_bin_basename | /u01/data/binlog/mysql-bin | | log_bin_compress | OFF | | log_bin_compress_min_len | 256 | | log_bin_index | /u01/data/binlog/mysql-bin.index | | log_bin_trust_function_creators | OFF | | sql_log_bin | ON | +---------------------------------+----------------------------------+ 7 rows in set (0.002 sec) ## 查看binlog的格式,是基於行的複製。 MariaDB [(none)]> show variables like '%binlog_format%'; +----------------------------+-------+ | Variable_name | Value | +----------------------------+-------+ | binlog_format | ROW | | wsrep_forced_binlog_format | NONE | +----------------------------+-------+ ## 查看最大的binlog size,到了512m切換一個。 MariaDB [(none)]> show variables like '%max_binlog_size%'; +-----------------+-----------+ | Variable_name | Value | +-----------------+-----------+ | max_binlog_size | 536870912 | +-----------------+-----------+ 1 row in set (0.001 sec)
3.建立replication用戶
CREATE USER 'repl'@'172.30.200.204' IDENTIFIED BY 'repl'; GRANT REPLICATION SLAVE ON *.* TO 'repl'@'172.30.200.204';
4.定位二進制位置
## 鎖定全部的表,保證數據一致性和完整性 MariaDB [(none)]> FLUSH TABLES WITH READ LOCK; Query OK, 0 rows affected (0.001 sec) MariaDB [(none)]> SHOW MASTER STATUS; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000001 | 667 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.000 sec)
5.使用mysqldump獲取數據庫快照
MariaDB [(none)]> FLUSH TABLES WITH READ LOCK; [root@oradb ~]# mysqldump -uroot -predhat --all-databases --master-data >dbdump.log MariaDB [(none)]> UNLOCK TABLES;
SLAVE的配置:
1.配置server-id
[root@oradb ~]# vi /etc/my.cnf ## 在[mysqld]做用域下面配置 server-id=30204
2.導入數據
若是是一個新庫,能夠不用導入數據。直接配置主從。架構
若是master已有數據,能夠導入以前的dump文件。命令以下;測試
shell> mysql -uroot -predhat < dbdump.log
3.開啓主從複製
CHANGE MASTER TO MASTER_HOST='172.30.200.200',MASTER_PORT = 3306,MASTER_USER = 'repl',MASTER_PASSWORD = 'repl',MASTER_LOG_FILE = 'mysql-bin.000001',MASTER_LOG_POS = 667;
4.開啓複製
MariaDB [(none)]> start slave; MariaDB [(none)]> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 172.30.200.200 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000002 Read_Master_Log_Pos: 342 Relay_Log_File: oradb-relay-bin.000003 Relay_Log_Pos: 641 Relay_Master_Log_File: mysql-bin.000002 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: 342 Relay_Log_Space: 1249 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: 30200 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: 0 Slave_Non_Transactional_Groups: 0 Slave_Transactional_Groups: 0 1 row in set (0.000 sec) ERROR: No query specified
數據測試
1.master端插入數據
MariaDB [(none)]> create database zsd; MariaDB [(none)]> use zsd; MariaDB [zsd]> create table test(id int,name varchar(20)); MariaDB [zsd]> insert into test values(1,"張盛東"); MariaDB [zsd]> commit;
2.slave端查詢數據
MariaDB [zsd]> select * from test; +------+-----------+ | id | name | +------+-----------+ | 1 | 張盛東 | +------+-----------+ 1 row in set (0.000 sec)