mysql replication

1. master mysql 中修改/etc/my.cnf 在mysqld下添加如下两行:

log-bin=/var/lib/mysql/mysql-bin.log
server-id=1

#binlog-do-db=DB1
#binlog-do-db=DB2     #如果备份多个数据库,重复设置这个选项即可

2.重启mysql,添加slave replication 用户

GRANT REPLICATION SLAVE ON *.* TO 'slave'@192.168.100.10' IDENTIFIED BY 'slave_password';

FLUSH PRIVILEGES;

FLUSH TABLES WITH READ LOCK;
SHOW MASTER STATUS;

 

+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |      890 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

unlock tables;

记下 file名和binlog的位置.

4.在slave mysql上操作.

编辑从服务器的配置文件:/etc/my.cnf
[mysqld]
#sync data
server-id=2            #注意不能重复
master-host=192.168.100.10
master-user=slave

master-password=slave_password
master-port=3306
master-connect-retry=60
#replicate-do-db=DB1
#replicate-do-db=DB2

5.重启slave mysql .

stop slave;

CHANGE MASTER TO MASTER_HOST='192.168.100.13', MASTER_USER='slave', MASTER_PASSWORD='slave_password', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=890;

start slave;

5.在master mysql上创建数据库测试,从库是否同步.或者show slave status\G 查看

Slave_IO_Running: Yes
Slave_SQL_Running: Yes

两个yes为同步.

补充:
在从服务器上使用show slave status\G
Slave_IO_Running,为No,
    则说明IO_THREAD没有启动,请执行start slave io_thread
    Slave_SQL_Running为No
    则复制出错,查看Last_error字段排除错误后执行start slave sql_thread

    查看Slave_IO_State字段空 //复制没有启动
    Connecting to master//没有连接上master
    Waiting for master to send event//已经连上

    主服务器上的相关命令:
    show master status
    show slave hosts
    show logs
    show binlog events
    purge logs to 'log_name'
    purge logs before 'date'
reset master(老版本flush master)
    set sql_log_bin=
    从服务器上的相关命令:
    slave start
    slave stop
    SLAVE STOP IO_THREAD //此线程把master段的日志写到本地
    SLAVE start IO_THREAD
    SLAVE STOP SQL_THREAD //此线程把写到本地的日志应用于数据库
    SLAVE start SQL_THREAD
    reset slave
    SET GLOBAL SQL_SLAVE_SKIP_COUNTER
    load data from master
show slave status(SUPER,REPLICATION CLIENT)
    CHANGE MASTER TO MASTER_HOST=, MASTER_PORT=,MASTER_USER=, MASTER_PASSWORD= //动态改变master信息
    PURGE MASTER [before 'date'] 删除master端已同步过的日志

你可能感兴趣的:(mysql,数据库,职场,Replication,休闲)