因此:php
1.主服務器mysql
創建2進制日誌,每產生語句或磁盤變化,寫進日誌binlog。web
2.從服務器sql
讀取主服務器binlog 造成relaylog(中繼日誌) 經語法分析變成從服務器的數據。數據庫
3.受權bash
因爲數據都很私密,因此主服務器要受權複製帳號 replication。服務器
從服務器利用複製帳號來堅挺主服務器的日誌。ide
操做測試
1. 首先編輯主服務器mysql的配置文件 my.cnf (apt-get 安裝位置 /etc/mysql/mysql.conf.d/ mysqld.cnf)spa
# The following can be used as easy to replay backup logs or for replication. # note: if you are setting up a replication slave, see README.Debian about # other settings you may need to change. server-id = 147 指定server_id log_bin = /var/log/mysql/mysql-bin.log 指定bin-log文件位置 expire_logs_days = 10 設置過時時間 max_binlog_size = 100M 設置文件大小上限 #statement row mixed binlog-format=mixed 設置匹配模式 此處選擇mixed模式
2. 編輯從服務器的my.cnf
[mysqld] port = 3306 server-id=69 指定server-id relay-log=mysql-relay 指定relay-log
3.重啓各自的mysql
sudo /etc/init.d/mysql restart
4.主服務器master設置複製受權帳號
grant replication client,replication slave on *.* to 'rep1'@'192.168.%.%'//受權給192.168打頭的 identified by 'rep1';//密碼
mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000001 | 154 | | | | +------------------+----------+--------------+------------------+-------------------+
5.從服務器 給你一個帳號去連你的master
change master to master_host='192.168.5.199',//主服務器地址 master_user='rep1',//受權用戶名 master_password='rep1',//受權帳號 master_log_file='mysql-bin.000001', //bin-log文件 master_log_pos=154; //同步的位置
6.重置從服務器 並啓動slave
mysql>reset slave
mysql>start slave
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.147 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000008 Read_Master_Log_Pos: 154 Relay_Log_File: mysql-relay.000002 Relay_Log_Pos: 320 Relay_Master_Log_File: mysql-bin.000008 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: 154 Relay_Log_Space: 523 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: 147 Master_UUID: dad611d4-3625-11e8-a11f-080027c354d3 Master_Info_File: D:\wamp64\bin\mysql\mysql5.7.14\data\master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec)
1.先查看主服務器,從數據庫信息
mysql> show databases; ----->master +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | twusa | +--------------------+ 5 rows in set (0.01 sec)
mysql> show databases; ----->slave
+--------------------+
| Database |
+--------------------+
| information_schema |
| blog |
| ecshop |
| mysql |
| performance_schema |
| shop |
| sys |
| test |
+--------------------+
8 rows in set (0.02 sec)
2.主服務器建立新的數據庫test_master 並查看bin-log大小
-rw-r----- 1 mysql mysql 322 4月 9 16:47 mysql-bin.000009
mysql> create database test_123; Query OK, 1 row affected (0.01 sec)
-rw-r----- 1 mysql mysql 493 4月 9 16:52 mysql-bin.000009
這個時候回過頭來看從庫
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | blog | | ecshop | | mysql | | performance_schema | | shop | | sys | | test | | test123 |--------->發現多了這一條 | twusa | +--------------------+ 10 rows in set (0.00 sec)
至此主從複製完成,撒花 ~
1.啓動slave 一直顯示正在connecting
2.Got fatal error 1236 from master when reading data from binary log: 'unknown error reading log event on the master; the first event '' at 4, the last event read from '/opt/3306/mysql-bin.000008' at 154, the last byte read from '/var/log/mysql-bin.000008' at 155.'
1.查看bin-log
sudo mysqlbinlog mysql-bin.000009 > masterbin000009.log