mysql replication

1.  mysql 复制工作方式:
          a.主服务器把数据更改记录到二进制日志中
          b.从服务器把主服务器的二进制日志事件copy到自己的中继日志中(从服务器会开启一个I/O线程来转储日志)
          c.从服务器重放中继日志(从服务器开启一个SQL线程,提取、重放中继日志,写到自己的磁盘上)
2.复制实现的过程:
           OS:centos 6.2
           Master:192.168.0.1
           Salve:192.168.0.2
    Master:
          yum install -y mysql mysql-server
          /etc/init.d/mysqld start
          mysql
          mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+
3 rows in set (0.00 sec)
        
         
   Slave:
       yum install -y mysql mysql-server
          /etc/init.d/mysqld start
          mysql
          mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+
3 rows in set (0.00 sec)


     简单可以分为3步:1.在master上创建复制账号
                     2.配置主slave服务器
                     3.配置slave服务器连接master,复制
     (master)1.mysql> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO miss@'192.168.0.%' IDENTIFIED BY '123456';(实际上复制只需要REPLICATION CLIENT权限,REPLICATION SLAVE 权限是为了监视和管理复制账号的)
               2.master:
mysqld段添加如下:
log_bin=mysql-bin
server_id=10(id号比从服务器的大就行了)
重启master:/etc/init.d/mysqld restart

 mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |      106 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
                 slave:
mysqld段添加如下:
log_bin=mysql-bin(这个不是必选的)
server_id=2
relay_log=mysql-relay-bin
log_slave_updates=1(这个也是可选的,将复制记录到自己的二进制日志中)
read_only=1(可选,阻止没有权限的用户更改数据)
重启slave:/etc/init.d/mysqld restart



3.启动复制:(salve)mysql> CHANGE MASTER TO MASTER_HOST='192.168.0.1',
    -> MASTER_USER='miss',
    -> MASTER_PASSWORD='123456',
    -> MASTER_LOG_FILE='mysql-bin.000001',
    -> MASTER_LOG_POS=0;(0表示日志开头)
Query OK, 0 rows affected, 3 warnings (0.17 sec)


查看slave设置是否正确mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State:
                  Master_Host: 192.168.16.133
                  Master_User: miss
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 4
               Relay_Log_File: mysql-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: No 、lave_SQL_Running: No(显示从服务器没有运行)
               
         mysql> START SLAVE;(开始复制)
       此时Slave_IO_State 、Slave_IO_Running: No 、lave_SQL_Running: No不再为no


如何验证:master:CREATE DATABASE XX;
         slave:mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
| XX         |
+--------------------+
4 rows in set (0.00 sec)
哈哈。。。复制成功了

     

你可能感兴趣的:(mysql,Replication)