温馨提示:要看高清无码套图,请使用手机打开并单击图片放大查看。
1.文档编写目的
MySQL数据库自身提供的主从复制功能可以方便的实现数据的多处自动备份,实现数据库的扩展。多个数据备份不仅可以加强数据的安全性,通过实现读写分离还能进一步提升数据库的负载性能。本文档讲述如何实现MySQL主从复制。注:本文档实现的MySQL主备模式为Active-Passive而不是Active-Active,如果使用双活的方式,建议企业内部配备MySQL的DBA来维护MySQL。CDH集群在运行过程中,MySQL的负载并不会太高,推荐的方式是Active-Passive模式,以降低维护成本和维护难度。
内容概述
1.Master和Slave配置
2.构建主从复制
3.主从复制验证
测试环境
1.两台Linux服务器(172.31.10.118(主)/172.31.5.190),操作系统为CentOS6.5
2.MySQL5.1.73
3.采用root用户操作
前置条件
1.两个MySQL版本必须一致
2.两个MySQL已安装好,且没有任何数据
3.主MySQL必须开启bin-log日志
2.MySQL主从复制
2.1Master和Slave配置
配置文件说明:
log-bin:开启二进制日志,日志文件前缀
server-id:数据库服务的唯一标识确保标识不重复,一般设置为服务器ip的末尾数
binlog-format:设置Mysql binlog记录日志的格式(格式含:Statement、MIXED、ROW),MySQL默认使用的是Statement,推荐使用MIXED。
Master主服务配置(172.31.10.118)
修改/etc/my.conf文件,增加如下配置
[root@ip-172-31-10-118 cloudera-scm-server]# vim /etc/my.cnf [mysqld]datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.sockuser=mysql# Disabling symbolic-links is recommended to prevent assorted security riskssymbolic-links=0log-bin=mysql-binserver-id=118binlog_format=MIXED[mysqld_safe]log-error=/var/log/mysqld.logpid-file=/var/run/mysqld/mysqld.pid
Slave服务配置(172.31.5.190)
修改/etc/my.conf文件,增加如下配置
[root@ip-172-31-5-190 ~]# vim /etc/my.cnf [mysqld]datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.sockuser=mysql# Disabling symbolic-links is recommended to prevent assorted security riskssymbolic-links=0log-bin=mysql-binserver-id=190[mysqld_safe]log-error=/var/log/mysqld.logpid-file=/var/run/mysqld/mysqld.pid
修改配置后重启Master和Slave的MySQL服务。
[root@ip-172-31-5-190 ~]# service mysqld restartStopping mysqld: [ OK ]Starting mysqld: [ OK ][root@ip-172-31-5-190 ~]#
2.1构建主从复制
在Master(172.31.10.118) 主MySQL上创建一个mysnc用户
用户名:mysync 密码:mysync
GRANT REPLICATION SLAVE ON *.* TO 'mysync'@'172.31.%' IDENTIFIED BY 'mysync';FLUSH PRIVILEGES;
mysync用户必须具有REPLICATION SLAVE权限。说明一下172.31.%,这个配置是指明mysync用户所在服务器,这里%是通配符,表示IP以172.31开头的Server都可以使用mysync用户登陆Master主服务器。也可以指定固定IP。
命令行操作
[root@ip-172-31-10-118 ~]# mysql -uroot -pEnter password: Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 11Server version: 5.1.73-log Source distributionCopyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> GRANT REPLICATION SLAVE ON *.* TO 'mysync'@'172.31.%' IDENTIFIED BY 'mysync';Query OK, 0 rows affected (0.00 sec)mysql> FLUSH PRIVILEGES;Query OK, 0 rows affected (0.00 sec)
2.查看Master(172.31.10.118) MySQL二进制日志File与Position
mysql> show master status;+------------------+----------+--------------+------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |+------------------+----------+--------------+------------------+| mysql-bin. | 1121 | | |+------------------+----------+--------------+------------------+1 row in set (0.00 sec)mysql>
3.在Slave从MySQL上执行如下SQL
change master tomaster_host='172.31.10.118',master_user='mysync',master_password='mysync',master_log_file='mysql-bin.',master_log_pos=1121;
命令行执行
[root@ip-172-31-5-190 ~]# mysql -uroot -p...Server version: 5.1.73-log Source distribution...mysql> change master to -> master_host='172.31.10.118', -> master_user='mysync', -> master_password='mysync', -> master_log_file='mysql-bin.', -> master_log_pos=1121;Query OK, 0 rows affected (0.02 sec)mysql>
4.在Slave从MySQL上执行命令,启动同步
mysql> start slave;Query OK, 0 rows affected (0.00 sec)mysql>
5.在Slave MySQL上查看Slave状态
mysql> show slave status \G*************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 172.31.10.118 Master_User: mysync Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin. Read_Master_Log_Pos: 1121 Relay_Log_File: mysqld-relay-bin. Relay_Log_Pos: 251 Relay_Master_Log_File: mysql-bin. 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: 1121 Relay_Log_Space: 407 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: 0Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: 1 row in set (0.00 sec)mysql>
注意:上图标注部分显示为“Yes”则表示主从复制构建成功。
2.3主从复制验证
登录Master主MySQL上执行SQL
2.登录Slave从MySQL上执行SQL
3.在Master主MySQL上执行SQL
mysql> create database test;Query OK, 1 row affected (0.00 sec)mysql> use test;Database changedmysql> create table table1(id int, name varchar(32));Query OK, 0 rows affected (0.01 sec)mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || test |+--------------------+3 rows in set (0.00 sec)mysql> show tables;+----------------+| Tables_in_test |+----------------+| table1 |+----------------+1 row in set (0.00 sec)mysql>
4.在Slave从MySQL上执行SQL查看
mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || mysql || test |+--------------------+3 rows in set (0.00 sec)mysql> use test;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> show tables;+----------------+| Tables_in_test |+----------------+| table1 |+----------------+1 row in set (0.00 sec)mysql>
通过上述测试,Master主MySQL创建的库和表都正常的同步到Slave从MySQL。
3.备注
如何停止并删除主从同步,在Slave从MySQL上执行如下SQL
mysql> stop slave;Query OK, 0 rows affected (0.00 sec)mysql> reset slave;Query OK, 0 rows affected (0.00 sec)mysql>
注意:执行上述操作后,需要重启MySQL服务。
MySQL配置参数说明
参考文档:
推荐关注Hadoop实操,第一时间,分享更多Hadoop干货,欢迎转发和分享。
原创文章,欢迎转载,转载请注明:转载自微信公众号Hadoop实操
标签: MySQL
②文章观点仅代表原作者本人不代表本站立场,并不完全代表本站赞同其观点和对其真实性负责。
③文章版权归原作者所有,部分转载文章仅为传播更多信息、受益服务用户之目的,如信息标记有误,请联系站长修正。
④本站一律禁止以任何方式发布或转载任何违法违规的相关信息,如发现本站上有涉嫌侵权/违规及任何不妥的内容,请第一时间反馈。发送邮件到 88667178@qq.com,经核实立即修正或删除。