Mysql Master slave复制

 配置的步骤(mysql56为例):


master数据库:

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

 

Slave数据库

#########开始,有这项就修改成自己对应的,没有就添加

[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
 
#########结束


配置slave对应的master数据库

Root登录执行:

change master tomaster_host='192.168.1.33',

master_port=3306,master_user='jk',master_password='密码'; 


开启slave:
Start slave;

  

主从同步检查
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,才表明状态正常。


如果主服务器已经存在应用数据,则在进行主从复制时,需要做以下处理:
(1)主数据库进行锁表操作,不让数据再进行写入动作
mysql> FLUSH TABLES WITH READ LOCK;
 
(2)查看主数据库状态
mysql> show master status;
 
(3)记录下 FILE 及 Position 的值。
将主服务器的数据文件(整个/opt/mysql/data目录)复制到从服务器,建议通过tar归档压缩后再传到从服务器解压。
 
(4)取消主数据库锁定

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.(需要权限)

 

大拿demo:

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; 


(Urghh...I see no way to embed code into these posts. Hence the ugly "____"thing to indicate indentation. I suggest copying into something with anon-proportinal font, to make it easier to read)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dingsai88

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值