- 两个MySQL版本必须一致
- 两个MySQL已安装好,且没有任何数据
- 主MySQL必须开启bin-log日志
- 两台Linux服务器(168.32.11.116(主)/168.32.6.120),操作系统为CentOS6.5
- MySQL版本:5.1.73
- 用root用户操作
MySQL主从复制
- 1Master和Slave配置
[root@ip-168-32-11-116 cloudera-scm-server]# vim /etc/my.cnf [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 log-bin=mysql-bin server-id=118 binlog_format=MIXED [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid ##配置文件说明: - log-bin:开启二进制日志,日志文件前缀 - server-id:数据库服务的唯一标识确保标识不重复,一般设置为服务器ip的末尾数 - binlog-format:设置Mysql binlog记录日志的格式(格式含:Statement、MIXED、ROW),MySQL默认使用的是Statement,推荐使用MIXED。 - Master主服务配置(168.32.11.116) - 修改/etc/my.conf文件,增加如下配置
- Slave服务配置(168.32.6.120)
- 修改/etc/my.conf文件,增加如下配置
[root@ip-168-32-6-120 ~]# vim /etc/my.cnf [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 log-bin=mysql-bin server-id=190 [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid
- 修改配置后重启Master和Slave的MySQL服务。
[root@ip-168-32-6-120 ~]# service mysqld restart Stopping mysqld: [ OK ] Starting mysqld: [ OK ] [root@ip-168-32-6-120 ~]#
- 构建主从复制
- 在Master(168.32.11.116) 主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-168-32-11-116 ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 11 Server version: 5.1.73-log Source distribution Copyright (c) 2000, 2013, 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 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)
- 查看Master(168.32.11.116) 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>
- 在Slave从MySQL上执行如下SQL
change master to master_host='168.32.11.116', master_user='mysync', master_password='mysync', master_log_file='mysql-bin.000003', master_log_pos=1121;
- 命令行执行
[root@ip-168-32-6-120 ~]# mysql -uroot -p
...
Server version: 5.1.73-log Source distribution
...
mysql> change master to
-> master_host='168.32.11.116',
-> 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>
- 在Slave从MySQL上执行命令,启动同步
mysql> start slave; Query OK, 0 rows affected (0.00 sec) mysql>
- 在Slave MySQL上查看Slave状态
- 注意:【 Slave_IO_Running: Yes】和【Slave_SQL_Running: Yes】显示为“Yes”则表示主从复制构建成功。
mysql> show slave status G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 168.32.11.116
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: 0
Master_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>
- 主从复制验证
- 登录Master主MySQL上执行SQL
- 登录Slave从MySQL上执行SQL
- 在Master主MySQL上执行SQL
mysql> create database test; Query OK, 1 row affected (0.00 sec) mysql> use test; Database changed mysql> 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>
- 在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 names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | table1 | +----------------+ 1 row in set (0.00 sec) mysql>
- 如何停止并删除主从同步,在Slave从MySQL上执行如下SQL
- 注意:执行下述操作后,需要重启MySQL服务。
mysql> stop slave; Query OK, 0 rows affected (0.00 sec) mysql> reset slave; Query OK, 0 rows affected (0.00 sec) mysql>
大数据视频推荐:
CSDN
大数据语音推荐:
企业级大数据技术应用
大数据机器学习案例之推荐系统
自然语言处理
大数据基础
人工智能:深度学习入门到精通



