Mysql master-slave configuration (super simple)

Keywords: MySQL Database shell

How to install mysql database, let's not say here, just say its master-slave replication, the steps are as follows:

1. The master and slave servers perform the following operations:
1.1. Consistent Version
1.2. Initialize the table and start mysql in the background
1.3. Modify the password of root

2. Modify master server:

   #vi /etc/my.cnf
       [mysqld]
       log-bin=mysql-bin   //[Must] Enable binary logging
       server-id=2     //[Must] Server unique ID, default is 1, generally take the last IP segment

3. Modify slave server:

   #vi /etc/my.cnf
       [mysqld]
       log-bin=mysql-bin   //[Not necessarily] Enable binary logging
       server-id=3      //[Must] Server unique ID, default is 1, generally take the last IP segment

4. Restart mysql of two servers

   /etc/init.d/mysql restart
   #phpstudy restart

5. Establish an account on the primary server and authorize slave:

   #/usr/local/mysql/bin/mysql -uroot -pmttang  
   mysql>GRANT REPLICATION SLAVE ON *.* to 'mysync'@'%' identified by 'q123456'; 

// Usually no root account is used,%. It means that all clients may connect, as long as the account number and password are correct, the specific client IP can be used here instead, such as 192.168.145.226, to enhance security. mysync writes slave IP

6. Log on to the mysql of the main 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 primary server MYSQL after this step to prevent the change of the status value of the primary server.

7. Configure 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 are no single quotation marks around 308 digits.
   Mysql>start slave;    //Start the replication function from the server

8. Check the status of replication function from server:

   mysql> show slave status\G

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

              Slave_IO_State: Waiting for master to send event
              Master_Host: 192.168.2.222  //Primary Server Address
              Master_User: mysync   //Authorize account names and avoid root as much as possible
              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     //# Synchronized reading of binary logs, 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: Slave_IO and Slave_SQL processes must run normally, that is, YES state, otherwise they are all wrong states (e.g., one of NO is wrong).

The master-slave server configuration is completed.

9. Master-slave server testing:

Mysql, the main server, establishes the database and inserts a data into the table in the library:

  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)

//Query from server Mysql:

   mysql> show databases;

   +--------------------+
   | Database               |
   +--------------------+
   | information_schema |
   | hi_db                 |       //I'M here,You 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 added on the primary server
   +------+------+
   | id   | name |
   +------+------+
   |    1 | bobu |
   +------+------+
   1 row in set (0.00 sec)

10. Complete:
Write a shell script and use nagios to monitor two yes (Slave_IO and Slave_SQL processes) of slave. If only one or zero yes is found, it indicates that the master and slave have problems. Send a short message alert.

The main modification configuration restarts mysql to query the status of user privileges  

Restart mysql from the modified configuration to open replication query status based on authorized ID access

Posted by Japet on Sun, 09 Jun 2019 16:16:18 -0700