首页 > 代码库 > MySQL 主从复制原理及搭建
MySQL 主从复制原理及搭建
一.主从复制的工作过程:
二.MySQL复制类型
基于SQL语句的复制
基于行的复制
混合复制
三.实验环境
OS:CentOS 6.5 x64
master:192.168.0.134
slave:192.168.0.135
三.配置主从复制
1.配置时间同步
master:配置为时间服务器
[root@master ~]# yum install ntp 编辑/etc/ntp.conf 添加如下两行: server 127.127.1.0 fudge 127.127.1.0 stratm 8 service ntpd start
slave:同步master时间
[root@slave ~]# yum install ntpdate [root@slave ~]# ntpdate 192.168.0.134 6 May 06:37:58 ntpdate[6653]: adjust time server 192.168.0.134 offset -0.469705 sec
2.安装MySQL
slave 和 master:
[root@master ~]# yum install mysql-server mysql -y [root@master ~]# /etc/rc.d/init.d/mysqld start [root@master ~]# chkconfig mysqld on [root@master ~]# mysqladmin -u root password "123.com"
3.编辑配置文件
master:
编辑/etc/my.conf添加如下几行 server-id=134 #设置id,主从不同 log-bin=master-bin #开启二进制日志 log-slave-update=true 重启MySQL服务 [root@smaster~]# service mysqld restart
slave:
编辑/etc/my.conf添加如下几行 server-id=135 relay-log=relay-log-bin relay-log-index=slave-relay-bin.index read-only=1 #这里可以设置mysql为仅读,不对root生效 重启MySQL服务 [root@slave ~]# service mysqld restart
3.登录mysql,给slave授权
master:
mysql> grant replication slave on *.* to ‘slave‘@‘192.168.0.%‘ identified by ‘123456‘; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> show master status; +-------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +-------------------+----------+--------------+------------------+ | master-bin.000001 | 181 | | | +-------------------+----------+--------------+------------------+ 1 row in set (0.00 sec)
# File:日志名 Position:偏移量
4.登录MySQL,配置同步
slave:
mysql> change master to master_host=‘192.168.0.134‘,master_user=‘slave‘,master_password=‘123456‘,master_log_file=‘master-bin.000001‘,master_log_pos=181; Query OK, 0 rows affected (0.12 sec) mysql> start slave; 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: 192.168.0.134 Master_User: slave Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000001 Read_Master_Log_Pos: 181 Relay_Log_File: relay-log-bin.000002 Relay_Log_Pos: 252 Relay_Master_Log_File: master-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: 181 Relay_Log_Space: 405 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)
#查看同步状态Slave_IO和Slave_SQL是YES说明主从同步成功。
四.测试
1.在master上面新建一个数据库
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | test | +--------------------+ 3 rows in set (0.00 sec) mysql> create database guoxh charset ‘utf8‘; Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | guoxh | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec)
2.在slave上面查看数据库
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | guoxh | | mysql | | test | +--------------------+ 4 rows in set (0.00 sec)
#在slave上面可以看到刚才新建的数据库,则说明主从复制配置成功。
本文出自 “隔壁老大哥” 博客,请务必保留此出处http://guoxh.blog.51cto.com/10976315/1922643
MySQL 主从复制原理及搭建
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。