主服務器:192.168.93.103
從服務器:192.168.93.102node
[root@CentOS7 ~]#vim /etc/my.cnf [mysqld] server_id=103 //指定一個服務id,若是不寫這裏默認爲1 log_bin=/data/mysql/bin/mysql-bin //必須啓動二進制日誌 binlog_format=row //建議使用行row記錄日誌 innodb_file_per_table datadir=/var/lib/mysql
MariaDB [(none)]> grant replication slave on *.* to repluser@'192.168.93.%'identified by 'centos';
查看主服務器的二進制日誌,記錄要複製的位置mysql
[root@node-103 ~]#mysql -e 'show master logs'; +------------------+-----------+ | Log_name | File_size | +------------------+-----------+ | mysql-bin.000002 | 288 | | mysql-bin.000003 | 288 | | mysql-bin.000004 | 442 | | mysql-bin.000005 | 245 | +------------------+-----------+
問題描述:若是一臺mysql主服務器已經運行了一段時間,主服務器的數據量很是大,而主從複製時間會很長,對主服務器壓力過大sql
解決辦法:先在主服務器上建立徹底備份,還原到從服務器上後,再作主從複製數據庫
徹底備份數據庫vim
[root@CentOS7 ~]#mysqldump -A --single-transaction -F --master-data=1 > /data/backup/all.sql
查看記錄二進制日誌中要複製的位置信息centos
[root@CentOS7 ~]#vim /data/backup/all.sql CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000018', MASTER_LOG_POS=245;
在從服務器須要作的操做:
修改配置文件
將主服務器徹底備份的數據庫文件all.sql拷貝至從服務器服務器
[root@CentOS7 ~]#scp 192.168.93.102:/data/backup/all.sql /data/backup
修改/data/backup/all.sql文件裏徹底備份位置語句:
CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000018',MASTER_LOG_POS=245;
修改成:ide
vim /data/backup/all.sql CHANGE MASTER TO MASTER_HOST='192.168.93.103', MASTER_USER='repluser', MASTER_PASSWORD='centos', MASTER_PORT=3306, MASTER_LOG_FILE='mysql-bin.000018', MASTER_LOG_POS=245;
而後導入all.sql文件線程
[root@CentOS7 ~]# mysql < all.sql
啓動主從同步線程日誌
MariaDB [(none)]> start slave;
修改配置文件
[root@CentOS7 ~]# vim /etc/my.cnf [mysqld] server_id=102 //指定從服務器的服務id,不能不寫,默認爲1,此id不能與其餘服務器衝突 read_only=ON //設置數據庫只讀
使用有複製權限的用戶帳號鏈接至主服務器,配置同步信息
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.93.103', //遠程主服務器IP MASTER_USER='repluser', //主從複製的用戶名 MASTER_PASSWORD=’centos’, //密碼 MASTER_PORT=3306, //端口號 MASTER_LOG_FILE='mysql-bin.000017', //要複製的主服務器的二進制文件名 MASTER_LOG_POS=245; //要開始複製的位置
啓動複製線程
MariaDB [(none)]> start slave; Query OK, 0 rows affected (0.00 sec)
顯示線程列表
MariaDB [(none)]> show processlist; +----+-------------+-----------+------+---------+--------+-----------------------------------------------------------------------------+------------------+----------+ | Id | User | Host | db | Command | Time | State | Info | Progress | +----+-------------+-----------+------+---------+--------+-----------------------------------------------------------------------------+------------------+----------+ | 3 | root | localhost | NULL | Query | 0 | NULL | show processlist | 0.000 | | 6 | system user | | NULL | Connect | 264 | Waiting for master to send event | NULL | 0.000 | | 7 | system user | | NULL | Connect | -27302 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL | 0.000 | +----+-------------+-----------+------+---------+--------+-----------------------------------------------------------------------------+------------------+----------+ 3 rows in set (0.00 sec)
顯示主服務器的狀態信息
MariaDB [(none)]> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.93.103 Master_User: repluser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000017 Read_Master_Log_Pos: 399 Relay_Log_File: mariadb-relay-bin.000002 Relay_Log_Pos: 683 Relay_Master_Log_File: mysql-bin.000017 Slave_IO_Running: Yes //主從複製的線程IO Slave_SQL_Running: Yes //主從複製的線程SQL 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: 399 Relay_Log_Space: 979 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: 103 1 row in set (0.00 sec) ERROR: No query specified