让两台服务器的MySQL(5.7)数据同步_主主同步(互为主从关系)

来源:互联网 发布:如何提升编程水平 编辑:程序博客网 时间:2024/05/16 04:26

网上找的大部分定义master-host的其实在新的MySQL中已经停用了,于是乎很悲剧的自己探索写出了这么一个东西

RedHat版本7.0

MySQL版本 5.7

服务器名称为假的!!!服务器名称为假的!!!服务器名称为假的!!!

重要事情说三遍~

另外需要注意的是,你同步的database里面的内容必须一致!!必须一致!!必须一致!!!

你想要电脑精神错乱吗?给俩不一样的数据让人家同步,还互为主从,直接短路啊!


服务器A:0.0.0.1

服务器B: 0.0.0.2


1. 首先在两个服务器上分别插入可以访问对方服务器的账号

在服务器A中设置:

Create user 'clare'@'0.0.0.2' identitied by 'clare-test123';

grant all on *.* to 'clare'@'0.0.0.2';

grant replication slave on *.* to 'clare'@'0.0.0.2';


服务器B则添加一模一样的账号:

Create user 'clare'@'0.0.0.1' identitied by 'clare-test123';

grant all on *.* to 'clare'@'0.0.0.1';

grant replication slave on *.* to 'clare'@'0.0.0.2';


2. 然后要在my.cnf中修改设置

就是这一步!!我累个去的网上的都是旧版本的不顶用了~!

服务器A作为主服务器,注意server-id=1

在服务器A中的/etc/my.cnf中添加如下设置:

server-id = 1

log-bin=my-bin

log-bin-index=my-bin.index

binlog-ignore-db = mysql

binlog-ignore-db = information_schema

binlog-db=test;

replicate-ignore-db=mysql;

replicate-ignore-db=information_schema

replicate-db=test;

#主主需加入的部分log-slave-updatessync_binlog=1auto_increment_offset=1auto_increment_increment=2

注意!最后两个log-bin和log-bin-index如果不配置的话,则在MySQL中使用show master status查不到任何记录!!!

文件名设置随便,但是目录位置在my.cnf配置文件中的datadir下


3. 服务器B重复上面的操作。注意server-id=2,别的内容都一样


4. 这时候重启服务器A\B的MySQL,应该是成功进入的

使用命令show master status应该能看到下面结果:


+---------------+----------+--------------+--------------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB         | Executed_Gtid_Set |
+---------------+----------+--------------+--------------------------+-------------------+
| my-bin.000001 |      154 |              | information_schema,mysql |                   |
+---------------+----------+--------------+--------------------------+-------------------+

5. 分别改变master的设置

A服务器:

mysql>change master to master_host='0.0.0.2', master_user='clare', master_password='clare-test123', master_log_file='my-bin.000001', master_log_pos=154;


B服务器:

mysql>change master to master_host='0.0.0.1', master_user='clare', master_password='clare-test123', master_log_file='my-bin.000001', master_log_pos=154;


这里讲解一下,master_log_file就是你用show master status命令看到的file一行的内容,而master_log_pos则是position的内容,不要设置错了


6. 然后在A、B服务器上开启slave

start slave;


7. 查看slave状态

show slave status\G

我截取一部分展示

  Slave_IO_State:
                  Master_Host: 0.0.0.1
                  Master_User: pccw
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: my-bin.000001
          Read_Master_Log_Pos: 154
               Relay_Log_File: apisec-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: my-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

              Replicate_Do_DB: mobile
          Replicate_Ignore_DB: mysql,information_schema
       

中间有颜色的那俩货是YES,就成功了

另外!!重要的事情说三遍!重要的事情说三遍!重要的事情说三遍!

如果你的tomcat 项目同时在A/B服务器上部署的话,要记得,连接服务器的URL这样写!

jdbc:mysql:loadbalance://[A服务器IP地址]:3306,[B服务器IP地址]:3306/mobile?useUnicode=true&characterEncoding=UTF-8

连接DB的账户要重新建立:

例如,用户名叫test,密码叫test123的话,那么A,B服务器的MySQL数据库中需要存在如下(使用语句select user,host from user时候要显示下面的情况)

---------------------------

user              host

--------------------------

test              A服务器IP

test              B服务器IP


创建账户的语句是:

create user 'test'@'A服务器IP' identified by 'test123';

grant all on *.* to 'test'@'A服务器IP' with grant option;

create user 'test'@'B服务器IP' identified by 'test123';

grant all on *.* to 'test'@'B服务器IP' with grant option;


记得!!!两个服务器的MySQL都要创建如上同样一个账号!!!


最近碰到一个问题,在这里马克一下

两个数据库同步因为时间差的关系,经常出现不一致,即使是设置了半同步也没有用

so,为了保证运行通畅,目前只能采取最简单粗暴的办法解决

使用show slave status\G命令经常看到出错的原因是1032错误,直接在配置文件my.cnf中添加忽略1032错误......

具体方法就不说了,可以上网找到




0 0
原创粉丝点击