配置的步骤(mysql56为例):
my.ini 5.6 修改
C:\Documents and Settings\AllUsers\Application Data\MySQL\my.ini
#########开始,有这项就修改成自己对应的,没有就添加 [client] port=3306 default-character-set=utf8 [mysqld] port=3306 #character_set_server=utf8 数据库字符集 character_set_server=utf8 #目录 basedir=C:\ProgramFiles\MySQL\mysql-5.6.16-win32 #目录下data目录,必须为data目录 datadir=C:\ProgramFiles\MySQL\mysql-5.6.16-win32\data #sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES这个有问题,在创建完新用户登录时报错 sql_mode=NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION #主服务器的配置 #开启二进制日志 log-bin=master-bin #使用二进制日志的索引文件 log-bin-index=master.bin.index #为服务器添加唯一的编号 一般为机器IP后两位192.168.1.33 server-id=33 #########结束
>mysql -u root –p
--创建用户
mysql> create user jk;
--添加用户的权限
mysql> grant replication slave on *.* to jk identified by '密码';
重启mysql :
netstop mysql56
netstart mysql56
#########开始,有这项就修改成自己对应的,没有就添加 [client] port=3306 default-character-set=utf8 [mysqld] port=3306 #character_set_server=utf8 数据库字符集 character_set_server=utf8 basedir=C:\ProgramFiles\MySQL\mysql-5.6.16-win32 datadir=C:\ProgramFiles\MySQL\mysql-5.6.16-win32\data #sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES这个有问题,在创建完新用户登录时报错 sql_mode=NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION ##192.168.1.22 声明自己的ID The master and each slave must be configured witha unique ID server-id=22 relay-log=slave-relay-log-bin relay-log-index=slave-relay-log-bin.index #########结束
change master tomaster_host='192.168.1.33',
master_port=3306,master_user='jk',master_password='密码';
主从同步检查 mysql> show slave status\G ============================================== **************** 1. row ******************* Slave_IO_State: Master_Host: 192.168.1.33 Master_User: rep1 Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000005 Read_Master_Log_Pos: 415 Relay_Log_File: localhost-relay-bin.000008 Relay_Log_Pos: 561 Relay_Master_Log_File: mysql-bin.000005 Slave_IO_Running: YES Slave_SQL_Running: YES Replicate_Do_DB: ……………省略若干…………… Master_Server_Id: 1 1 row in set (0.01 sec) ============================================== 其中Slave_IO_Running 与 Slave_SQL_Running 的值都必须为YES,才表明状态正常。
mysql> UNLOCK TABLES;
master数据库执行:show master status \G;
查看File和Position值
mysql> show master status \G *************************** 1. row *************************** File: master-bin.000004 Position: 1018 Binlog_Do_DB: Binlog_Ignore_DB: Executed_Gtid_Set: 1 row in set (0.00 sec)
slave数据库执行:
停止slave;
change master to master_log_file='master-bin.000004', master_log_pos=1018;
启动 start slave;
mysql> stop slave; Query OK, 0 rows affected (0.02 sec) mysql> change master to master_log_file='master-bin.000004', master_log_pos=1018; Query OK, 0 rows affected (0.06 sec) mysql> start slave; Query OK, 0 rows affected (0.00 sec) mysql> use proxy1
在master数据库创建数据库,添加数据
在slave数据库可以查询到
http://dev.mysql.com/doc/refman/5.7/en/replication-howto.html
官网步骤:
There are somegeneric tasks that are common to all replication setups:
· On the master, you must enable binary logging andconfigure a unique server ID. This might require a server restart. See Section 17.1.1.1, “Settingthe Replication Master Configuration”.(master开启binary log和server ID重启)
· On each slave that you want to connect to the master, youmust configure a unique server ID. This might require a server restart. See Section 17.1.1.2, “Settingthe Replication Slave Configuration”.(slave配置serverID 重启)
· Optionally, create a separate user for your slaves to useduring authentication with the master when reading the binary log forreplication. See Section 17.1.1.3, “Creating a User for Replication”.(创建用于复制的用户)
· Before creating a data snapshot or starting thereplication process, you should record the position of the binary log on themaster. You will need this information when configuring the slave so that theslave knows where within the binary log to start executing events. See Section 17.1.1.4,“Obtaining the Replication Master Binary Log Coordinates”.(master的binlog日志)
· If you already have data on your master and want to useit to synchronize your slave, you need to create a data snapshot to copy thedata to the slave. The storage engine you are using has an impact on how youcreate the snapshot. When you are using MyISAM, you must stopprocessing statements on the master to obtain a read-lock, then obtain itscurrent binary log coordinates and dump its data, before permitting the masterto continue executing statements. If you do not stop the execution ofstatements, the data dump and the master status information will not match,resulting in inconsistent or corrupted databases on the slaves. For moreinformation on replicating a MyISAM master, see Section 17.1.1.4, “Obtainingthe Replication Master Binary Log Coordinates”. If you are using InnoDB,you do not need a read-lock and a transaction that is long enough to transferthe data snapshot is sufficient. For more information, see Section 14.17, “InnoDB and MySQL Replication”.(存储引擎不一样的操作,锁表等。)
· Configure the slave with settings for connecting to themaster, such as the host name, login credentials, and binary log file name andposition. (Slave配置连接master的信息)See Section 17.1.1.8, “Settingthe Master Configuration on the Slave”.
Note
Certain steps within the setup processrequire the SUPER privilege. If youdo not have this privilege, it might not be possible to enable replication.(需要权限)
It is possible to sync a database to a masterwhile the master is in active use. Commands starting "M:" are run onthe master, "S:" are run in the slave. Until the end, the slave stateis undefined.
Thisworks for InnoDB tables, if you have set transactionality to REPEATABLE-READ.
M:begin; M:flush tables with read lock; M:show master status; M:show databases; M:(for each db:) M:__show tables in $db; M:__(for each table:) M:____select 1 from $db.$table limit 1; M:unlock tables; M: settime_zone = '+00:00'; S: setforeign_key_checks = 0; S:stop slave io_thread; S:stop slave; S:reset slave; S:reset master; S: settime_zone = '+00:00'; _:(for each database:) S:__show databases like '$db'; _:__(if it does not exist:) S:____create database $db; _:__(for each table:) M:____show create table $db.$table; S:____show create table $db.$table; _:____(if different:) S:______drop table $db.$table; _:____(if different or not on slave:) S:______create table $db.$table .... ; S:____delete from $db.$table; M:____select * from $db.$table; S:____insert into $db.$table values ([...from above...]); S:__show tables in $db; _:__(for each table that shouldn't be there:) S:____drop table $db.$table; S:show databases; _:(for each db that shouldn't be there:) S:__drop database $db; S:change master to [...details from show master above...]; S:start slave io_thread; S:start slave; M:rollback;