MySQL主从复制

实现主从同步主服务器只能写入数据,从服务器只提供查询
配置过程:
主服务器172.16.200.135
1,更改server-id
2,启用二进制日志
3,创建有复制权限的帐号
从服务器172.16.200.136
1,更改server-id
2,启用中继日志
3,指向主服务器(连接)
4,启动复制线程
5,限制从服务器只读
主服务器:
1.更改server-id 启动二进制日志
在mysqld中增加两行log-bin=/mydata/binlogs/master-bin server-id=1
创建文件夹binlogs,给予属主属组mysql权限,使其能读能写,之后重启服务器。

[root@localhost data]# vim /etc/my.cnf
log-bin=/mydata/binlogs/mater-bin
server-id = 1
[root@localhost data]# mkdir -pv /mydata/binlogs
mkdir: 已创建目录 "/mydata/binlogs"
[root@localhost data]# chown -R mysql.mysql /mydata/binlogs
[root@localhost data]# service mysqld restart
Shutting down MySQL. SUCCESS!
Starting MySQL.. SUCCESS!

在这里插入图片描述
在这里插入图片描述
2,创建有复制权限的帐号这里使用 帐号:user 密码:pass

[root@localhost data]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.5.59-log Source distribution
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'user'@'172.16.200.136' IDENTIFIED BY 'pass';
Query OK, 0 rows affected (0.24 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.09 sec)

mysql>

在这里插入图片描述
至此,主服务器配置完毕!是不是横简单呢?
接下来开始配置从服务器
1,修改server-id,启动中继日志 创建文件夹,然后重启服务器

[root@localhost relaylogs]# vim /etc/my.cnf
server-id=11
relay-log=/mydata/relaylogs/relay-bin
[root@localhost wang161x]# mkdir /mydata/relaylogs
[root@localhost wang161x]# chown -R mysql.mysql /mydata/relaylogs
[root@localhost relaylogs]# service mysqld restart
ERROR! MySQL server PID file could not be found!
Starting MySQL.. SUCCESS!

在这里插入图片描述
2.指向主服务器

mysql> CHANGE MASTER TO MASTER_HOST='172.16.200.135',MASTER_USER='user',MASTER_PASSWORD='pass';
Query OK, 0 rows affected (0.00 sec)

mysql> use mydb;
Database changed
mysql> show tables;
+----------------+
| Tables_in_mydb |
+----------------+
| area |
| city |
| hellodb |
| province |
| students |
+----------------+
5 rows in set (0.00 sec)

mysql> set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;
Query OK, 0 rows affected (0.06 sec)

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

mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.200.135
Master_User: user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mater-bin.000001
Read_Master_Log_Pos: 1285
Relay_Log_File: relay-bin.000003
Relay_Log_Pos: 253
Relay_Master_Log_File: mater-bin.000001
Slave_IO_Running: Yes
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: 1285
Relay_Log_Space: 1727
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: 1
1 row in set (0.00 sec)

ERROR:
No query specified

3,限制从服务器只读
在mysqld中添加read_only=ON

[root@localhost relaylogs]# vim /etc/my.cnf
read_only=ON
[root@localhost relaylogs]# service mysqld restart
Shutting down MySQL. SUCCESS!
Starting MySQL... SUCCESS!
[root@localhost relaylogs]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.59 Source distribution

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

在这里插入图片描述
测试主从同步:
在主服务器中创建一个数据库。查看从服务器是否可以同步
主服务器:

mysql> create database test1;
Query OK, 1 row affected (0.15 sec)

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mysql |
| mytestdb |
| performance_schema |
| test |
| test1 |
| testdb |
+--------------------+
8 rows in set (0.05 sec)

从服务器:

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mysql |
| mytestdb |
| performance_schema |
| test |
| test1 |
| testdb |
+--------------------+
8 rows in set (0.00 sec)

两个数据库里的数据完全相同,主从复制成功!!
遇到的错误:
1,

[root@localhost wang161x]# service mysqld start
Starting MySQL.. ERROR! The server quit without updating PID file (/mydata/data/localhost.localdomain.pid).

然后发现我已经启动了mysqld进程,所以kill重新开始,问题解决。

[root@localhost data]# ps -ef|grep mysqld
root 29101 1 0 12:09 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/mydata/data --pid-file=/mydata/data/bogon.pid
mysql 29400 29101 0 12:09 pts/0 00:00:06 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/mydata/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=bogon.err --pid-file=/mydata/data/bogon.pid --socket=/tmp/mysql.sock --port=3306
root 37244 30588 0 17:54 pts/1 00:00:00 grep --color=auto mysqld
[root@localhost data]# kill -9 29101
[root@localhost data]# kill -9 29400
[root@localhost data]# service mysqld restart
ERROR! MySQL server PID file could not be found!
Starting MySQL.. SUCCESS!

2.连接主服务器不成功

mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Connecting to master
Master_Host: 172.16.200.135
Master_User: user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File:
Read_Master_Log_Pos: 4
Relay_Log_File: relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File:
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: 0
Relay_Log_Space: 107
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: 2003
Last_IO_Error: error connecting to master 'user@172.16.200.135:3306' - retry-time: 60 retries: 86400
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 0
1 row in set (0.00 sec)

ERROR:
No query specified

遇到这种情况
1.尝试是否能ping通主服务器,ping不能则网络问题,若能ping通,则为设置问题,尝试用shell连接mysql数据库,若不能连接,显示错误代码113,则为密码错误,若可以连接,则尝试清空一下主从服务器上的iptables,

iptables -F
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.200.135
Master_User: user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mater-bin.000001
Read_Master_Log_Pos: 1198
Relay_Log_File: relay-bin.000002
Relay_Log_Pos: 1170
Relay_Master_Log_File: mater-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: No
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 1007
Last_Error: Error 'Can't create database 'mydb'; database exists' on query. Default database: 'mydb'. Query: 'CREATE DATABASE mydb'
Skip_Counter: 0
Exec_Master_Log_Pos: 1024
Relay_Log_Space: 1494
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: 0
Last_IO_Error:
Last_SQL_Errno: 1007
Last_SQL_Error: Error 'Can't create database 'mydb'; database exists' on query. Default database: 'mydb'. Query: 'CREATE DATABASE mydb'
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
1 row in set (0.00 sec)

ERROR:
No query specified

3,可以连接到主服务器,但不能进行复制数据,如上图,
这是因为有未同步数据,在从服务器上将指针后裔以为即可

mysql> set GLOBAL SQL_SLAVE_SKIP_COUNTER=1;
Query OK, 0 rows affected (0.06 sec)

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

mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.200.135
Master_User: user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mater-bin.000001
Read_Master_Log_Pos: 1285
Relay_Log_File: relay-bin.000003
Relay_Log_Pos: 253
Relay_Master_Log_File: mater-bin.000001
Slave_IO_Running: Yes
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: 1285
Relay_Log_Space: 1727
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: 1
1 row in set (0.00 sec)

ERROR:
No query specified

另外,mysql的错误也很有用,在数据目录下。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值