mysql主从配置(master-slaver)

1.master机器上

mysql> GRANTREPLICATION SLAVE ON *.* to 'backup'@'192.168.101.103' identified by'password';                    

ERROR 2006 (HY000): MySQL server has gone away

No connection. Tryingto reconnect...

Connectionid:    3

Currentdatabase: *** NONE ***


Query OK, 0 rowsaffected (0.01 sec)


mysql> showmaster status;

ERROR 2006 (HY000): MySQL server has gone away

No connection.Trying to reconnect...

Connectionid:    4

Current database:*** NONE ***


Empty set (0.00sec)

--- 解决办法 ---

[root@test3data]# vim /opt/mysql/my.cnf

binlog_format           = mixed

log-bin                 = /opt/mysql/log/mysql-bin

max_binlog_size         = 128M

[root@test3data]# mysqladmin -S /tmp/mysql.sock shutdown

        [root@test3data]# /opt/mysql/bin/mysqld --defaults-file=/opt/mysql/my.cnf &

        [root@test3data]# mysql -S /tmp/mysql.sock

        mysql>show master status;   //正常状态

+------------------+----------+--------------+------------------+

| File            | Position |Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000009 |      107 |              |                  |

+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)



2.slave机器上

[root@test3 gymysql]# vim my.cnf

server-id = 10000    //确保唯一

[root@test3 gymysql]# mysqladmin -S /tmp/gymysql.sockshutdown

[root@test3 gymysql]# /opt/gymysql/bin/mysqld--defaults-file=/opt/gymysql/my.cnf &

[root@test3 ~]# mysql -S /tmp/gymysql.sock

mysql> change master tomaster_host='192.168.101.103', master_user='backup',master_password='password', master_log_file='mysql-bin.000011',master_log_pos=107;  

Query OK, 0 rows affected (0.50 sec)

mysql> start slave;

Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G

***************************1. row ***************************

              Slave_IO_State: Waiting formaster to send event

                 Master_Host: 192.168.101.103

                 Master_User: backup

                 Master_Port: 3306

               Connect_Retry: 60

             Master_Log_File: mysql-bin.000011

         Read_Master_Log_Pos: 107

              Relay_Log_File:gymysql-relay-bin.000002

               Relay_Log_Pos: 253

       Relay_Master_Log_File: mysql-bin.000011

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: 107

             Relay_Log_Space: 411

             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: 1633306

1 row in set (0.00 sec)

*****************************************

其中Slave_IO_Running: YesSlave_SQL_Running: Yes都必须为yes才算正常。


验证:master

        mysql>create database first_db;

        mysql>use first_db;

Database changed

mysql> createtable first_table(id int(3),name char(10));

Query OK, 0 rows affected (0.32 sec)


mysql> insertinto first_table values(001,'myself');

Query OK, 1 row affected (0.00 sec)


mysql> showdatabases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| first_db           |

| mysql              |

| performance_schema |

| test               |

+--------------------+

5 rows in set (0.00 sec)


mysql> select* from first_db.first_table;

+------+--------+

| id  | name   |

+------+--------+

|   1 | myself |

+------+--------+

1 row in set (0.00 sec)


Slave

[root@test3 ~]# mysql -S /tmp/gymysql.sock

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| first_db           |

| mysql              |

| performance_schema |

| test               |

+--------------------+

5 rows in set (0.00 sec)


mysql> select * fromfirst_db.first_table;

+------+--------+

| id  | name   |

+------+--------+

|   1 | myself |

+------+--------+

1 row in set (0.00 sec)


如果主服务器已经存在应用数据,则在进行主从复制时,需要做以下处理:
(1)
主数据库进行锁表操作,不让数据再进行写入动作
mysql> FLUSH TABLES WITH READ LOCK;

(2)查看主数据库状态
mysql> show master status;

(3)记录下 FILE Position 的值。
将主服务器的数据文件(整个/opt/mysql/data目录)复制到从服务器,建议通过tar归档压缩后再传到从服务器解压。

(4)取消主数据库锁定
mysql> UNLOCK TABLES;


你可能感兴趣的:(mysql,主从,master-slaver)