zoukankan      html  css  js  c++  java
  • MySQL主从复制

    ----------主数据库  ---------从数据库
              192.168.1.1           192.168.1.2

    一、准备工作

    1、关闭防火墙

    [root@localhost ~]# service iptables stop

    [root@localhost ~]# chkconfig iptables off

    二、时间同步

    1、在主数据库上安装ntp时间服务

    [root@localhost ~]# yum -y install ntp

    [root@localhost ~]# vim /etc/ntp.conf

    添加:

    server 127.127.1.0

    fudge 127.127.1.0 stratum 8

    [root@localhost ~]# service ntpd restart

    2、在从数据库上安装ntp时间服务

    [root@localhost ~]# yum -y install ntpdate

    [root@localhost ~]# ntpdate 192.168.1.1

    三、主从配置

    1、主数据库服务器配置

    [root@localhost ~]# vim /etc/my.cnf

    添加:去掉前面#号之后再去修改

    server-id=11 //修改

    log-bin=master-bin //修改

    log-slave-updates=true //添加

    [root@localhost ~]# service mysqld restart

    [root@localhost ~]# mysql -u root -p123.com

    mysql> grant replication slave on *.* to 'slave'@'192.168.1.%' identified by '123.com';   #slave为用户,123.com为密码

    mysql> flush privileges;

    mysql> show master status;

    +------------------+----------+--------------+------------------+
    | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
    +------------------+----------+--------------+------------------+
    | mysql-bin.000001 |      107 |              |                  |
    +------------------+----------+--------------+------------------+

    mysql> quit

    2、从数据库服务器配置

    [root@localhost ~]# vim /etc/my.cnf

    添加:

    server-id=22 //修改

    relay-log=relay-log-bin //添加

    relay-log-index=slave-relay-bin.index //添加

    注意:主—从的ID不能相同。

    [root@localhost ~]# service mysqld restart

    [root@localhost ~]# mysql -u root -p123.com

    mysql> change master to master_host='192.168.1.1',master_user='slave',master_password='123.com',master_log_file='mast er-bin.000001',master_log_pos=107;

    mysql> start slave;

    mysql> show slave statusG     //结尾不能加; 否则或报错。

    Slave_IO_Running: Yes  //确保为YES

    Slave_SQL_Running: Yes  //确保为YES

    mysql> quit

     

    问题说明:

    1、如果出现ERROR: No query specified

    则:原因是语法错误,去掉语句后的;号。

    2、如果bin-log日志不更新,或者无法进行数据同步,

    则:删除bin-log日志,重新启动服务即可。

    3、如果出现

    slave_io_running: connecting

    Slave_SQL_Running: Yes   问题通常是:

    网络不通、密码不对、pos不正确

    四、验证:

    1)主数据库服务器:

    [root@localhost ~]# mysql -u root -p123.com

    mysql> show databases;

    mysql> create database hehe;

    mysql> show databases; mysql> use hehe;

    mysql> create table biao1 (id int(5),name char(12));

    mysql> show tables;

    mysql> quit

     

    2)从数据库服务器:

    [root@localhost ~]# mysql -u root -p123.com

    mysql> show databases;

    mysql> show databases; mysql> use hehe

    mysql> show tables;

    mysql> quit

     

  • 相关阅读:
    BZOJ 2034 【2009国家集训队】 最大收益
    vijos P1780 【NOIP2012】 开车旅行
    BZOJ 2115 【WC2011】 Xor
    BZOJ 3631 【JLOI2014】 松鼠的新家
    BZOJ 4717 改装
    BZOJ 2957 楼房重建
    BZOJ 4034 【HAOI2015】 T2
    BZOJ 1834 【ZJOI2010】 network 网络扩容
    BZOJ 2440 【中山市选2011】 完全平方数
    BZOJ 2733 【HNOI2012】 永无乡
  • 原文地址:https://www.cnblogs.com/li1204008978/p/8340997.html
Copyright © 2011-2022 走看看