MySQL master-slave replication (super simple)

How to install the mysql database, I will not talk about it here, just talk about its master-slave replication, the steps are as follows:
1. The master and slave servers do the following respectively:
  1.1. The versions are consistent
  1.2. Initialize the table, and start mysql in the background
  1.3. Modify the root Password

2. Modify the master server master:
   #vi /etc/my.cnf
       [mysqld]
       log-bin=mysql-bin //[must] enable binary log
       server-id=222 //[must] server unique ID, the default is 1. Generally take the last segment of IP

3. Modify the slave server:
   #vi /etc/my.cnf
       [mysqld]
       log-bin=mysql-bin //[not required] enable binary log
       server-id=226 //[required ] The unique ID of the server, the default is 1, generally take the last segment

of the IP 4. Restart mysql
   /etc/init.d/mysql restart of the two servers

5. Create an account on the master server and authorize the slave:
   #/usr/local/mysql /bin/mysql -uroot -pmttang   
   mysql>GRANT REPLICATION SLAVE ON *.* to 'mysync'@'%' identified by 'q123456'; //The root account is generally not used, "%" means that all clients can be connected, as long as the account and password are correct, this You can use specific client IP instead, such as 192.168.145.226, to strengthen security.

6. Log in to the mysql of the master server and query the status of the master
   mysql>show master status;
   +-----------------+----------+- -------------+------------------+
   | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
   +------- -------------+------------+--------------+------------ ------+
   | mysql-bin.000004 | 308 | | |
   +------------------+----------+- -------------+------------------+
   1 row in set (0.00 sec)
   Note: Do not Operate the master server MYSQL to prevent the state value of the master server from changing

7. Configure the slave server Slave:
   mysql>change master to master_host='192.168.145.222',master_user='mysync',master_password='q123456',
         master_log_file='mysql-bin.000004',master_log_pos=308; //Be careful not to disconnect, there is nothing before and after 308 numbers apostrophe.

   Mysql>start slave; //Start the slave server replication function

8. Check the slave server replication function status:

   mysql> show slave status\G

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

              Slave_IO_State: Waiting for master to send event
              Master_Host: 192.168.2.222 // Master server address
              Master_User: mysync //authorized account name, try to avoid using root
              Master_Port: 3306 //database port, some versions do not have this line
              Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
              Read_Master_Log_Pos: 600 //#The position of reading the binary log synchronously, greater than or equal to Exec_Master_Log_Pos
              Relay_Log_File: ddte-relay-bin.000003
              Relay_Log_Pos: 251
              Relay_Master_Log_File: mysql-bin.000004
              Slave_IO_Running: Yes //This state must be YES
              Slave_SQL_Running: Yes // This state must be YES
                    ...

Note: The Slave_IO and Slave_SQL processes must be running normally, that is, the YES state, otherwise it is an error state (eg: one of NO is an error).

The above operation process, the master-slave server configuration is completed.
  
9. Master-slave server test:

master server Mysql, create a database, and create a table in this database to insert a piece of data:

  mysql> create database hi_db;
  Query OK, 1 row affected (0.00 sec)

  mysql> use hi_db;
  Database changed

  mysql>  create table hi_tb(id int(3),name char(10));
  Query OK, 0 rows affected (0.00 sec)
 
  mysql> insert into hi_tb values(001,'bobu');
  Query OK, 1 row affected (0.00 sec)

  mysql> show databases;
   +--------------------+
   | Database           |
   +--------------------+
   | information_schema |
   | hi_db                |
   | mysql                |
   | test                 |
   +--------------------+
   4 rows in set (0.00 sec)

从服务器Mysql查询:

   mysql> show databases;

   +--------------------+
   | Database               |
   +--------------------+
   | information_schema |
   | hi_db | //I'M here, you can see it
   | mysql |
   | test |
   +--------------------+
   4 rows in set ( 0.00 sec)

   mysql> use hi_db
   Database changed
   mysql> select * from hi_tb; //View the specific data newly added on the main server
   +------+------+
   | id | name |
   +-- ----+------+
   | 1 | bobu |
   +------+------+
   1 row in set (0.00 sec)
 
10. Finish:
    write a shell script, use Nagios monitors the two yeses of the slave (Slave_IO and Slave_SQL processes). If only one or zero yes is found, it means that there is a problem with the master and slave. Send a text message alert.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326223167&siteId=291194637