##創建兩臺版本相同的mysql
1.主節點修改配置文件 /etc/mysql/my.cf
log-bin=master-bin #啓用二進制日誌 默認可以不改
# binary logging format - mixed recommended
binlog_format=mixed
# required unique id between 1 and 2^32 - 1
# defaults to 1 if master-host is not set
# but will not function as a master if omitted
server-id = 1 #serverid 默認可以不改
修改後重啓數據庫
2.mysql主服務器創建一個只用於複製的用戶:
MariaDB [(none)]> grant replication client,replication slave on *.* to 'repluser'@'192.168.%.%' identified by 'replpass';
MariaDB [(none)]> flush privileges;
3.修改從節點服務器配置文件:
# Replication Master Server (default)
# binary logging is required for replication
#log-bin=mysql-bin #關閉二進制
# binary logging format - mixed recommended
binlog_format=mixed
# required unique id between 1 and 2^32 - 1
# defaults to 1 if master-host is not set
# but will not function as a master if omitted
server-id = 20 #修改id編號
relay-log = relay-bin #添加中繼服務日誌
read-only = on #添加只讀選項
MariaDB [(none)]> show global variables like '%only%'; 查看只讀參數
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| read_only | OFF |
+---------------+-------+
1 row in set (0.00 sec)
MariaDB [(none)]>
4.然後修改數據庫中繼配置:
MASTER_HOST = 'host_name' 主節點地址
MASTER_USER = 'user_name' 有複製權限用戶名稱
MASTER_PASSWORD = 'password' 有複製權限用戶的密碼
master_heartbeat_period=2;心跳信息時間間隔
master_log_file='master-bin.000001 主服務器二進制日誌文件 # 查看主節點當前二進制日誌文件位置
MariaDB [mysql]> show master status;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000001 | 497 | | |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
master_log_pos=497 主服務器二進制日誌開始位置
MariaDB [(none)]> change master to master_host='192.168.1.146',master_user='repluser',master_password='replpass',master_log_file='master-bin.000001',master_log_pos=497,master_connect_retry=5,master_heartbeat_period=2;
Query OK, 0 rows affected (0.06 sec)
創建完成後已經成功了;查看從節點狀態:
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.146
Master_User: repluser
Master_Port: 3306
Connect_Retry: 5
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 497
Relay_Log_File: relay-bin.000003
Relay_Log_Pos: 530
Relay_Master_Log_File: master-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: 497
Relay_Log_Space: 818
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
1 row in set (0.00 sec)
兩個主要線程是否啓動:如果沒啓動執行 start slave 命令
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
5.ok配置完成,查看是否有日誌:
[root@localhost mydata]# ll
total 29776
-rw-rw----. 1 mysql mysql 16384 Nov 23 19:44 aria_log.00000001
-rw-rw----. 1 mysql mysql 52 Nov 23 19:44 aria_log_control
-rw-rw----. 1 mysql mysql 18874368 Nov 23 19:44 ibdata1
-rw-rw----. 1 mysql mysql 5242880 Nov 23 19:44 ib_logfile0
-rw-rw----. 1 mysql mysql 5242880 Nov 23 19:04 ib_logfile1
-rw-rw----. 1 mysql mysql 5 Nov 23 19:44 localhost.pid
-rw-rw----. 1 mysql mysql 82 Nov 23 20:05 master.info
drwx------. 2 mysql root 4096 Nov 23 19:03 mysql
-rw-rw----. 1 mysql mysql 28229 Nov 23 19:03 mysql-bin.000001
-rw-rw----. 1 mysql mysql 1038814 Nov 23 19:03 mysql-bin.000002
-rw-rw----. 1 mysql mysql 264 Nov 23 19:44 mysql-bin.000003
-rw-rw----. 1 mysql mysql 57 Nov 23 19:04 mysql-bin.index
drwx------. 2 mysql mysql 4096 Nov 23 19:03 performance_schema
-rw-rw----. 1 mysql mysql 245 Nov 23 20:05 relay-bin.000001
-rw-rw----. 1 mysql mysql 19 Nov 23 20:05 relay-bin.index
-rw-rw----. 1 mysql mysql 43 Nov 23 20:05 relay-log.info
drwx------. 2 mysql root 4096 Nov 23 19:03 test
[root@localhost mydata]#
6.主節點創建庫或表查看從節點是否有複製;
MariaDB [mydb]> create database mydb;
MariaDB [mydb]> create table tb1(id int);
雙主模式配置:
主主:
互爲主從:
1、數據不一致;
2、自動增長id
定義一個節點使用奇數id
auto_increment_offset=1
auto_increment_increment=2
定義另一個節點使用偶數id
auto_increment_offset=2
auto_increment_increment=2
(1) 各自使用不同的server id
(2) 都啓用binlog和relay log
(3) 定義自動增長的id字段的增長方式
(4) 都授權有複製權限的用戶賬號
(5) 各自把對方指定爲主服務器
節點1 my.cf配置:
log-bin=master-bin
relay-log=relay-bin
auto_increment_offset=1
auto_increment_increment=2
節點2 my.cf配置:
server-id = 21
log-bin=master-bin
relay-log=relay-bin
auto_increment_offset=1
auto_increment_increment=2
兩個節點創建可以複製權限的用戶:
grant replication slave,replication client on *.* to 'repluser'@'192.168.%.%' identified by 'replpass';
查看兩個節點的二進制信息:
節點一:
MariaDB [(none)]> show master status;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000003 | 507 | | |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
MariaDB [mydb]> change master to master_host='192.168.1.109',master_user='repluser',master_password='replpass',master_log_file='master-bin.000001',master_log_pos=507;
啓動進程:start slave;
節點二:
MariaDB [(none)]> show master status;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000001 | 507 | | |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
MariaDB [mydb]> change master to master_host='192.168.1.146',master_user='repluser',master_password='replpass',master_log_file='master-bin.000003',master_log_pos=507;
啓動進程:start slave;
配置完成創建數據查看狀態:
MariaDB [mydb]> create table tb1 (id int unsigned not null auto_increment primary key,name char(30));
MariaDB [mydb]> insert into tb1 (name) values ('tom'),('jerry');
複製如何開始?
主節點運行很長時間,且已經有一定規模的數據,如何啓動複製?
在主節點做一個完全備份,並記錄二進制日誌文件及位置;
在從節點恢復此完全備份,並在啓動複製時從記錄的二進制日誌文件和位置開始;
複製時應該注意的問題:
1、如何限制從服務器只讀?
在從服務器啓動read_only;但僅對非具有SUPER權限的用戶有效;
阻止所有用戶 :MariaDB> FLUSH TABLES WITH READ LOCK;
2、如何保證主從複製時的事務安全?
在master節點啓用參數:
sync_binlog = on #讓從服務器也對儘早的讀到二進制日誌文件
如果用到的爲InnoDB存儲引擎:
innodb_flush_logs_at_trx_commit #表示innodb事務提交時立即刷寫到數據磁盤中
innodb_support_xa=on #表示讓innodb支持分佈式事務
在slave節點:
skip_slave_start #表示跳過slave啓動
主節點(不考慮io情況下)可以啓動以下選項:
sync_master_info = 1 1
從節點:
sync_relay_log = 1
sync_relay_log_info = 1