windows mysql主从数据库环境搭建(windows7+mysql 5.7)
数据库主从备份主要原理就是slave数据库根据master数据库进行的增删改操作留下的日志文件,在slave数据库里再执行一遍。
1.准备两台win7机器或虚拟机,并安装好mysql5.7数据库 Ip分别为192.168.0.127(主:Master)与192.168.0.129(从:Slave)
在master与slave上分别创建两个相同的数据库test 及student表
2.Master数据库配置如下:
1).修改mysql安装目录下的my.ini(在data目录下)文件 在文件末尾添加如下配置:
server_id=1 # 全局唯一的ID
binlog-ignore-db=mysql #忽略系统提供的mysql数据库
log-bin=master-mysql-bin #开启log-bin功能并设置log-bin的名称为:master-mysql-bin,可自定义
2).重启mysql服务
3).开通其他从服务器访问Master的权限
进入mysql命令窗口
帐号:root, 密码:123456 ‘192.168.0.129’为Salve机器的IP,即;允许129机器通过root 123456 访问Master
mysql>grant replication slave,replication client on *.* to 'root'@'192.168.0.129' identified by '123456';
Query OK, 0 rows affected
4).参看授权信息
mysql>show master status;
+-------------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------------+----------+--------------+------------------+-------------------+
| master-mysql-bin.000001 | 481 | | mysql | |
+-------------------------+----------+--------------+------------------+-------------------+
1 row in set
master-mysql-bin.000001 与481在salve的机器上要用到
3.Slave数据库配置如下:
1).修改mysql安装目录下的my.ini(在data目录下)文件 在文件末尾添加如下配置:
server_id=2 # 全局唯一的ID 不可与Master的ID重复
binlog-ignore-db=mysql #忽略系统提供的mysql数据库
log-bin=salve-mysql-bin #开启log-bin功能并设置log-bin的名称为:salve-mysql-bin,可自定义
2).重启mysql服务
3).登录mysql数据库并连接master
mysql>change master to
>master_host='192.168.0.127',
>master_user='root',
>master_password='123456',
>master_log_file='master-mysql-bin.000001',
>master_log_pos=481;
Query OK, 0 rows affected
4).启动(如果 slave已经启动 先stop slave;再start slave;)
mysql>start slave;
Query OK, 0 rows affected
5).查看slave启动状态
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.127
Master_User: root
Master_Port: 3306
Connect_Retry: 30
Master_Log_File: master-mysql-bin.000001
Read_Master_Log_Pos: 481
Relay_Log_File: USER-20170303GQ-relay-bin.000002
Relay_Log_Pos: 327
Relay_Master_Log_File: master-mysql-bin.000001
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: 481
Relay_Log_Space: 544
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: 6
Master_UUID: d142c834-f833-11e6-bf56-708bcd7e3096
Master_Info_File: D:\MysqlData\MySQL Server 5.7\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)
如提示:
Slave_SQL_Running:Yes
则表明,启动成功。
6).测试
在Master的test数据库student表修改数据后,即可看到slave数据库的test里的student表也发生相同变化。
4.以上步骤为主从(master-slave)单向复制,如要主从双向复制(即master-master);只要在上述的Slave机器上对Master进行授权即可步骤如下:
1).开通其他从服务器访问Slave的权限
mysql>grant replication slave,replication client on *.* to 'root'@'192.168.0.127' identified by '123456';
2).查看slave机器的授权信息
mysql>show master status;
+-------------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------------+----------+--------------+------------------+-------------------+
| slave-mysql-bin.000001 | 502 | | mysql | |
+-------------------------+----------+--------------+------------------+-------------------+
1 row in set
3).在Master机器上链接salve机器,如下:
mysql>change master to
>master_host='192.168.0.129',
>master_user='root',
>master_password='123456',
>master_log_file='slave-mysql-bin.000001',
>master_log_pos=502;
Query OK, 0 rows affected
4).同理,查看查看slave启动状态;
在Master的test数据库student表修改数据后,即可看到slave数据库的test里的student表也发生相同变化。
同理:在Slave的test数据库student表修改数据后,即可看到Master数据库的test里的student表也发生相同变化。
即表明环境搭建成功!