基於windows平臺,mysql版本mysql-5.7.14-winx64,步驟以下
###1.建立兩個mysql實例
詳見http://www.javashuo.com/article/p-qktxkfpy-de.html
###2.查看mysql官網
http://dev.mysql.com/doc/refman/5.7/en/replication.html
###3.配置master
http://dev.mysql.com/doc/refman/5.7/en/replication-howto-masterbaseconfig.html
個人配置以下html
[mysqld] # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin # These are commonly set, remove the # and set as required. basedir = E:\mysql-5.7.14-winx64\mysql-5.7.14-winx64 datadir = E:\mysql-5.7.14-winx64\mysql-5.7.14-winx64\data port = 3306 server_id = 1 #開啓二進制日誌 log-bin= master-bin
備註:咱們下載的mysql.zip,默認配置文件是my-default.ini,可是mysql文檔描述以下mysql
To configure the binary log and server ID options, shut down the MySQL server and edit the my.cnf or my.ini file. Within the [mysqld] section of the configuration file, add the log-bin and server-id options. If these options already exist, but are commented out, uncomment the options and alter them according to your needs. For example, to enable binary logging using a log file name prefix of mysql-bin, and configure a server ID of 1, use these lines:
我試過my-default.ini,可是沒有成功,所以這裏手動的將配置文件的名稱改爲my.ini。master數據庫改了,slave數據庫也要修改,slave修改了以後,mysql2啓動不起來,同時要修改windows註冊表裏面的信息。
###4.啓動master
####1.啓動mastersql
E:\mysql-5.7.14-winx64\mysql-5.7.14-winx64\bin>net start mysql MySQL 服務正在啓動 . MySQL 服務已經啓動成功。
####2.登錄master數據庫
E:\mysql-5.7.14-winx64\mysql-5.7.14-winx64\bin>mysql -u root -p777888999
####3.查看masterwindows
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec) mysql>
####4.查看用戶ide
mysql> select user,host from mysql.user; +-----------+-----------+ | user | host | +-----------+-----------+ | lxl | % | | panda | % | | mysql.sys | localhost | | root | localhost | +-----------+-----------+ 4 rows in set (0.00 sec) mysql>
####5.新建用戶
因爲slave鏈接master須要一個用戶,因此,咱們這裏新建一個用戶,而且賦予它權限。測試
mysql> create user zhangsan identified by '123456'; Query OK, 0 rows affected (0.03 sec) mysql> select user,host from mysql.user; +-----------+-----------+ | user | host | +-----------+-----------+ | lxl | % | | panda | % | | zhangsan | % | | mysql.sys | localhost | | root | localhost | +-----------+-----------+ 5 rows in set (0.00 sec) mysql>
####6.賦予新建用戶權限ui
mysql> grant replication slave on *.* to zhangsan; Query OK, 0 rows affected (0.03 sec) mysql>
賦予 zhangsan這個用戶slave權限,*.*表示master全部的數據庫下面全部的表,實際項目中,應該是指定數據庫。
###5.配置slave
mysql官網
http://dev.mysql.com/doc/refman/5.7/en/replication-setup-slaves.html
個人配置以下.net
[mysqld] # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin # These are commonly set, remove the # and set as required. basedir = F:\mysql2\mysql-5.7.14-winx64\mysql-5.7.14-winx64 datadir = F:\mysql2\mysql-5.7.14-winx64\mysql-5.7.14-winx64\data port = 3307 server_id = 2 relay_log=slave-relay-bin
####1..啓動/登錄/進入slave日誌
F:\mysql2\mysql-5.7.14-winx64\mysql-5.7.14-winx64\bin>net start mysql2 mysql2 服務正在啓動 . mysql2 服務已經啓動成功。 F:\mysql2\mysql-5.7.14-winx64\mysql-5.7.14-winx64\bin>mysql -P3307 -u root -p Enter password:
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec) mysql>
這裏能夠發現,slave與master是同樣的,都是新安裝後的初始狀態。 ####2.配置slave歸屬master
1.獲取master二進制文件名稱與位置
mysql> show master status; +-------------------+----------+--------------+------------------+-------------- -----+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid _Set | +-------------------+----------+--------------+------------------+-------------- -----+ | master-bin.000009 | 1419 | | | | +-------------------+----------+--------------+------------------+-------------- -----+ 1 row in set (0.00 sec) mysql>
2.配置slave歸屬master
mysql> change master to -> master_host='192.168.2.67', -> master_user='zhangsan', -> master_password='123456', -> master_log_file='master-bin.000009', -> master_log_pos=1419; Query OK, 0 rows affected, 2 warnings (0.18 sec) mysql>
3.開啓slave
mysql> start slave; Query OK, 0 rows affected (0.04 sec)
4.查看slave狀態
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.2.67 Master_User: zhangsan Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000009 Read_Master_Log_Pos: 1419 Relay_Log_File: slave-relay-bin.000002 Relay_Log_Pos: 321 Relay_Master_Log_File: master-bin.000009 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: 1419 Relay_Log_Space: 528 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: 1 Master_UUID: ed77c8ba-7496-11e6-8100-c86000d039ef Master_Info_File: F:\mysql2\mysql-5.7.14-winx64\mysql-5.7.14-winx64 \data\master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for more up dates 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) ERROR: No query specified mysql>
有上述信息可知:
1.Slave_IO_State:等待master發送事件
做用:監控master二進制日誌是否有變動
2.Slave_IO_Running:正在運行
做用:將master二進制日誌變動部分取回slave
3.Slave_SQL_Running:正在運行
做用:在slave上重現變動部分,即實現了數據同步
###6.測試
####1.在master上建立數據庫
mysql> create database test; Query OK, 1 row affected (0.04 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test | +--------------------+ 5 rows in set (0.00 sec) mysql>
####2.查看slave
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test | +--------------------+ 5 rows in set (0.00 sec) mysql>
發現已經同步了。接着測試下數據。
####3.master建表,添加數據
mysql> create table t_user(id int(4),name varchar(100)); Query OK, 0 rows affected (0.41 sec) mysql>
添加數據
mysql> insert into t_user(id,name) values(1000,'zhangsan'); Query OK, 1 row affected (0.16 sec) mysql>
####4.查看slave
mysql> use test Database changed mysql> select * from t_user; +------+----------+ | id | name | +------+----------+ | 1000 | zhangsan | +------+----------+ 1 row in set (0.01 sec) mysql>
如上信息,代表已經同步成功了。
注意:若是slave不須要與master同步了,必定要
mysql> stop slave; Query OK, 0 rows affected (0.08 sec) mysql>
不然,下次設置該slave的時候會出現以下錯誤
mysql> change master to -> master_host='192.168.2.67', -> master_user='zhangsan', -> master_password='123456', -> master_log_file='master-bin.000009', -> master_log_pos=1419; ERROR 3021 (HY000): This operation cannot be performed with a running slave io t hread; run STOP SLAVE IO_THREAD FOR CHANNEL '' first.
固然解決辦法很明顯,先stop slave,在設置slave。