mysql主从数据库配置

一、注意事项

1、从数据库mysql版本等于或大于主数据库Mysql版本


二、实验环境

1、主:windows7  mysql5.5

2、从:windows10 mysql5.6


三、主数据库配置(master)

参考文章: http://blog.csdn.net/qmhball/article/details/8233769
主机(master)配置:
1.修改mysql配置文件my.cnf
在[mysqld]标签下添加以下几行
[plain]  view plain  copy
  1. log-bin #开启二进制日志  
  2. server-id=id #主服务器id号  
  3. binlog-do-db=db_nameA #指定对db_nameA记录二进制日志  
  4. binlog-ignore-db=db_namB #指定不对db_namB记录二进制日志  
注意:
log-bin,server-id是配置文件中必须添加的内容。此时主服务器默认对所有数据库进行备份。如果需要特殊指明只对某个数据库进行备份或不备份,则可以加入binlog-do-db和binlog-ignore-db选项。有关(log-bin的详细说明见附录1)

在测试主机上,我们实际添加入如下内容:
[plain]  view plain  copy
  1. log-bin  
  2. server-id=1  
  3. binlog-do-db=test  

2.为从服务器添加mysql账户并配置权限
在主服务器上,必须为从服务器创建一个用来连接主服务器的用户,并设置replication slave权限。所用具体命令如下:
[sql]  view plain  copy
  1. grant replication slave  
  2. on *.*  
  3. to '帐号' @ '从服务器IP' identified by '密码';  
在测试机上,我们实际执行:
[sql]  view plain  copy
  1. grant replication slave  
  2. on *.*  
  3. to 'replication'@'%' identified by 'sosotest';  
这时在mysql库的user表中使用
[sql]  view plain  copy
  1. select * from user where user = 'replication' \G;  
可以看到Repl_slave_priv项对就的值为Y。
3.导出主服务器数据
导出主服务器数据,以备之后将其导入从服务器,使主从服务器的初始状态保持一致。

在测试机上,我们实际执行:
[sql]  view plain  copy
  1. mysqldump test > test.bak  

4.记录File 及Position 项的值
  重启mysql,使用show master status\G;查看主服务器状态,记录File 及Position 项的值,以便之后对从服务器进行配置。
  
在测试机上,我们实际执行时看到的结果如下:
*************************** 1. row ***************************
File: simba-bin.000008
Position: 79
Binlog_Do_DB: test
Binlog_Ignore_DB: 
1 row in set (0.00 sec)

其中File为imba-bin.000008;Position为79。

至此主服务器配置完毕

四、从数据库配置(slave)

1.修改mysql配置文件my.cnf
在[mysqld]标签下添加以下面一行:
[plain]  view plain  copy
  1. server-id=id #从机id  
实际中,我们添加:
[plain]  view plain  copy
  1. server-id=2  

2.导入主机数据库
在测试机上,我们实际执行:
mysql test < test.bak

3.重启mysql数据库并设置相关参数
[sql]  view plain  copy
  1. change master to  
  2. master_host = '10.1.146.133',  
  3. master_user = 'replication',  
  4. master_password = 'sosotest',  
  5. master_log_file = 'darkstar-bin.000008',  
  6. master_log_pos = 79;  
  7. slave start;  
4.检查数据库查看相关参数
  使用show slave status \G;查看相关参数是否正确。
  在实际执行时,我们在从机上看到的结果如下(只摘取了部分输出):
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.1.146.133
Master_User: replication
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: darkstar-bin.000006
Read_Master_Log_Pos: 79
Relay_Log_File: simba-relay-bin.000003
Relay_Log_Pos: 171
Relay_Master_Log_File: darkstar-bin.000006
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
……

对部分参数的解释:
Slave_IO_State: Waiting for master to send event 
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
以上参数说听slave正常运行,正等待主服务器发来的消息。此时若用netstate命令可以看到从机与主机间已经建立了一条边接。

特别需要注意的两个参数是:
Master_Log_File和Read_Master_Log_Pos。Master_Log_File代表主机上用于主备同步的日志文件名,Read_Master_Log_Pos代表上一次成功同步到的日志文件中的位置。
如果这两项与先前在主服务器上看到的File及Position的值不相符,则无法正确进行同步。

五、提醒

1、使用show variables like '%server_id%';命令查看my.conf配置文件 server-id值,主一般是1,从是2,主从必须不同。
2、flush logs命令,刷新二进制日志文件;show master status\G;查看最新File和Position值


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值