操作CDH元数据库MySQL主备详细教程
下文主要给大家带来操作CDH元数据库MySQL主备详细教程,希望这些内容能够带给大家实际用处,这也是我编辑操作CDH元数据库MySQL主备详细教程这篇文章的主要目的。好了,废话不多说,大家直接看下文吧。
- 内容概述
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.000003 | 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.000003',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.000003', -> 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.000003 Read_Master_Log_Pos: 1121 Relay_Log_File: mysqld-relay-bin.000002 Relay_Log_Pos: 251 Relay_Master_Log_File: mysql-bin.000003 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服务。
对于以上关于操作CDH元数据库MySQL主备详细教程,大家是不是觉得非常有帮助。如果需要了解更多内容,请继续关注我们的行业资讯,相信你会喜欢上这些内容的。