mysql 主从同步

主从复制
 yum install mysql-server


vim /etc/my.cnf

修改  server-id=1
修改   log-bin=log
 添加 relay-log=relay-bin
 添加  relay-log-index=relay-bin.index

service mysqld restart

在主配置文件 mysql
 mysql>grant replication slave on *.* to [email protected] identified by '123456';

 

在server

yum install mysql-server


vim /etc/my.cnf
 修改 server-id=2
 添加relay-log=relay-bin
 添加relay-log-index=relay-bin.index
 修改master-host     =   192.168.0.50
 修改master-user     =   repl
 修改master-password = 123456

service mysqld restart

mysql>slave start;
mysql>show slave status\G;

mysql> show slave status\G
*************************** 1. row ***************************
             Slave_IO_State: Waiting for master to send event
                Master_Host: 192.168.0.50
                Master_User: repl
                Master_Port: 3306
              Connect_Retry: 60
            Master_Log_File: mysql-bin.000001
        Read_Master_Log_Pos: 98
             Relay_Log_File: relay-bin.000006
              Relay_Log_Pos: 235
      Relay_Master_Log_File: mysql-bin.000001
           Slave_IO_Running: Yes
          Slave_SQL_Running: Yes
            Replicate_Do_DB:


如果两行 出现yes 表示成功

如果出错 先关闭 iptables 

在检查 主的和 从的  Connect_Retry: 60
            Master_Log_File: mysql-bin.000001  是否一样


如果不一样 就修改 mysql>slave stop;
mysql>CHANGE MASTER TO MASTER_HOST='10.0.4.158',MASTER_USER='forslave', MASTER_PASSWORD='********',MASTER_LOG_FILE='materlog.000001',MASTER_LOG_POS= 45277;
mysql>slave start;

 

 

接下来 开始 创建数据库:
创建数据库:create database repl;
mysql> use repl
mysql> create table test(id int,name char(10));
Query OK, 0 rows affected (0.00 sec)
mysql> insert into test values(1,'zaq');
Query OK, 1 row affected (0.00 sec)
mysql> insert into test values(1,'xsw');
Query OK, 1 row affected (0.00 sec)
mysql> select * from test;
+------+------+
| id | name |
+-------+------+
| 1 | zaq |
| 1 | xsw |
+-------+------+


在从服务器查看是否同步过来
mysql> use repl
Database changed
mysql> select * from test;
+------+------+
| id | name |
+------+------+
| 1 | zaq |
| 1 | xsw |
+------+------+


ok 了 终于成功了

 

 

 

 

你可能感兴趣的:(数据库,mysql,职场,休闲,主从复制)