基于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。