荣耀之链论坛

 找回密码
 立即注册
搜索
查看: 5035|回复: 2

centos下的mysql主从同步

[复制链接]

1326

主题

2373

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
10267
发表于 2015-11-1 18:22 | 显示全部楼层 |阅读模式
http://wenzhang.baidu.com/page/v ... cbcb150a-1426530514

1、主从服务器分别作以下操作:版本一致
把主服务器的数据库复制一份到从服务器

2、修改主服务器
   #vi /etc/my.cnf
       [mysqld]
       log-bin=mysql-bin   //[必须]启用二进制日志
       server-id=222      //[必须]服务器唯一ID,默认是1,一般取IP最后一段
然后启动主服务器数据库
3、修改从服务器
   #vi /etc/my.cnf
       [mysqld]
      server-id=2
master-host=192.168.9.2
master-user=tongbu
master-password=123456
replicate-do-db=qicai
#这里填需要同步的数据库
然后启动从服务器数据库


测试:
进入主服务器修改一个数据,然后进入从服务器查看 是立刻就同步好了的
回复

使用道具 举报

匿名  发表于 2015-11-10 16:49
上面的方法我在另外两台centos上设置没有成功 不知道什么问题

用下面的方法吧
http://www.linuxidc.com/Linux/2012-07/66871.htm

回复 支持 反对

使用道具

1326

主题

2373

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
10267
 楼主| 发表于 2015-12-22 21:31 | 显示全部楼层
MySQL互为主从Replication

[日期:2012-07-31]        来源:Linux社区  作者:tonychiu        [字体:大 中 小]
mysql互为主从Replication
一、环境

系统:CentOS x64

Mysql:Version 5.1.47

主机:A:192.168.10.101   root:linuxidc.com

      B:192.168.10.102   root:linuxidc.com

二、步骤

两台MySQL均如要开启binlog日志功能,开启方法:加入log-bin=mysql-bin

1、主机A配置文件如下:

# vi /etc/my.cnf

[mysqld]

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

log-error=/var/lib/mysql/mysql.err

log = /var/lib/mysql/query_log.log

log-slow-queries = /var/lib/mysql/slow_query_log.log

user=mysql

default-character-set=utf8

init_connect='SET NAMES utf8'

# Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

log-bin=mysql-bin

server-id=1

binlog-do-db=iccstm1   #是要记录日志的数据库

binlog-ignore-db=mysql  #是不要记录日志的数据库名,多个数据库中间用逗号(,)隔开

replicate-do-db=iccstm1

replicate-ignore-db=mysql

log-slave-updates  #表示 如果一个MASTER 挂掉的话,另外一个马上接管;一定要加上,否则不会把更新的记录写到二进制文件里

slave-skip-errors=all   #是跳过错误,继续执行复制操作

sync_binlog=1

auto_increment_increment=2

auto_increment_offset=1 #这样A的auto_increment字段产生的数值是:1, 3, 5, 7, …等奇数ID



[client]

default-character-set=utf8



[mysqld_safe]

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid



2、主机B配置文件如下:

[mysqld]

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

log-error=/var/lib/mysql/mysql.err

log = /var/lib/mysql/query_log.log

log-slow-queries = /var/lib/mysql/slow_query_log.log

user=mysql

default-character-set=utf8

init_connect='SET NAMES utf8'

# Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

log-bin=mysql-bin

server-id=2

binlog-do-db=iccstm1

binlog-ignore-db=mysql

replicate-do-db=iccstm1

replicate-ignore-db=mysql

log-slave-updates

slave-skip-errors=all

sync_binlog=1

auto_increment_increment=2

auto_increment_offset=2



[client]

default-character-set=utf8



[mysqld_safe]

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid



3、SQL操作

可以选择锁表进一步操作,这里锁表的目的是为了生产环境中不让进新的数据,好让从服务器定位同步位置。初次同步完成后,记得解锁

【1】取得master值

A服务器:

mysql> show master status\G

*************************** 1. row ***************************

            File: mysql-bin.000007

        Position: 42617

    Binlog_Do_DB: iccstm1

Binlog_Ignore_DB: mysql

1 row in set (0.00 sec)

B服务器:

mysql> show master status\G

*************************** 1. row ***************************

            File: mysql-bin.000005

        Position: 44687

    Binlog_Do_DB: iccstm1

Binlog_Ignore_DB: mysql

1 row in set (0.00 sec)

记住上面两个数值,master_log_file对应File,master_log_pos对应Position



【2】指定同步位置(以下操作顺序不可乱)


&&&要先打开两台服务器的允许远程&&&

Mysql> grant all privileges on *.* to 'root'@'%' identified by 'linuxidc.com' with grant option;

   B服务器:

MySQL> grant replication slave on *.* to 'replication'@'%' identified by 'linuxidc.com';
Query OK, 0 rows affected (0.00 sec)   

MySQL>flush privileges;
Query OK, 0 rows affected (0.00 sec)





   A服务器:

mysql>change master to master_host='192.168.10.102', master_user='replication', master_password='linuxidc.com',master_log_file='mysql-bin.000005',master_log_pos=44687;

MySQL> grant replication slave on *.* to 'replication'@'%' identified by 'linuxidc.com';
Query OK, 0 rows affected (0.00 sec)   

MySQL>flush privileges;
Query OK, 0 rows affected (0.00 sec)



  

B服务器:

mysql>change master to master_host='192.168.10.101', master_user='replication', master_password='linuxidc.com',master_log_file='mysql-bin.000007',master_log_pos=42617;



A、B服务器分别查看从服务器状态

A服务器

mysql>start slave;

mysql>show slave status\G

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.10.102

Master_User: replication

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000007

Read_Master_Log_Pos: 42617

Relay_Log_File: mysqld-relay-bin.000002

Relay_Log_Pos: 46592

Relay_Master_Log_File: mysql-bin.000007

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

Replicate_Do_DB: iccstm1

Replicate_Ignore_DB: mysql

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: 42617

Relay_Log_Space: 40748

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:

1 row in set (0.00 sec)

B服务器

mysql>start slave;

mysql>show slave status\G

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.10.101

Master_User: replication

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000005

Read_Master_Log_Pos: 44687

Relay_Log_File: mysqld-relay-bin.000002

Relay_Log_Pos: 532

Relay_Master_Log_File: mysql-bin.000005

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

Replicate_Do_DB: iccstm1

Replicate_Ignore_DB: mysql

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: 44687

Relay_Log_Space: 688

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:

1 row in set (0.00 sec)



Slave_IO_Running: Yes

Slave_SQL_Running: Yes

  查看以上两项的值,均为Yes则表示状态正常
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

荣耀之链

GMT+8, 2025-6-18 03:25 , Processed in 0.012390 second(s), 20 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表