環境
主庫:mysqld Ver 5.7.26 for Linux on x86_64 (MySQL Community Server (GPL)) Red Hat 4.8.5-11
從庫:mysqld Ver 5.7.26-0ubuntu0.16.04.1 for Linux on x86_64 ((Ubuntu))
原理
場景以及問題
權限
1.最好兩台數據庫都搞成這個樣子的權限
mysql -u root -p GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION flush privileges ; show grants for root; update mysql.user set authentication_string = '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' where user='root'; exit; service mysql restart;
主庫配置及其操作
1.主庫配置
server-id = 10 log_bin = /var/log/mysql/mysql-bin.log expire_logs_days = 10 max_binlog_size = 100M #binlog_do_db = include_database_name #binlog_ignore_db = include_database_name # # * InnoDB # InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/. # Read the manual for more InnoDB related options. There are many! innodb_flush_log_at_trx_commit=1 sync_binlog=1
2.重啟,This procedure uses FLUSH TABLES WITH READ LOCK
, which blocks COMMIT
operations forInnoDB
tables.
mysql> FLUSH TABLES WITH READ LOCK;
3.記錄二進制文件位置
mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000003 | 154 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
4.主庫導出數據
mysqldump -uroot -p --single-transaction --all-databases --master-data > dbdump.db
5. 解鎖事務鎖,On the master, released the read lock:
mysql> UNLOCK TABLES;
從庫配置及其操作
1.配置
server-id = 11
2.倒入主庫轉儲db文件
mysql -u root -p < dbdump.db
3.指向主庫,其文件名寫錯了以后mysql-bin.000002,照樣能復制
CHANGE MASTER TO -> MASTER_HOST='192.168.43.114', -> MASTER_USER='root', -> MASTER_PASSWORD='123456', -> MASTER_LOG_FILE='mysql-bin.000002', -> MASTER_LOG_POS=154;
4.啟動從庫復制線程
start slave;
5.測試成功
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.43.114 Master_User: root Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 3765 Relay_Log_File: bogon-relay-bin.000003 Relay_Log_Pos: 3978 Relay_Master_Log_File: mysql-bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes