MySQL主从复制配置实现
来源:互联网 发布:oi随机数算法 编辑:程序博客网 时间:2024/06/05 16:20
在实际生产中,数据的重要性不言而喻,因此考虑到数据的重要性比如单点故障导致后端数据库奔溃,或者后端数据库访问压力过大等,mysql数据库做主从非常有必要,减轻后端数据库压力,主服务器负责读写,从服务器只负责读,这样一来即保证了数据的可靠性,同时提高服务器的高可用。
MySQL主从复制原理:master服务器将数据的改变记录二进制日志,当master上的数据发生改变时,则将其改变写入二进制日志中,salve服务器会在一定时间间隔内对master二进制日志进行探测其是否发生改变,如果发生改变,则开始一个I/OThread请求master二进制事件,同时主节点为每个I/O线程启动一个dump线程,用于向其发送二进制事件,并保存至从节点本地的中继日志中,从节点将启动SQL线程从中继日志中读取二进制日志,在本地重放,使得其数据和主节点的保持一致,最后I/OThread和SQLThread将进入睡眠状态,等待下一次被唤醒。
二、主从复制配置实现
要求:
1、双方mysql版本需一致,如不一致,只要主节点低于从节点
2、两节点间时间需同步
配置:
主服务器配置如下:
1、修改/etc/my.cnf配置文件
log-bin=/mydata/data/binlogs/master-bin
2、创建此目录并修改属组属主为mysql
mkdir /mydata/binlogs/
chown -R mysql.mysql /mydata/binlogs/
3、授权用户
grant replication slave,replication client on *.* to 'repluser'@'10.1.10.%' identified by 'pass';
flush privileges;
从服务器配置如下:
1、修改/etc/my.cnf配置文件,注释二进制日志,开启中继日志,修改server-id和主节点不一致
server-id=11
relay-log=/mydata/relaylogs/relay-bin
2、创建其目录并授予此目录的属主、属组为mysql
mkdir /mydata/relaylogs/
chown -R mysql.mysql /mydata/relaylogs/
3、连接主服务器
change master to master_host='10.1.10.1',master_user='repluser',master_password='pass';
完成上诉配置过程即可各自启动mysql服务器,且在从服务器上启动I/O,SQL线程,例如:start slave
测试两数据可数据是否能同步,则可在主库上插入数据在从库上查看是否存在,注意:从库只能读,而不能写
[root@centos6 ~]
# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection
id
is 13
Server version: 5.5.32-MariaDB MariaDB Server
Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
Type
'help;'
or
'\h'
for
help. Type
'\c'
to
clear
the current input statement.
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting
for
master to send event
Master_Host: 10.1.10.1
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000003
Read_Master_Log_Pos: 326
Relay_Log_File: relay-bin.000007
Relay_Log_Pos: 611
Relay_Master_Log_File: master-bin.000003
Slave_IO_Running: Yes
#确保I/O和SQL线程开启,即可实现数据同步
Slave_SQL_Running: Yes
#确保I/O和SQL线程开启,即可实现数据同步
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: 326
Relay_Log_Space: 1184
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
MariaDB [(none)]>
#测试在主库上删除dbs,验证从库是否存在dbs数据库,结果如下:
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| dbs |
| hellodb |
| mydbs |
| mysql |
| performance_schema |
|
test
|
+--------------------+
7 rows
in
set
(0.01 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb |
| mydbs |
| mysql |
| performance_schema |
|
test
|
+--------------------+
6 rows
in
set
(0.00 sec)
MariaDB [(none)]>
[root@centos6 ~]
# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection
id
is 14
Server version: 5.5.32-MariaDB-log MariaDB Server
Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
Type
'help;'
or
'\h'
for
help. Type
'\c'
to
clear
the current input statement.
MariaDB [(none)]>
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| dbs |
| hellodb |
| mydbs |
| mysql |
| performance_schema |
|
test
|
+--------------------+
7 rows
in
set
(0.02 sec)
MariaDB [(none)]> drop database dbs;
Query OK, 0 rows affected (0.04 sec)
MariaDB [(none)]>
三、主从不同步时,如何进行数据同步至一致实战
描述:当主服务器已经运行一段时间,并且存在不小的数据时,则需把主服务器备份,然后在从服务器恢复,从备份时所在的位置开始复制。
1、将主服务器上的数据做完全备份
[root@centos6 ~]
# mysqldump --lock-all-tables --all-databases --flush-logs --master-data=2 >/root/all.sql
2、在从服务器上导入主服务上的完全备份,在导入时关闭I/O和SQL线程
[root@centos6 ~]
# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection
id
is 34
Server version: 5.5.32-MariaDB MariaDB Server
Copyright (c) 2000, 2013, Oracle, Monty Program Ab and others.
Type
'help;'
or
'\h'
for
help. Type
'\c'
to
clear
the current input statement.
MariaDB [(none)]>
source
/root/alren
.sql
MariaDB [testdbs]> change master to master_host=
'10.1.10.1'
,master_user=
'repluser'
,master_password=
'pass'
,MASTER_LOG_FILE=
'master-bin.000007'
,MASTER_LOG_POS=245;
Query OK, 0 rows affected (0.02 sec)
MariaDB [testdbs]> start slave;
Query OK, 0 rows affected (0.02 sec)
MariaDB [testdbs]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting
for
master to send event
Master_Host: 10.1.10.1
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000007
#已经恢复到主节点的二进制的位置
Read_Master_Log_Pos: 245
Relay_Log_File: relay-bin.000002
Relay_Log_Pos: 530
Relay_Master_Log_File: master-bin.000007
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: 245
Relay_Log_Space: 818
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)
MariaDB [testdbs]> show tables;
+-------------------+
| Tables_in_testdbs |
+-------------------+
| tbl |
+-------------------+
1 row
in
set
(0.00 sec)
MariaDB [testdbs]>
select
* from tbl;
+-------+
| name |
+-------+
| tom |
| jerry |
| lucy |
+-------+
3 rows
in
set
(0.00 sec)
MariaDB [testdbs]> use mydbs;
Database changed
MariaDB [mydbs]> show tables;
#测试数据是否和主节点一致
+-----------------+
| Tables_in_mydbs |
+-----------------+
| students |
+-----------------+
1 row
in
set
(0.00 sec)
MariaDB [mydbs]>
select
* from students;
+------+-------+
|
id
| name |
+------+-------+
| 1 | tom |
| 2 | jerry |
+------+-------+
2 rows
in
set
(0.00 sec)
MariaDB [mydbs]>
总结:此实战中最为关键主要有两步①主服务器上锁表做完全备份,并滚动日志,②从服务器上进行半道恢复
- MySQL主从复制配置实现
- MySQL主从复制配置与实现
- MySQL主从复制配置与实现
- MySql主从搭建、复制及配置实现
- mysql 主从复制 主从配置
- MySQL主从复制配置
- MySQL主从复制配置
- 配置MySQL主从复制
- MySQL主从复制配置
- mysql 主从复制配置
- MySQL主从复制配置
- mysql 主从复制配置
- MySQL主从复制配置
- MySQL主从复制配置
- MySQL主从复制配置
- MySQL主从复制配置
- MySql 主从复制配置
- 配置MySQL主从复制
- android 获取 imei号码
- C-C++练习8
- 第二节 elk搭建
- ListView的head消失页面导航栏的渐变出现和隐藏
- mysql启动报错
- MySQL主从复制配置实现
- solr5.5.3 集成mmseg4j 中文分词(二)
- freechart 简单应用
- 如何找到链表的中间元素?
- 自定义多行标签
- 数据库倒叙日记
- POJ 3061 Subsequence 尺取法
- No command 'setenv' found
- FloatingActionButton