手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

MySQL互为主从复制详解----mysql-mm

[日期:2012-02-20] 来源:Linux社区  作者:anljf [字体:大 中 小]

B:允许A通过repl1账户与B同步数据
MySQL> grant replication client on *.* to 'repl1'@'A_IP' identified by 'PASSWD';
mysql> flush privileges;
mysql> select * from mysql.user where host='repl1'@'A_IP'\G;
*************************** 1. row ***************************
                 Host: 211.100.97.246
                 User: repl1
             Password: *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9
          Select_priv: Y
          Insert_priv: Y
          Update_priv: Y
          Delete_priv: Y
          Create_priv: Y
            Drop_priv: Y
          Reload_priv: Y
        Shutdown_priv: Y
         Process_priv: Y
            File_priv: Y
           Grant_priv: N
      References_priv: Y
           Index_priv: Y
           Alter_priv: Y
         Show_db_priv: Y
           Super_priv: Y
Create_tmp_table_priv: Y
     Lock_tables_priv: Y
         Execute_priv: Y
      Repl_slave_priv: Y
     Repl_client_priv: Y
     Create_view_priv: Y
       Show_view_priv: Y
  Create_routine_priv: Y
   Alter_routine_priv: Y
     Create_user_priv: Y
             ssl_type:
           ssl_cipher:
          x509_issuer:
         x509_subject:
        max_questions: 0
          max_updates: 0
      max_connections: 0
 max_user_connections: 0
1 row in set (0.00 sec)
授权以后需要测试
A: /usr/local/mysql/bin/mysql -h'B_IP' -urepl1 -p
B: /usr/local/mysql/bin/mysql -h'A_IP' -urepl2 -p

两台机器上均重启mysql
killall mysqld
ps aux |grep mysql
/usr/local/mysql/bin/mysqld_safe &
ps aux |grep mysql

 

 

进入MYSQL的SHELL
/usr/local/mysql/bin/mysql -uroot -p

A:
服务器锁表(锁表状态下不能终止mysql进程,否则会失败)
mysql> flush tables with read lock\G;
Query OK, 0 rows affected (0.01 sec)
----------------

查看 A 服务器主机状态(记录二进制开始文件,位置)
mysql> show master status\G;
*************************** 1. row ***************************
            File: mysql-bin.000005
        Position: 106
    Binlog_Do_DB: test
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)
----------------

修改A服务器配置
mysql> change master to
    -> master_host='211.100.97.250',
    -> master_user='repl2',
    -> master_password='123456',
    -> master_log_file='mysql-bin.000014',
    -> master_log_pos=98;
Query OK, 0 rows affected (0.01 sec)

说明:
master_host表示主机B(250)是A(246)的master
master_user表示允许A(246)上的账户repl1连接到master进行复制,建议两台主机的授权用户和密码完全相同。
master_password 表示授权用户repl1的密码
master_log_file 表示master上日志文件的名称
master_log_pos  表示日志文件的位置
----------------
mysql> slave stop;
mysql> change master to master_host='B_IP', master_user='repl1', master_password='123456', master_log_file='mysql-bin.000001', master_log_pos=106;

然后启动slave
mysql> slave start;

启动之后查看slave的状态
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 211.100.97.250
                  Master_User: repl1
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 106
               Relay_Log_File: XKWB5510-relay-bin.000002
                Relay_Log_Pos: 251
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: test
          Replicate_Ignore_DB: mysql
           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: 106
              Relay_Log_Space: 409
              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:
1 row in set (0.00 sec)

查看相关进程
mysql> show processlist\G;
*************************** 1. row ***************************
     Id: 4
   User: root
   Host: localhost
     db: NULL
Command: Query
   Time: 0
  State: NULL
   Info: show processlist
*************************** 2. row ***************************
     Id: 18
   User: system user
   Host:
     db: NULL
Command: Connect
   Time: 100
  State: Waiting for master to send event
   Info: NULL
*************************** 3. row ***************************
     Id: 19
   User: system user
   Host:
     db: NULL
Command: Connect
   Time: 100
  State: Has read all relay log; waiting for the slave I/O thread to update it
   Info: NULL
*************************** 4. row ***************************
     Id: 21
   User: repl2
   Host: 211.100.97.250:34536
     db: NULL
Command: Binlog Dump
   Time: 19
  State: Has sent all binlog to slave; waiting for binlog to be updated
   Info: NULL
4 rows in set (0.00 sec)
----------------------
同步两个数据库的基础库

----------------
解锁服务器
mysql> unlock tables;
----------------
mysql> use test;
mysql> show tables;
Empty set (0.00 sec)
----------------
mysql> create table t11_replicas
    -> (id int not null auto_increment primary key,
    -> str varchar(255) not null) engine myisam;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into t11_replicas(str) values
    -> ('This is a master to master test table');
Query OK, 1 row affected (0.00 sec)
----------------
mysql> show tables;
----------------
mysql> select * from t11_replicas;

------------------------------------------------------------------------------------------------
B:
mysql> show master status\G;
*************************** 1. row ***************************
            File: mysql-bin.000014
        Position: 98
    Binlog_Do_DB: test
Binlog_Ignore_DB: mysql
1 row in set (0.00 sec)
----------------
mysql> stop slave;
mysql> change master to master_host='A_IP', master_user='repl2', master_password='123456', master_log_file='mysql-bin.000005',master_log_pos=106;
mysql> start slave;
----------------
mysql> show processlist\G;
*************************** 1. row ***************************
     Id: 3
   User: root
   Host: localhost
     db: NULL
Command: Query
   Time: 0
  State: NULL
   Info: show processlist
*************************** 2. row ***************************
     Id: 15
   User: repl1
   Host: 211.100.97.246:51840
     db: NULL
Command: Binlog Dump
   Time: 101
  State: Has sent all binlog to slave; waiting for binlog to be updated
   Info: NULL
*************************** 3. row ***************************
     Id: 16
   User: system user
   Host:
     db: NULL
Command: Connect
   Time: 20
  State: Waiting for master to send event
   Info: NULL
*************************** 4. row ***************************
     Id: 17
   User: system user
   Host:
     db: NULL
Command: Connect
   Time: 20
  State: Has read all relay log; waiting for the slave I/O thread to update it
   Info: NULL
4 rows in set (0.00 sec)
----------------
mysql> show slave status\G;
----------------
mysql> use test;
Database changed
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)

----------------

linux
相关资讯       MySQL数据库教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款