欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Linux Mysql 5.6 主从(Master/Slave)同步配置

程序员文章站 2022-05-26 16:13:02
...

安装环境:

操作系统:CentOS 6.7 64位

数据库:mysql 5.6

主库(master)IP:192.168.1.91

从库(slave)IP:192.168.1.92

 

主从数据库的同步主要通过bin-log 日志进行读取赋值,所有操作sql都会在bin-log中展示,所以主从库赋值也主要是通过日志方式来访问。

 

Master配置:

在Linux下mysql配置文件/etc/my.cnf,添加一下配置:

 

log-bin=mysql-bin
server-id=2
binlog-ignore-db=information_schema
binlog-ignore-db=cluster
binlog-ignore-db=mysql
binlog-do-db=test

 

 

server-id:数据库唯一标识,可任意设置

binlog-ignore-db:同步时忽略的数据库

binlog-do-db:需要同步的数据库

 

完整配置如下:


Linux Mysql 5.6 主从(Master/Slave)同步配置
            
    
    博客分类: mysql  
 

<!--[if !supportLists]-->1.       <!--[endif]-->重启mysql:service mysql restart

<!--[if !supportLists]-->2.       <!--[endif]-->进入mysql:mysql –u root –p

<!--[if !supportLists]-->3.       <!--[endif]-->显示主库信息:mysql>show master status;


Linux Mysql 5.6 主从(Master/Slave)同步配置
            
    
    博客分类: mysql  
 

 

 

Slave配置:

从库同样修改mysql配置文件 /etc/my.cnf,主要配置如下:

log-bin=mysql-bin
server-id=3
binlog-ignore-db=information_schema
binlog-ignore-db=cluster
binlog-ignore-db=mysql
replicate-do-db=test
replicate-ignore-db=mysql
log-slave-updates
slave-skip-errors=all
slave-net-timeout=60

 

完整配置如下:


Linux Mysql 5.6 主从(Master/Slave)同步配置
            
    
    博客分类: mysql  
 

 

注:Mysql5.6 之前版本不能在配置文件中配置master相关配置信息:

master-host=192.168.1.91 #Master的主机IP

master-user=root

master-password=mysql password #Master的MySQL密码

 

修改my.cnf文件后进入mysql

mysql> stop slave;  #关闭Slave

mysql> change master to master_host='192.168.1.91',master_user='root',master_password='123456',master_log_file='mysql-bin.000001', master_log_pos=5;

mysql> start slave;  #开启Slave

 

查看slave:

mysql> show slave status \G;

mysql> show slave status \G;

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

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.1.91

                  Master_User: root

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000002

          Read_Master_Log_Pos: 120

               Relay_Log_File: localhost-relay-bin.000006

                Relay_Log_Pos: 283

        Relay_Master_Log_File: mysql-bin.000002

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

              Replicate_Do_DB: ufind_db,test

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

              Relay_Log_Space: 623

              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:

  Replicate_Ignore_Server_Ids:

             Master_Server_Id: 2

                  Master_UUID: a2e674e1-a536-11e8-92da-000c29d0ad20

             Master_Info_File: /data/mysql/master.info

                    SQL_Delay: 0

          SQL_Remaining_Delay: NULL

      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it

           Master_Retry_Count: 86400

                  Master_Bind:

      Last_IO_Error_Timestamp:

     Last_SQL_Error_Timestamp:

               Master_SSL_Crl:

           Master_SSL_Crlpath:

           Retrieved_Gtid_Set:

            Executed_Gtid_Set:

                Auto_Position: 0

1 row in set (0.00 sec)

 

注:在该处可能会有如下错误:

Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'

解决方法:

mysql> stop slave;

mysql> reset slave;

mysql> start slave;

 

在主库中配置文件中需要添加binlog-do-db 需同步的数据库的配置信息,同样在从库中也要添加需要赋值的replicate-do-db 的数据库的配置信息。

 

访问配置:

访问通过360的Atlas代理方式访问两个数据库,代码GitHub下载,连接地址:https://github.com/Qihoo360/Atlas

下载Atlas-XX.el6.x86_64.rpm 

linux下安装

rpm –i Atlas-XX.el6.x86_64.rpm 

修改mysql-proxy/conf/test.cnf文件内容,修改后内容如下

[mysql-proxy]

 

#带#号的为非必需的配置项目

 

#管理接口的用户名

admin-username = root

 

#管理接口的密码

admin-password = 123456

 

#Atlas后端连接的MySQL主库的IP和端口,可设置多项,用逗号分隔

proxy-backend-addresses = 192.168.1.91:3306

 

#Atlas后端连接的MySQL从库的IP和端口,@后面的数字代表权重,用来作负载均衡,若省略则默认为1,可设置多项,用逗号分隔

proxy-read-only-backend-addresses = 192.168.1.92:3306@1

 

#用户名与其对应的加密过的MySQL密码,密码使用PREFIX/bin目录下的加密程序encrypt加密,下行的user1和user2为示例,将其替换为你的MySQL的用户名和加密密码!

pwds = root:/iZxz+0GRoA=

 

#设置Atlas的运行方式,设为true时为守护进程方式,设为false时为前台方式,一般开发调试时设为false,线上运行时设为true,true后面不能有空格。

daemon = false

 

#设置Atlas的运行方式,设为true时Atlas会启动两个进程,一个为monitor,一个为worker,monitor在worker意外退出后会自动将其重启,设为false时只有worker,没有monitor,一般开发调试时设为false,线上运行时设为true,true后面不能有空格。

keepalive = false

 

#工作线程数,对Atlas的性能有很大影响,可根据情况适当设置

event-threads = 4

 

#日志级别,分为message、warning、critical、error、debug五个级别

log-level = message

 

#日志存放的路径

log-path = /usr/local/mysql-proxy/log

 

#SQL日志的开关,可设置为OFF、ON、REALTIME,OFF代表不记录SQL日志,ON代表记录SQL日志,REALTIME代表记录SQL日志且实时写入磁盘,默认为OFF

sql-log = REALTIME

 

#慢日志输出设置。当设置了该参数时,则日志只输出执行时间超过sql-log-slow(单位:ms)的日志记录。不设置该参数则输出全部日志。

#sql-log-slow = 10

 

#实例名称,用于同一台机器上多个Atlas实例间的区分

#instance = test

 

#Atlas监听的工作接口IP和端口

proxy-address = 0.0.0.0:1234

 

#Atlas监听的管理接口IP和端口

admin-address = 0.0.0.0:2345

 

#分表设置,此例中person为库名,mt为表名,id为分表字段,3为子表数量,可设置多项,以逗号分隔,若不分表则不需要设置该项

#tables = person.mt.id.3

 

#默认字符集,设置该项后客户端不再需要执行SET NAMES语句

charset = utf8

 

#允许连接Atlas的客户端的IP,可以是精确IP,也可以是IP段,以逗号分隔,若不设置该项则允许所有IP连接,否则只允许列表中的IP连接

client-ips = 127.0.0.1, 192.168.1

 

#Atlas前面挂接的LVS的物理网卡的IP(注意不是虚IP),若有LVS且设置了client-ips则此项必须设置,否则可以不设置

 

#lvs-ips = 192.168.1.1

 

 

启动

mysql-proxy/bin/mysql-proxyd test start

 

启动后正常访问mysql方式访问,访问地址192.168.1.91 端口1234

可查看日志mysql-proxy/log/sql_test.log

 

读数据库是访问的是192.168.1.92,写数据库时访问的是192.168.1.91

 

  • Linux Mysql 5.6 主从(Master/Slave)同步配置
            
    
    博客分类: mysql  
  • 大小: 39.1 KB
  • Linux Mysql 5.6 主从(Master/Slave)同步配置
            
    
    博客分类: mysql  
  • 大小: 7.9 KB
  • Linux Mysql 5.6 主从(Master/Slave)同步配置
            
    
    博客分类: mysql  
  • 大小: 42.4 KB