Mysql online from new or re-call the shots

1 Introduction

Mysql database before to call the shots from, are doing in the case of the primary server to stop taking. But a recent project has been on-line for a few days, but also a single database server to run a few days before deciding to give the Mysql server to do a master-slave architecture, a simple master-slave architecture.

The best project can be completed in a non-stop service Mysql main case from the building. And later turned over some information, you can really find online add or re-call the shots from the method.

In fact, we used to stop taking shots from the main purpose is to lock the table, trying to find master_log_file and master_log_pos two parameters. If these two parameters method in case of non-stop service, can be determined, establish the main line from an architectural feature, it can be achieved.

2. Server environment and version

系统: CentOS7.5
Mysql: 5.6.x
主端: 172.188.26.221
从端: 172.188.26.229

3. Prerequisites

Note: The main premise of the end stop service is that it has opened a bin-log log! !

If you have not turned on bin-log log at the main library, there is no way after new built, because the configuration bin-log log, main library must be restarted to take effect. However, if the situation is heavy shots library, before it proves to be done from the main, but may need to redo the master-slave fail. This case, do not need to restart the main library, as long as the backup of the database again, you can rebuild from the library.

Let's continue to talk about a specific primary process from new.

Modify the my.cnf configuration file in the master library, add open bin-log log format by row, note that server-id, and its value as prepared either end of the main end, generally used in the last paragraph ip.

server-id=221
log-bin=/data/mysqldata/mysql-bin
binlog-format=ROW

If you previously opened the bin-log function, it would not have changed.

Followed by a backup user authorization operation on the primary library:

mysql>grant replication slave on *.* to 'repel'@'172.188.26.229' identified by 'password';

Authorization from ip address to the library, the user name of the backup that repel, do not recommend using 'repel' @ '%' authorized to operate in this way, mainly to security issues, ip restrictions whitelist authorization.

The next step is to guide the main library in the database will be backed up:

  • With mysqldump way, the following is to export the entire database:
mysqldump -uroot -p  --single-transaction --no-autocommit --master-data=2 -A >test2.sql
  • With mysqldump way, if there is more than a business project library on the master Mysql, but want to export only one service database, called the hypothesis feitian database:

    mysqldump -uroot -p  --single-transaction --no-autocommit --master-data=2  feitian > feitian_dump.sql

There is a critical parameter "--master-data = 2", this parameter can help us rebuild the master from the online database.

After exporting the database, it will spread from the server library, how to pass, everyone relaxed. I use scp:

scp  feitian_dump.sql  [email protected]:/usr/local/src/

The next step is to do the configuration from the library.

The best from the library is completely "clean" state, or reinstall. Like install Mysql, Nginx, php these conventional operations, the best script written in peacetime, ready to install the package, their common profile. When you want to use, directly run about script installed.

I used to use ansible playbook to do this operation scripted.

Reinstall the operating Mysql I do not write, or write another article I will share out their own scripts.

After Mysql reloading is completed, the database initialization to do, to change the root password, these are routine operations.

When a new Mysql installed and successfully started, you can configure the functions from the library.

Modify from the library my.cnf, because I want to back up feitian business library, so only adds:

server-id=229
replicate_wild_do_table=feitian.%
  • In fact, if a full backup is performed, so long as the configuration server-id and the main library is not the same on it, the other does not need to be increased.
  • However, if you want to back up some specific service database, you need to use replicate_wild_do_table this value. Its role is to tell from the library only need to back up a specific library, if there are multiple libraries, separated by commas to continue to add to.
  • There is also a function parameter replicate_wild_ignore_table the contrary, is to ignore the configuration database, the database that is not backed up, this parameter to use more restrictions are not familiar with is best not to use.

Next it is into a database before importing the database, you need to reset it from the library binlog:


mysql -uroot -p   -e 'reset master'
mysql -uroot -p   feitian < feitian_dump.sql

Highlight below to configure the master-slave synchronization.

Mentioned above, when the backup of the database, using a key parameter --master-data = 2. The key parameters Why do? Because the backup file, you can see master_log_file and master_log_pos two parameters we need. With these two parameters, we did not need to stop taking the lock table as in the past, to see the.

Specifically how to find these two parameters? They probably within the first 30 lines of the file, it can be found with head command:


[root@ND-BACKUP mysql]# head -n 30 feitian_dump.sql 
-- MySQL dump 10.13  Distrib 5.6.39, for linux-glibc2.12 (x86_64)
--
-- Host: localhost    Database: feitian
-- ------------------------------------------------------
-- Server version   5.6.39-log

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Position to start replication or point-in-time recovery from
--

-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000063', MASTER_LOG_POS=144333309;

--
-- Table structure for table `ft_access`
--

In the above output, you can see a binlog mysql-bin.000063, position is 144333309. With these two parameters, you can configure the synchronization from the library.

Generally from the library change master to it.

So, do the following configurations from the library:

# 先重置一下 slave 
mysql>reset slave all;
 #然后再执行操作 mysql>change master to master_host='172.188.26.221', master_user='repel', master_password='*****', master_port=3306,master_log_file='mysql-bin.000063', master_log_pos=144333309; 

Then, you can start a copy from the library:

mysql>start slave;

From the library to check the status:

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.188.26.221
                  Master_User: repel
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000063
          Read_Master_Log_Pos: 146576160
               Relay_Log_File: nd-feitian-relay-bin.000002
                Relay_Log_Pos: 2243134
        Relay_Master_Log_File: mysql-bin.000063
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

      Replicate_Wild_Do_Table: feitian.%
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 146576160
              Relay_Log_Space: 2243311
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No

        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: 221
                  Master_UUID: a986c930-214a-11e8-b69c-00163e0cee42
             Master_Info_File: /usr/local/mysql/mysqldata/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           Master_Retry_Count: 86400

                Auto_Position: 0
  • See Slave_IO_Running: Yes and Slave_SQL_Running: Yes, yes two master-slave synchronization basically no problem.
  • To confirm whether the master-slave synchronization to the same content, by:

    • Master_Log_File: mysql-bin.000063 and Relay_Master_Log_File: mysql-bin.000063 master-slave synchronization to have the same binlog log.
  • If the above two parameters are the same, but also need to compare Read_Master_Log_Pos and Exec_Master_Log_Pos two parameters. See there is no difference, no difference if the master-slave synchronization is better.

In the main library you can also look at the state of the master-slave synchronization:

msyql> show processlist;

Guess you like

Origin www.cnblogs.com/kcxg/p/11130697.html