Let's talk about how to build a MySQL Replication environment.

Source: Internet
Author: User
Tags database load balancing

This article mainly introducesBuild a MySQL Replication EnvironmentTo achieveDatabase Load Balancing. The specific process of setting up the MySQL Replication environment is as follows:

Environment: Two ubuntu instances, one Master instance and one Slave instance.

Master: ubuntub (192.168.1.101)

Slave: ubuntuc (192.168.1.104)

The configuration on the Master is as follows:

 
 
  1. Ubuntub @ ubuntub:/etc/mysql $ sudo vi my. cnf
  2.  
  3. # Bind-address = 127.0.0.1 // comment out
  4.  
  5. Server-id = 1 // set master to 1 and slave to 2
  6.  
  7. Log_bin =/var/log/mysql/mysql-bin.log
  8.  
  9. Ubuntub @ ubuntub:/etc/mysql $ sudo/etc/init. d/mysql restart
  10.  
  11. Ubuntub @ ubuntub:/etc/mysql $ mysql-uroot-p
  12.  
  13. Mysql> grant replication slave on *. * TO 'rep _ user' @ '% 'identified BY 'rep _ password ';
  14.  
  15. Mysql> flush tables with read lock;
  16.  
  17. Query OK, 0 rows affected (0.00 sec)
  18.  
  19. Mysql> show master status;
  20.  
  21. + ------------------ + ---------- + -------------- + ------------------ +
  22.  
  23. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
  24.  
  25. + ------------------ + ---------- + -------------- + ------------------ +
  26.  
  27. | Mysql-bin.000001 | 261 |
  28.  
  29. |
  30.  
  31. + ------------------ + ---------- + -------------- + ------------------ +
  32.  
  33. 1 row in set (0.02 sec)
  34.  
  35. Mysql> use rep;
  36.  
  37. Mysql> select * from user;
  38.  
  39. + ------- + ---- +
  40.  
  41. | Name | id |
  42.  
  43. + ------- + ---- +
  44.  
  45. | B | 1 |
  46.  
  47. | User3 | 2 |
  48.  
  49. + ------- + ---- +
  50.  
  51. 2 rows in set (0.00 sec)
  52.  
  53. Ubuntub @ ubuntub:/data/mysql_bak $ mysqldump-uroot-p rep> dbdump. SQL
  54.  
  55. Ubuntub @ ubuntub:/data/mysql_bak $ mysql-u root-p
  56.  
  57. Mysql> unlock tables;
  58.  
  59. Ubuntub @ ubuntub:/data/mysql_bak $ scp/data/mysql_bak/dbdump. SQL ubuntuc@192.168.1.104:/data // reverse the backup data to the slave

Configurations on Slave:

 
 
  1. Ubuntuc @ ubuntuc:/data $ sudo vi/etc/mysql/my. cnf
  2.  
  3. # Bind-address = 127.0.0.1
  4.  
  5. Server-id = 2
  6.  
  7. Log_bin =/var/log/mysql/mysql-bin.log
  8.  
  9. Ubuntuc @ ubuntuc:/etc/mysql $ sudo/etc/init. d/mysql restart
  10.  
  11. Ubuntuc @ ubuntuc:/data $ mysql-u root-p
  12.  
  13. Mysql> use rep;
  14.  
  15. Mysql> source/data/dbdump. SQL // import data
  16.  
  17. Mysql> change master to // Change Master
  18.  
  19. -> MASTER_HOST = '192. 168.1.101 ',
  20.  
  21. -> MASTER_USER = 'rep _ user ',
  22.  
  23. -> MASTER_PASSWORD = 'rep _ password ',
  24.  
  25. -> MASTER_LOG_FILE = 'mysql-bin.000001 ',
  26.  
  27. -> MASTER_LOG_POS = 261;
  28.  
  29. Mysql> start slave;
  30.  
  31. Mysql> show master status;
  32.  
  33. + ------------------ + ---------- + -------------- + ------------------ +
  34.  
  35. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
  36.  
  37. + ------------------ + ---------- + -------------- + ------------------ +
  38.  
  39. | Mysql-bin.000002 | 746 |
  40.  
  41. |
  42.  
  43. + ------------------ + ---------- + -------------- + ------------------ +
  44.  
  45. Mysql> show slave status; // check whether there are any errors. For example, if server-id conflicts, some errors may occur.

Test:

1) Create a database on the Master and view it on the Slave. You can see the show databases;

2) operate the user table of the rep database on the Master, and immediately copy the result to the slave;

This section describes how to build a MySQL Replication environment.

Related Article

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.