MySQL Replication配置

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

[toc]

MySQL Replication配置

一、MySQL主从介绍

MySQL主从又叫做Replication、AB复制。简单讲就是A和B两台机器做主从后,在A上写数据,另外一台B也会跟着写数据,两者数据实时同步;

MySQL主从是基于binlog的,主上须开启binlog才能进行主从;

主从过程大致有3个步骤:

1)主将更改操作记录到binlog里

2)从将主的binlog事件(sql语句)同步到从本机上并记录在relaylog里

3)从根据relaylog里面的sql语句按顺序执行 主上有一个log

dump线程,用来和从的I/O线程传递binlong;

从上有两个线程,其中I/O线程用来同步主的binlog并生成relaylog,另外一个sql线程用来把relaylog里面的sql语句落地。

原理图

二、mysql编译安装:

cd /usr/local/src

wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz 

tar zxvf mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz

mv mysql-5.6.36-linux-glibc2.5-x86_64 /usr/local/mysql

cd /usr/local/mysql

useradd mysql

mkdir -p /data/mysql

chown -R mysql:mysql /data/mysql

./scripts/mysql_install_db --user=mysql --datadir=/data/mysql

运行出现两个OK后

cp support-files/my-default.cnf  /etc/my.cnf


vim /etc/my.cnf
 basedir =/usr/local/mysql
 datadir =/data/mysql
 port =3306
 server_id =128
 socket =/tmp/mysql.sock
 log_bin=xavilinux01
# Remove leading # to set options mainly useful for reporting servers.
:wq 

cp support-files/mysql.server /etc/init.d/mysqld

正常操作是没有问题,但是也发现了错误

[root@xavilinux mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
-bash: ./scripts/mysql_install_db: No such file or directory

这个问题处理了很久,没有思路,左后想起查看下当前的mysql路径下有哪些文件,才找到错误的地方是里面的srcipts目录,是自己创建的,里面是空的

[root@xavilinux mysql]# ls
scripts
[root@xavilinux mysql]# cd /usr/local/src
[root@xavilinux src]# ls
mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz  nginx-1.12.1
[root@xavilinux src]# tar zxvf mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz
tar (child): mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz: Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
tar: Child returned status 2
tar: Error is not recoverable: exiting now
[root@xavilinux src]# cd /usr/local/mysql
[root@xavilinux mysql]# ls
bin  COPYING  data  docs  include  lib  man  mysql-test  README  scripts  share  sql-bench  support-files

再次报错:

[root@xavilinux mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
Installing MySQL system tables..../bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

原因:缺少libaio库文件

解决方法:yum install libaio* -y

在两台服务器上完成了mysql的安装和配置

MySQL支持单向、异步复制,复制过程中一个服务器充当主服务器,而一个或多个其它服务器充当从服务器。主服务器将更新写入二进制日志文件,并维护日志文件的一个索引以跟踪日志循环。当一个从服务器连接到主服务器时,它通知主服务器从服务器在日志中读取的最后一次成功更新的位置。从服务器接收从那时起发生的任何更新,然后封锁并等待主服务器通知下一次更新。

为什么使用主从复制?

1、主服务器/从服务器设置增加了健壮性。主服务器出现问题时,你可以切换到从服务器作为备份。

2、通过在主服务器和从服务器之间切分处理客户查询的负荷,可以得到更好的客户响应时间。但是不要同时在主从服务器上进行更新,这样可能引起冲突。

3、使用复制的另一个好处是可以使用一个从服务器执行备份,而不会干扰主服务器。在备份过程中主服务器可以继续处理更新。

MySQL使用3个线程来执行复制功能(其中1个在主服务器上,另两个在从服务器上。当发出START SLAVE时,从服务器创建一个I/O线程,以连接主服务器并让主服务器发送二进制日志。主服务器创建一个线程将二进制日志中的内容发送到从服务器。从服务器I/O线程读取主服务器Binlog Dump线程发送的内容并将该数据拷贝到从服务器数据目录中的本地文件中,即中继日志。第3个线程是SQL线程,从服务器使用此线程读取中继日志并执行日志中包含的更新。SHOW PROCESSLIST语句可以查询在主服务器上和从服务器上发生的关于复制的信息。

默认中继日志使用host_name-relay-bin.nnnnnn形式的文件名,其中host_name是从服务器主机名,nnnnnn是序列号。用连续序列号来创建连续中继日志文件,从000001开始。从服务器跟踪中继日志索引文件来识别目前正使用的中继日志。默认中继日志索引文件名为host_name-relay-bin.index。在默认情况,这些文件在从服务器的数据目录中被创建。中继日志与二进制日志的格式相同,并且可以用mysqlbinlog读取。当SQL线程执行完中继日志中的所有事件后,中继日志将会被自动删除。

从服务器在数据目录中另外创建两个状态文件--master.info和relay-log.info。状态文件保存在硬盘上,从服务器关闭时不会丢失。下次从服务器启动时,读取这些文件以确定它已经从主服务器读取了多少二进制日志,以及处理自己的中继日志的程度。

vim /etc/my.cnf

vim /etc/init.d/mysqld

[root@xavilinux ~]# /etc/init.d/mysqld start
/etc/init.d/mysqld: line 46: /usr/local/mysql: Is a directory
/etc/init.d/mysqld: line 47: /data/mysql: Is a directory
Starting MySQL.Logging to '/data/mysql/xavilinux.err'.
                                                           [  OK  ]

出现mysql无法启动,报错的情况

1.有可能开了java别的服务占用端口,lsof -i :80查看端口情况,将占用的服务关闭

[root@xavi ~]# lsof -i :80
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME

2. 从错误日志入手看下报错原因

[root@xavi ~]# /etc/init.d/mysqld start
Starting MySQL... ERROR! The server quit without updating PID file (/data/mysql/xavi.pid).
[root@xavi ~]# ls -l /data/mysql
总用量 110720
-rw-rw---- 1 mysql mysql       56 3月  13 22:17 auto.cnf
drwx------ 2 mysql mysql       20 3月  23 22:17 db2
-rw-rw---- 1 mysql mysql 12582912 4月   6 15:53 ibdata1
-rw-rw---- 1 mysql mysql 50331648 4月   6 15:53 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 3月  13 22:11 ib_logfile1
drwx------ 2 mysql mysql     4096 3月  13 22:11 mysql
drwx------ 2 mysql mysql     4096 3月  24 09:55 mysql2
drwx------ 2 mysql mysql     4096 3月  13 22:11 performance_schema
drwx------ 2 mysql mysql        6 3月  13 22:11 test
-rw-rw---- 1 mysql mysql   114094 4月   6 15:53 xavi.err
drwx------ 2 mysql mysql      324 3月  31 15:49 zrlog
  • 查看错误日志
[root@xavi mysql]# tail xavi.err
2018-04-06 16:08:43 7318 [Note] Shutting down plugin 'CSV'
2018-04-06 16:08:43 7318 [Note] Shutting down plugin 'MEMORY'
2018-04-06 16:08:43 7318 [Note] Shutting down plugin 'MyISAM'
2018-04-06 16:08:43 7318 [Note] Shutting down plugin 'MRG_MYISAM'
2018-04-06 16:08:43 7318 [Note] Shutting down plugin 'sha256_password'
2018-04-06 16:08:43 7318 [Note] Shutting down plugin 'mysql_old_password'
2018-04-06 16:08:43 7318 [Note] Shutting down plugin 'mysql_native_password'
2018-04-06 16:08:43 7318 [Note] Shutting down plugin 'binlog'
2018-04-06 16:08:43 7318 [Note] /usr/local/mysql/bin/mysqld: Shutdown complete

[root@xavi mysql]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS! 

三、配置主机

主master:192.168.72.130 从slave:192.168.72.133

3.1 修改my.cnf,增加server-id=130和log_bin=xavilinux1

[mysqld]
datadir=/data/mysql
basedir=/usr/local/mysql
socket=/tmp/mysql.sock
symbolic-links=0

server_id =130
log_bin=xavilinux

3.2 修改完配置文件后,启动或者重启mysqld服务

[root@xavi ~]# vim /etc/my.cnf
[root@xavi ~]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 
  • 生成了两个重要文件,前缀名为xavilinux1,这是实现主从的根本。000001是二进制日志文件,index是索引。

3.3 备份之前blog的所有的数据(测试用)

[root@xavi mysql]# mysqldump -uroot -pxavilinux blog > /tmp/blog.sql
Warning: Using a password on the command line interface can be insecure.
[root@xavi mysql]# du -sh /tmp/blog.sql
4.0K	/tmp/blog.sql

3.4 创建一个新库把之前的备份数据恢复

[root@xavi mysql]# mysql -uroot -pxavilinux -e "create database xavi"
Warning: Using a password on the command line interface can be insecure.
[root@xavi mysql]# mysql -uroot -pxavilinux xavi < /tmp/blog.sql
Warning: Using a password on the command line interface can be insecure.

3.5 创建用作同步数据的用户

[root@xavi mysql]# mysql -uroot -pxavilinux

mysql> grant replication slave on *.* to 'repl'@'192.168.72.133' identified by 'xavilinux111'; //密码是主机登入密码??
Query OK, 0 rows affected (0.00 sec)

把数据表锁住,不在往里面写数据

mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| xavilinux1.000001 |      732 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.01 sec)

3.6 备份下/data/mysql下的部分目录,但mysql不需要备份,无法主从同步,里面包含了账户密码等信息

cd /data/mysql
[root@xavi mysql]# ls
auto.cnf  db2      ib_logfile0  mysql   performance_schema  xavi      xavilinux1.000001  xavi.pid
blog      ibdata1  ib_logfile1  mysql2  test                xavi.err  xavilinux1.index   zrlog
[root@xavi mysql]# mysqldump -uroot -pxavilinux mysql2 > /tmp/my2.sql
Warning: Using a password on the command line interface can be insecure.
[root@xavi mysql]# mysqldump -uroot -pxavilinux zrlog > /tmp/zrlog.sql
Warning: Using a password on the command line interface can be insecure.

四、配置从机

4.1 编辑配置文件,并重启MySQL服务

[root@xavilinux mysql]# vim /etc/my.cnf
 basedir =/usr/local/mysql
 datadir =/data/mysql
# port =3306
# server_id = .....
 socket =/tmp/mysql.sock
 server_id=133
 
 [root@xavi-002 mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@xavi-002 mysql]# vim /etc/init.d/mysqld

basedir=/usr/local/mysql
datadir=/data/mysql

[root@xavilinux mysql]# /etc/init.d/mysqld restart
Shutting down MySQL..                                      [  OK  ]
Starting MySQL.                                            [  OK  ]

4.2 查看从机下有哪些内容,把主机上所有mysql文件远程复制过来scp

[root@xavi-002 ~]# ls /data/mysql
auto.cnf  ib_logfile0  mysql               test          xavi-002.pid
ibdata1   ib_logfile1  performance_schema  xavi-002.err
[root@xavi-002 ~]# scp 192.168.72.130:/tmp/*.sql /tmp/
[email protected]'s password: 
Permission denied, please try again.
[email protected]'s password: 
blog.sql                                      100% 1258     1.2KB/s   00:00    
my2.sql                                       100%  638KB 638.4KB/s   00:00    
zrlog.sql                                     100% 9880     9.7KB/s   00:00 

4.3 把两个命令mysql和mysqldump用alias设置

[root@xavi-002 ~]# alias 'mysql=/usr/local/mysql/bin/mysql'
[root@xavi-002 ~]# alias 'mysqldump=/usr/local/mysql/bin/mysqldump'

4.4 进入mysql,不设密码,建立4个数据库

[root@xavi-002 mysql]# mysql -uroot
mysql> create database xavi;
Query OK, 1 row affected (0.00 sec)

mysql> create database zrlog;
Query OK, 1 row affected (0.00 sec)

mysql> create database blog;
Query OK, 1 row affected (0.00 sec)

mysql> create database mysql2;
Query OK, 1 row affected (0.00 sec)

4.5 将新建的数据库中恢复主机备份过来的数据

[root@xavi-002 ~]# mysql -uroot blog < /tmp/blog.sql
[root@xavi-002 ~]# mysql -uroot zrlog < /tmp/zrlog.sql

[root@xavi-002 ~]# mysql -uroot mysql2 < /tmp/my2.sql
[root@xavi-002 ~]# ls /data/mysql
auto.cnf  ib_logfile0  mysql2              xavi          zrlog
blog      ib_logfile1  performance_schema  xavi-002.err
ibdata1   mysql        test                xavi-002.pid

4.6 复制数据后,在slave上配置

[root@xavi-002 ~]# mysql -uroot
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> change master to master_host='192.168.72.130',master_user='repl',master_password='xavilinux',master_log_file='xavilinux1.000001',master_log_pos=424;
Query OK, 0 rows affected, 2 warnings (0.03 sec)

4.7 启动 slave

mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Connecting to master
                  Master_Host: 192.168.72.130
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: xavilinux1.000001
          Read_Master_Log_Pos: 424
               Relay_Log_File: xavi-002-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: xavilinux1.000001
             Slave_IO_Running: Connecting
            Slave_SQL_Running: Yes

报错

 Last_IO_Error: error connecting to master '[email protected]:3306' - retry-time: 60  retries: 1

这是在3.5 小节测试时写错了从机的IP地址,

[root@xavi mysql]# mysql -uroot -pxavilinux
mysql>  grant replication slave on *.* to 'repl'@'192.168.72.133' identified by 'xavilinux';
Query OK, 0 rows affected (0.00 sec)

mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| xavilinux.000002 |      120 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

[root@xavi mysql]# ls
auto.cnf  db2      ib_logfile0  mysql   performance_schema  xavi      xavilinux.000002  xavi.pid
blog      ibdata1  ib_logfile1  mysql2  test                xavi.err  xavilinux.index   zrlog

报错:

mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)

mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Connecting to master
                  Master_Host: 192.168.72.130
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: xavilinux1.000004
          Read_Master_Log_Pos: 331
               Relay_Log_File: xavi-002-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: xavilinux1.000004
             Slave_IO_Running: Connecting
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           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: 331
              Relay_Log_Space: 120
              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: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 1045
                Last_IO_Error: error connecting to master '[email protected]:3306' - retry-time: 60  retries: 1
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 0
                  Master_UUID: 
             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: 180408 23:22:46
     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)

ERROR: 
No query specified

mysql> perror 1045;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'perror 1045' at line 1

查看主机授权

mysql> select user,host,password from mysql.user;
+-------+----------------+-------------------------------------------+
| user  | host           | password                                  |
+-------+----------------+-------------------------------------------+
| root  | localhost      | *254DE8C0E825F909A01A520D296E6A883FFDE4F8 |
| root  | xavi           | *254DE8C0E825F909A01A520D296E6A883FFDE4F8 |
| root  | 127.0.0.1      | *254DE8C0E825F909A01A520D296E6A883FFDE4F8 |
| root  | ::1            | *254DE8C0E825F909A01A520D296E6A883FFDE4F8 |
|       | localhost      |                                           |
|       | xavi           |                                           |
| user1 | 127.0.0.1      | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| user1 | localhost      | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| user2 | 192.168.133.1  | *59C70DA2F3E3A5BDF46B68F5C8B8F25762BCCEF0 |
| user2 | 192.168.133.2  | *59C70DA2F3E3A5BDF46B68F5C8B8F25762BCCEF0 |
| zrlog | 127.0.0.1      | *D66E108B48328249BB2779FCB0F0B03E3EDF92C1 |
| blog  | 127.0.0.1      | *D66E108B48328249BB2779FCB0F0B03E3EDF92C1 |
| repl  | 192.168.72.133 | *BF29B1459C13E117044049733F4E17D0D14AC7DB |
+-------+----------------+-------------------------------------------+
13 rows in set (0.00 sec)

mysql> show grants for 'repl'@'192.168.72.133';
+------------------------------------------------------------------------------------------------------------------------------+
| Grants for [email protected]                                                                                               |
+------------------------------------------------------------------------------------------------------------------------------+
| GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.72.133' IDENTIFIED BY PASSWORD '*BF29B1459C13E117044049733F4E17D0D14AC7DB' |
+------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
  • 问题一直未找到,有可能是mysql版本5.5和5.6的区别

1、在master mysql创建同步用户

grant emuser slave,file on *.* to [email protected] identified by 123456;
flush privileges;
修改master的my.cnf的配置
wait_timeout = 30000000
interactive-timeout = 30000000
binlog-do-db=cdn_view  #设置二进制日志记录的库
log-bin=mysql-bin         #打开mysql二进制日志
binlog-ignore-db=mysql   ##设置二进制日志不记录的库
server-id = 12760         #设置mysql_id,主从不能相同
long_query_time = 5
expire_logs_days= 3
2、修改slave的my.cnf的配置
wait_timeout = 30000000
interactive-timeout = 30000000
log-bin=mysql-bin
server-id = 12761
replicate-do-db=cdn_view   #设置同步的库
replicate-ignore-db=mysql  #设置不同步的库
log-slave-updates         #同步后记录二进制日志
slave-skip-errors=all  
slave-net-timeout=60
sync_binlog=1
binlog_format=mixed

############################################################################
分别重启主从mysqld服务,登录主mysql,在主上执行flush tables with read lock;
然后将cdn_view数据库的数据copy到从上,并记录下主上show master statusG的结果:
mysql> show master statusG;
*************************** 1. row ***************************
            File: mysql-bin.000009
        Position: 341
    Binlog_Do_DB: cdn_view
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)

然后执行unlock tables;
登录从mysql,在从上执行:
stop slave;
change master to master_host=192.168.5.60,master_user=emuser,master_password=123456, master_log_file=mysql-bin.000009, master_log_pos=341;
start slave;
show slave statusG;

如果出现如下信息说明主从同步成功。
Slave_IO_Running: Yes
Slave_SQL_Running: Yes

五、测试主从

不成功

转载于:https://my.oschina.net/u/3960917/blog/2992338

你可能感兴趣的:(MySQL Replication配置)