mysql數據庫實現主從復制
阿新 • • 發佈:2019-05-06
建議 pac ast port -- count my.cnf 主從同步 0 rows 服務器準備
主服務器:192.168.93.103
從服務器:192.168.93.102
主服務器操作
修改配置文件
[[email protected] ~]#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 [email protected]‘192.168.93.%‘identified by ‘centos‘;
查看主服務器的二進制日誌,記錄要復制的位置
[[email protected] ~]#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服務器?
問題描述:如果一臺mysql主服務器已經運行了一段時間,主服務器的數據量非常大,而主從復制時間會很長,對主服務器壓力過大
解決辦法:先在主服務器上創建完全備份,還原到從服務器上後,再做主從復制
完全備份數據庫
[[email protected] ~]#mysqldump -A --single-transaction -F --master-data=1 > /data/backup/all.sql
查看記錄二進制日誌中要復制的位置信息
[[email protected] ~]#vim /data/backup/all.sql CHANGE MASTER TO MASTER_LOG_FILE=‘mysql-bin.000018‘, MASTER_LOG_POS=245;
在從服務器需要做的操作:
修改配置文件
將主服務器完全備份的數據庫文件all.sql拷貝至從服務器
[[email protected] ~]#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;
修改為:
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文件
[[email protected] ~]# mysql < all.sql
啟動主從同步線程
MariaDB [(none)]> start slave;
從服務器操作
修改配置文件
[[email protected] ~]# 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
mysql數據庫實現主從復制