MySQL Replication配置

[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语句落地。

原理图

mark

二、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的安装和配置

vim /etc/my.cnf

mark

vim /etc/init.d/mysqld

mark

[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!


## 三、配置主机

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

![mark](http://p0weeraap.bkt.clouddn.com/xavi2017/180405/GlG0GJI3f9.png?imageslim)

[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是索引。
![mark](http://p0weeraap.bkt.clouddn.com/xavi2017/180405/c70Kl3EA7A.png?imageslim)

### 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服务

![mark](http://p0weeraap.bkt.clouddn.com/xavi2017/180406/GHh4fl9Ek4.png?imageslim)

[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/
root@192.168.72.130's password:
Permission denied, please try again.
root@192.168.72.130'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 'repl@192.168.72.130: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 'repl@192.168.72.130: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 repl@192.168.72.133 |
+------------------------------------------------------------------------------------------------------------------------------+
| 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的区别

## 五、测试主从

未完待续

转载于:https://blog.51cto.com/12995218/2096822

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值