MySQL master-slave Replication

Source: Internet
Author: User
MySQL Of Install The process is relatively simple.

1. On the maste host,ConfigurationMy. CNF
Add the following fields in the [mysqld] configuration section:

Log-bin = mysql-bin.log
BINLOG-do-DB = blog # Name of the database to be synchronized
BINLOG-do-DB = index # Name of the database to be synchronized

Server-id = 1

Restart MySQL

# Service mysqld restart
Entering MySQL

# Mysql-u root-ppassword
Add a synchronization account

Mysql> grant replication slave on *. * To 'repication' @ '%' identified by 'Password ';
Mysql> use database;
Mysql> flush tables with read lock;
Mysql> show Master Status:

---------------------------------------------------------------------
| File | position | binlog_do_db | binlog_ignore_db |
+ ------------------ + ---------- + ------------------ +
| Mysql-bin.000002 | 1448 | blog, index |
+ ------------------ + ---------- + ------------------ +

Do not close this terminal at this time!
Remember the information mysql-bin.000002 and 1448 for the first two fields in the table above.
Open another terminal:

Get the "database snapshot" on the primary database"

# Tar-czvf database.tar.gz/data/Database

Switch to the previous Terminal
Mysql> unlock tables; # unlock to update the database on the master database 192.168.1.10

2. Configure the slave Database

First create a database

# Mysql-uroot-ppassword
Mysql> Create Database databasename # create a database with the masterServerDatabase with the same name
Mysql> exit;

3. Add the following fields in the [mysqld] configuration section of the two slave databases:

#192.168.1.11
Server-id = 2
Master-host = 192.168.1.10
Master-user = Replication
Master-Password = Password
Master-connect-retry = 60
Replicate-do-DB = blog
Replicate-do-DB = index

#192.168.1.12
Server-id = 3
Master-host = 192.168.1.10
Master-user = Replication
Master-Password = Password
Master-connect-retry = 60
Replicate-do-DB = blog
Replicate-do-DB = index

3. Restart two databases respectively.

# Service mysqld restart

4. Access two slave databases respectively.

On salve2

# Mysql-uroot-ppassword
Mysql> slave stop;
> Change master
> Master_host = '1970. 168.1.10 ',
> Master_user = 'replicase ',
> Master_password = 'Password ',
> Master_log_file = 'mysql-bin.000002 ',
> Master_log_pos = 1448;

Mysql> Start slave;

On salve2

# Mysql-uroot-ppassword
Mysql> slave stop;
> Change master
> Master_host = '1970. 168.1.10 ',
> Master_user = 'replicase ',
> Master_password = 'Password ',
> Master_log_file = 'mysql-bin.000002 ',
> Master_log_pos = 1448;

Mysql> Start slave;

On the slave side:

Mysql> show processlist;
+ ---- + ------------- + ----------- + ------ + --------- + ------------ + ------------------------------------------------------------------- + -------------------- +
| ID | user | host | dB | command | time | state | info |
+ ---- + ------------- + ----------- + ------ + --------- + ------------ + ------------------------------------------------------------------- + -------------------- +
| 4 | system user | null | connect | 48 | waiting for Master to send event | null |
| 5 | system user | null | connect | 4294923022 | has read all relay log; waiting for the slave I/O thread to update it | null |
| 6 | root | localhost | null | query | 0 | null | show processlist |
+ ---- + ------------- + ----------- + ------ + --------- + ------------ + ----------------------------------------------------------
The above information indicates that the synchronization is successful!

4. synchronization test process
Create a table on the primary database and insert a data entry.
Check whether updates are made to the two databases.
Process omitted.

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.