MySQL Master-slave configuration

Source: Internet
Author: User



Master-Slave Replication configuration

The steps are as follows:

Primary server: From the server IP address, respectively

[Python]View Plain copy
    1. 192.168.145.222,192.168. 145.226

1. Modify Master server Master:

[Python]View Plain copy
    1. Vi/etc/my.cnf
    2. [Mysqld]
    3. Log-bin=mysql-bin #[must] enable binary logging
    4. server-id=222 #[must] server unique ID, default is 1, usually take IP last paragraph

2. Modify the slave from the server:

[Python]View Plain copy
    1. Vi/etc/my.cnf
    2. [Mysqld]
    3. Log-bin=mysql-bin #[not required] enable binary logging
    4. server-id=226 #[must] server unique ID, default is 1, usually take IP last paragraph

3. Restart MySQL for two servers

[Python]View Plain copy
    1. Systemctl Restart MARIADB

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

[Python]View Plain copy
    1. Mysql
    2. Mysql>grant REPLICATION SLAVE On * * to ' mysync ' @'% ' identified by ' q123456 ';//normally without root account, "%" Indicates that all clients may connect, as long as the account, the password is correct, here can be replaced by specific client IP, such as 192.168.   145.226, enhance security.

5, log on to the master server MySQL, query the status of master

[Python]View Plain copy
  1. Mysql>show Master status;
  2. +------------------+----------+--------------+------------------+
  3. | File | Position | binlog_do_db | binlog_ignore_db |
  4. +------------------+----------+--------------+------------------+
  5. | Mysql-bin.      000004 |              308 |                  |  |
  6. +------------------+----------+--------------+------------------+
  7. 1 row in Set (0.00 sec)


Note: Do not operate the master server MySQL again after performing this step to prevent the change of the primary server state value


6. Configure the slave from the server:

Note that mysql-bin.000004 and 308 are file and position in the fifth step

[Python]View Plain copy
    1. 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;//Note mysql-bin. 000004 and 308 are the fifth steps in the file and
    2. Mysql>start slave; To start the Copy from Server feature


7. Check the status of the replication function from the server:

[Python]View Plain copy
  1. Mysql> Show Slave Status\g
  2. 1. Row ***************************
  3. Slave_io_state:waiting for Master to send event
  4. Master_host: 192.168. 2.222//Primary server address
  5. Master_user:mysync//Authorization account name, try to avoid using root
  6. Master_port: 3306//Database port, partial version not on this line
  7. Connect_retry:
  8. Master_log_file:mysql-bin. 000004
  9. Read_master_log_pos: + //#同步读取二进制日志的位置, greater than or equal to Exec_master_log_pos
  10. Relay_log_file:ddte-relay-bin. 000003
  11. Relay_log_pos: 251
  12. Relay_master_log_file:mysql-bin. 000004
  13. Slave_io_running:yes//This status must be Yes
  14. Slave_sql_running:yes//This status must be Yes
  15. ......

Note: The slave_io and slave_sql processes must function normally, that is, the Yes state, otherwise it is an error state (e.g., one of the No is an error).

The above operation process, the master and slave server configuration is complete.

9, the master-slave server test:

master server MySQL, build the database, and create a table in this library to insert a piece of data:

[Python]View Plain copy
  1. mysql> CREATE DATABASE hi_db;
  2. Query OK, 1 row affected (0.00 sec)
  3. mysql> use hi_db;
  4. Database changed
  5. Mysql> CREATE TABLE HI_TB (id int (3), name char (10));
  6. Query OK, 0 rows affected (0.00 sec)
  7. mysql> INSERT into HI_TB values (001,' Bobu ');
  8. Query OK, 1 row affected (0.00 sec)
  9. mysql> show databases;
  10. +--------------------+
  11. | Database |
  12. +--------------------+
  13. | Information_schema |
  14. | hi_db |
  15. | MySQL |
  16. | Test |
  17. +--------------------+
  18. 4 rows in Set (0.00 sec)
  19. MySQL Query from server:
  20. mysql> show databases;
  21. +--------------------+
  22. | Database |
  23. +--------------------+
  24. | Information_schema |
  25. |       hi_db | I ' M here, you see?
  26. | MySQL |
  27. | Test |
  28. +--------------------+
  29. 4 rows in Set (0.00 sec)
  30. Mysql> Use hi_db
  31. Database changed
  32. Mysql> SELECT * from HI_TB; View new specific data on the primary server
  33. +------+------+
  34. | ID | name |
  35. +------+------+
  36. |  1 | bobu |
  37. +------+------+
  38. 1 row in Set (0.00 sec)

MySQL Master-slave configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.