Easy to get _mysql master-slave replication

How to install mysql database, do not say here, it's only master-slave replication, follow these steps:

1, as the master server from each of the following :
  1.1, the same version
  1.2, initialization table, and starts in the background mysql
  1.3, modified root password

2, the master server modify master:
   the command line: vi /etc/my.cnf

       amend as below--------------

       [mysqld]
       log-bin bin-MySQL = // [must] enable binary logging
       server-id = 222 // [must] unique server ID, the default is 1, and generally the last paragraph of IP

3. Modify from the server slave:
   the command line: vi /etc/my.cnf

      Amended as follows --------------
       [mysqld]
       log-bin = MySQL-bin // [not required] enable binary logging
       server-id = 226 // [must] server unique ID, default 1, and generally the last paragraph of IP

4, restart the two servers MySQL
   /etc/init.d/mysql restart

5, create an account on the master server and authorize the slave:
   Command line: / usr / local / mysql / bin / mysql -uroot -p // here to enter your password 
   mysql> GRANT REPLICATION SLAVE ON * * to 'root'. @ '%' identified by 'root '; // generally do not have the root account, '%'; means that all clients may even, as long as the account number, password is correct, here are available instead of the specific client IP, such as 192.168.11.119, strengthen Safety.

6, log on the primary server mysql, query the status of master
   MySQL> Show Status master;
   + ------------------ + ---------- + - + ------------------ + -------------
   | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
   + ------- ----------- + ------------ + ---------- + -------------- + ------
   | MySQL-bin.000004 | 308 | | |
   + ------------------ + ---------- + - + ------------------ + -------------
   1 Row in the SET (0.00 sec)
   Note: Do not perform this step after completion operating the main server MYSQL, to prevent the primary server state value change

7, the configuration server from the Slave:
   MySQL> = Change Master MASTER_HOST to '192.168.145.222', MASTER_USER = 'the root', master_password = 'the root',
        MASTER_LOG_FILE = 'MySQL-bin.000004', MASTER_LOG_POS = 308; // careful not OFF, no 308 numbers before and after the single quotation marks.

   mysql> start slave; // start replication from the server

8, check the status of replication from the server:

   MySQL> Show Slave Status \ G

   *************************** 1. Row **** ***********************

              Slave_IO_State: Waiting for master to the send Event
              MASTER_HOST: // 192.168.2.222 primary server address
              Master_User: mysync // authorized account name, try to avoid using the root
              MASTER_PORT: // database port 3306, this line is not part of the version
              Connect_Retry: 60
              Master_Log_File: MySQL-bin.000004
              Read_Master_Log_Pos: // # 600 synchronous read binary log position, greater than or equal 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 status must be YES
              Slave_SQL_Running: Yes // This status must be YES
                    ......

Note: Slave_IO and Slave_SQL process must run properly, that is YES state otherwise is wrong state (eg: NO belong to one error).

During the above operations, from the master server to complete the configuration.

9, the master from the server test:

master Mysql, to establish a database, and construction of the table in this library is inserted into a data:

  MySQL> Create Database hi_db;
  Query the 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 the OK, 0 rows affected (0.00 sec)
 
  MySQL> INSERT INTO hi_tb values (001, 'BOBU');
  Query the OK,. 1 Row affected (0.00 sec )

  MySQL> Show Databases;
   + -------------------- +
   | Database |
   + ------------------ - +
   | information_schema |
   | hi_db |
   | MySQL |
   | the Test |
   + -------------------- +
   4 rows in the SET (0.00 sec)

Mysql query from the server:

   MySQL> Show Databases;

   + -------------------- +
   | Database |
   + -------------- + ------
   | information_schema |
   | hi_db | // the I'M here Wallpaper, we see it
   | MySQL |
   | the Test |

   + -------------------- +
   4 rows in the SET (0.00 sec)

   MySQL> use hi_db
   Database changed
   MySQL> the SELECT * from hi_tb; // view on the new primary server specific data increase of
   + ------ + ------ +
   | the above mentioned id | name |
   + ------ + ------ +
   | 1 | BOBU |
   + ----- - + ------ +
   1 Row in the SET (0.00 sec)

10, complete:
    write a shell script to monitor two yes (Slave_IO and Slave_SQL process) slave with nagios, found only one or zero yes, it indicates a problem with the master and slave, texting alert bar.

  命令行脚本:  !/bin/bash|@|port=`netstat -anl|grep 3306 |sed -n '1p' |awk '{print $4}'|awk -F: '{ print $2}'`|@|array=($(mysql -uroot -p123 -e "show slave status\G"|grep "Running" |awk '{print $2}'))|@|if ["$port" == "3306"]|@|then|@|   if [ "${array[0]}" ==     "Yes" ] || [ "${array[1]}" == "Yes" ] |@|     then|@|       echo "slave is OK"|@|     else|@|       echo "slave is error"|@|   fi|@|fi|@|

Guess you like

Origin www.cnblogs.com/shundong106/p/10992531.html