mysql数据库实现主从复制
生活随笔
收集整理的這篇文章主要介紹了
mysql数据库实现主从复制
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
服務器準備
主服務器:192.168.93.103
從服務器:192.168.93.102
主服務器操作
修改配置文件
[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';查看主服務器的二進制日志,記錄要復制的位置
[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服務器?
問題描述:如果一臺mysql主服務器已經運行了一段時間,主服務器的數據量非常大,而主從復制時間會很長,對主服務器壓力過大
解決辦法:先在主服務器上創建完全備份,還原到從服務器上后,再做主從復制
完全備份數據庫
[root@CentOS7 ~]#mysqldump -A --single-transaction -F --master-data=1 > /data/backup/all.sql查看記錄二進制日志中要復制的位置信息
[root@CentOS7 ~]#vim /data/backup/all.sql CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000018', MASTER_LOG_POS=245;在從服務器需要做的操作:
修改配置文件
將主服務器完全備份的數據庫文件all.sql拷貝至從服務器
修改/data/backup/all.sql文件里完全備份位置語句:
CHANGE MASTER TO 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 eventMaster_Host: 192.168.93.103Master_User: repluserMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000017Read_Master_Log_Pos: 399Relay_Log_File: mariadb-relay-bin.000002Relay_Log_Pos: 683Relay_Master_Log_File: mysql-bin.000017Slave_IO_Running: Yes //主從復制的線程IOSlave_SQL_Running: Yes //主從復制的線程SQLReplicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 399Relay_Log_Space: 979Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_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: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 103 1 row in set (0.00 sec)ERROR: No query specified轉載于:https://blog.51cto.com/14233815/2390068
總結
以上是生活随笔為你收集整理的mysql数据库实现主从复制的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【robot framework日志】更
- 下一篇: linux cmake编译源码,linu