ponkiti's blog

主に自分用、イベント参加メモや備忘録として利用

MySQL5.5のレプリケーションでスレーブがSlave_IO_Running: Connectingとなってしまう

このサイトを参考にレプリケーションを進めたが、MySQL5.1(マスター・スレーブ共に5.1、yum install)でのレプリケーションはうまくいったのだが、MySQL5.5(ソースインストール)同士でのレプリケーションはスレーブ側でSlave_IO_Running: Connectingとなってしまった。

mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: Connecting to master
                  Master_Host: 192.168.33.12
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysqld-bin.000001
          Read_Master_Log_Pos: 249
               Relay_Log_File: localhost-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysqld-bin.000001
             Slave_IO_Running: Connecting
            Slave_SQL_Running: Yes
(省略)

結論から言うと、対応(3)にあるように、マスター側でスレーブ(外部ホスト)からの接続を許可してやればOK。

環境

  • Vagrant
  • CentOS 6.4
  • マスター・スレーブ共にMySQL5.5(ソースインストール)

対応(1)

よくよく CHANGE MASTER TO を確認してみると、MASTER_LOG_FILE の設定が「mysqld-bin.000001」となっている。今回の場合、これは「mysqld」ではなく「mysql」が正解。

修正前

CHANGE MASTER TO
MASTER_HOST='192.168.33.12',
MASTER_USER='repl',
MASTER_PASSWORD='password',
MASTER_LOG_FILE='mysqld-bin.000001',
MASTER_LOG_POS=249;

修正後

CHANGE MASTER TO
MASTER_HOST='192.168.33.12',
MASTER_USER='repl',
MASTER_PASSWORD='password',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=249;

上記の対応を行ったが、Slave_IO_Running は Connecting のままで、エラーも変わらず・・・。

mysql> SHOW SLAVE STATUS\G
(省略)
Slave_SQL_Running: Yes
(省略)
150108 23:29:30 [ERROR] Slave I/O: error connecting to master 'repl@192.168.33.12:3306' - retry-time: 60 retries: 86400, Error_code: 2003

対応(2)

マスター側でMySQLの3306番ポートを許可するようiptablesを設定してみる。iptables-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPTを追加する。

[root@localhost mysql-5.5.24]# vi /etc/sysconfig/iptables

[root@localhost mysql-5.5.24]# service iptables restart
iptables: Flushing firewall rules: [ OK ]
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Unloading modules: [ OK ]
iptables: Applying firewall rules: [ OK ]

これも効果なし。

mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.33.12
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 249
Relay_Log_File: localhost-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: No
Slave_SQL_Running: Yes
(省略)
Last_IO_Errno: 1593
Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids; these ids must be different for replication to work (or the --replicate-same-server-id option must be used on slave but this does not always make sense; please check the manual before using it).
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
1 row in set (0.00 sec)

対応(3)

スレーブからマスターのMySQLに接続できるか確認すると、アクセスが拒否された。どうやらマスターのMySQLへの接続権限がないらしい。

[root@localhost ~]# mysql -h 192.168.33.12 -u root
ERROR 1045 (28000): Access denied for user 'root'@'192.168.33.13' (using password: NO)

マスター側でスレーブ(外部ホスト)からの接続を許可する。(参照サイト

grant all privileges on client_data.* to root@"192.168.33.%" identified by '' with grant option;

再度、スレーブからマスターのMySQLに接続できるか確認すると、接続できた。

[root@localhost ~]# mysql -h 192.168.33.12 -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 19
Server version: 5.5.24-log Source distribution

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

スレーブで Slave_IO_Running を確認してみると、「Slave_IO_Running: Yes」となった。

SHOW SLAVE STATUS\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.33.12
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 746
               Relay_Log_File: localhost-relay-bin.000007
                Relay_Log_Pos: 253
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB:
          Replicate_Ignore_DB:
           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: 746
              Relay_Log_Space: 413
              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:
  Replicate_Ignore_Server_Ids:
             Master_Server_Id: 1
1 row in set (0.00 sec)

マスターで新規作成した「sample」データベースも、スレーブで反映された。

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sample             |
| test               |
+--------------------+
5 rows in set (0.00 sec)

ちなみに、対応(2)のiptablesの設定をせずともレプリケーションには問題なかった。

スレーブからマスターのMySQLに接続できるか確認した際に、下記のエラーメッセージが出力された場合は、対応(2)のようにiptablesの設定をする必要がある。

[root@localhost ~]# mysql -h 192.168.33.12 -u root
ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.33.12' (113)

参照