Welcome 微信登录

首页 / 数据库 / MySQL / MySQL主从复制

一、准备1、同步时间Ntpdate 时间服务器ip2、主从节点可以互相解析# vim /etc/hosts172.16.5.11 www.a.com a172.16.5.12 www.b.com b3、建立双机互信# ssh-keygen -t rsa -P ‘’# ssh-copy-id root@172.16.5.11二、修改配置文件,在两个节点上mysql初始化完之后修改。1、主服务器配置文件修改Server-id = 10Binlog_format=mixedsync-binlog = ON 安装完了之后配置,不要再初始化数据文件的时候启用2、从服务器配置文件修改Server -id = 20Read-only=1Relay-log = /mydata/data/relay-binRelay-log-index = /mydata/data/relay-bin.index 中继日志索引,这个可以不启用,中继日志启动之后,自动开启,开启是为了自定义它的目录skip-slave-start=1# log-bin = /mysql-bin 关闭二进制日志三、实现主从复制1、在主服务器上创建一个只有复制权限的用户mysql> grant replication slave,replication client on *.* to "test"@"%" identified by "pass";
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 |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 |      341 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.01 sec)2、从服务器上启动主从复制功能mysql> change master to master_host="172.16.5.11", master_user="user",
master_password="pass",master_log_file="mysql-bin.000003",master_log_pos=341;这里最好指定位置,否则可能会报错,不如直接指定,来的省事。mysql> start slave;
也可以单独启动线程,但start slave 默认会将连个线程同时启动
mysql> start io_thread;
mysql> start sql_thread;
mysql> show slave statusG
*************************** 1. row ***************************
              Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.5.11
                  Master_User: user
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 341
              Relay_Log_File: relay-bin.000004
                Relay_Log_Pos: 253
        Relay_Master_Log_File: mysql-bin.000004
            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: 188
              Relay_Log_Space: 403
              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)3、在中服务器上创建数据库,看从服务器能否同步。主服务器mysql> create database db0;
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database          |
+--------------------+
| information_schema |
| DB1                |
| db0                |
| mysql              |
| performance_schema |
| test              |
+--------------------+
6 rows in set (0.00 sec)从服务器mysql> show databases;
+--------------------+
| Database          |
+--------------------+
| information_schema |
| DB1                |
| db0                |
| mysql              |
| performance_schema |
| test              |
+--------------------+
6 rows in set (0.01 sec)发现已经同步过来了。推荐阅读:Ubuntu下Nginx做负载实现高性能WEB服务器5---MySQL主主同步 http://www.linuxidc.com/Linux/2012-06/61687p5.htm生产环境MySQL主主同步主键冲突处理 http://www.linuxidc.com/Linux/2013-07/86890.htmMySQL主从失败 错误Got fatal error 1236 http://www.linuxidc.com/Linux/2012-02/54729.htmMySQL主从复制,单台服务器上实施 http://www.linuxidc.com/Linux/2013-03/81913.htm 继续阅读本文的精彩内容请看第2页: http://www.linuxidc.com/Linux/2013-09/90411p2.htm
  • 1
  • 2
  • 下一页
超详细的Data Guard之physical standby 搭建Data Guard 之RMAN备份搭建物理standby相关资讯      MySQL主从复制  MySQL主从 
  • MySQL主从环境下存储过程,函数,  (今 07:26)
  • XtraBackup构建MySQL主从环境  (09月01日)
  • MySQL主从复制原理应用基础  (08月27日)
  • CentOS搭建MySQL主从复制,读写分  (09月12日)
  • MySQL主从复制出现1205错误  (08月31日)
  • MySQL 主从复制原理  (08月22日)
本文评论 查看全部评论 (0)
表情: 姓名: 字数