mysql 主从同步配置

Master-slave data replication allows for replicated data to be copied to multiple computers for backup and analysis by multiple parties. Needed changes identified by a group member must to be submitted to the designated “master” of the node. This differs from Master-Master replication, in which data can be updated by any authorized contributor of the group.

This article provides steps for setting up MySQL master-slave database replication between two cloud servers. The operating system used for the examples in the article is CentOS 6, built from a Rackspace Cloud Servers base image.

Before you begin

The steps in this article use two cloud servers named db01 and db02. Cloud servers have two IP addresses (one public, one private). The examples demonstrate configuring replication over the private IP interface so that no bandwidth charges are incurred. For the duration of the article, db01 is considered the masterMySQL server (running in read-write mode), and db02 is considered the slave server (running in read-only mode).

If you already have a MySQL database running on the master node, a dump and restore into the slave node is required before configuring replication between them. You use the mysqldump command to dump a database into a file, then transfer it and restore it to the slave. After the necessary configuration has been performed, replication is in effect. For more information, see the Configure replication section.

Create the Cloud Servers

Create two Linux cloud servers, using the Centos 6 base image. Use the following steps to create each server separately.

  1. Log in to your Rackspace Cloud Control Panel.
  2. On the Cloud Servers page, click Create Server.
  3. Name the servers so that you can easily identify them during the setup.
  4. Select the Centos 6 base image.
  5. Select the RAM configuration (flavor) appropriate for your database requirements.
  6. Click Create Server.

The commands outlined in the following sections need to be executed by a privileged (root, sudo group) user. Any strings or values specified in brackets should be replaced with data specific to your setup.

Install MySQL

You must install the mysql-server package on both CentOS Cloud Servers.

  1. Before installing MySQL, confirm that the package database is up-to-date by running the following command:

    #yum update
    
    
  2. Install MySQL and enable it to run at boot automatically:

     #yum install mysql-server
    #chkconfig mysqld on
    
    
  3. Start the mysqld service:

    #service mysqld start
    
    
  4. After the mysqld service has been started, set your mysql server root password by using following commands:

    /usr/bin/mysqladmin -u root password 'new-password'
    /usr/bin/mysqladmin -u root -h web01 password 'new-password'
    
    

    Note: Alternatively, you can run the secure installation script packaged with the MySQL installation:

    # /usr/bin/mysql_secure_installation
    Enter current password for root (enter for none):
    ...
    Set root password? [Y/n] Y
    ...
    Remove anonymous users? [Y/n] Y
    ...
    Disallow root login remotely? [Y/n] Y
    ...
    Remove test database and access to it? [Y/n] Y
    ...
    Reload privilege tables now? [Y/n] Y
    
    
  5. To permit connections on port 3306 (the mysqld default port), add a TCP port 3306 rule with an insert at the last line number in the RH-Firewall-1-INPUT chain (in this case, line 10):

    # iptables -I RH-Firewall-1-INPUT 10 -p tcp --dport 3306 -j ACCEPT
    
    
  6. Save the firewall configuration:

    # service iptables save
    
    

Complete the following section to make relevant configuration changes to enable replication.

Configure replication

A MySQL user is required on the master server (db01) to be used for replication.

  1. Run the following commands to set up the MySQL user, updatng the entries in brackets with strings or values you that you want to use with your setup.

    # mysql -u root -p
    mysql> grant replication slave on *.* TO [replication_username]@'[private IP of db02]' identified by '[some password]';
    mysql> flush privileges;
    mysql> quit
    
    
  2. Edit the /etc/my.cnf file and add the following entries:

    bind-address = 0.0.0.0
    server-id = 1
    log-bin = mysql-bin
    binlog-ignore-db = "mysql"
    
    
  3. After you have finished updating the /etc/my.cnf file, restart the MySQL service.

    #service mysqld restart
    
    

    Before starting replication, the data on each server (master and slave) must be the same. To accomplish this duplication, dump the data from the master (db01) server and add it to the slave (db02) server, as instructed in the following.

  4. Use the following command to ensure that nothing can write to the master database during a database dump. Also note the filename and position of the binary log because you will need these values to complete the replication configuration on db02.

    # mysql -u root -p
    mysql> FLUSH TABLES WITH READ LOCK;
    mysql> SHOW MASTER STATUS;
    
    +------------------+--------------------------+------------------+
    | File             | Position  | Binlog_Do_DB | Binlog_Ignore_DB |
    +------------------+--------------------------+------------------+
    | mysql-bin.000010 |        10 |              | mysql            |
    +------------------+--------------------------+------------------+
    1 row in set (0.00 sec)
    
    
  5. Perform a database dump by using mysqldump as follows. list all the databases barring mysql and information_schema:

    # mysqldump -u root -p --databases [database-1] [database-2] ...  > /root/db_dump.sql
    
    
  6. Aftre the database dump has completed, lift the read lock from the master (db01):

    # mysql -u root -p
    mysql> UNLOCK TABLES;
    
    
  7. Copy the database dump file to the slave server so that it can be restored. You can use the scp command to accomplish this:

    scp /root/db_dump.sql [private-IP-of-db02]:/root/
    
    
  8. On db02, edit the /etc/my.cnf file and add the following entries:

    bind-address = 0.0.0.0
    server-id = 2
    master-host =  [private-IP-of-db01]
    master-user = [replication-username]
    master-password = [replication-password]
    master-connect-retry = 60
    
    
  9. Import the db_dump.sql file copied earlier and restart the MySQL service.

    # mysql -u root -p < /root/db_dump.sql
    # service mysqld restart
    
    
  10. Complete the slave replication steps:

```
# mysql -u root -p
mysql> SLAVE STOP;
mysql> CHANGE MASTER TO MASTER_HOST='[private-IP-of-db01]',
MASTER_USER='[replication-username]',
MASTER_PASSWORD='[replication-password]',
MASTER_LOG_FILE='[file-listed-on-master-status]',
MASTER_LOG_POS=[log-position-listed-on-master-status];
mysql> START SLAVE;
mysql> SHOW SLAVE STATUS\G

```

The **Slave_IO_State** field should show “Waiting for master to send event”. If it shows “Connecting to Master” please check your MySQL log file. By default it is **/var/log/mysqld.log** but it may be configured differently on your system. As always **/etc/my.cnf** will define the location of your log file.

Test replication

To test the replication setup, create a new database and associated table on db01, and insert data to confirm that the changes are mirrored on db02. In the following example, the new database is calledtesting and the new table is called users:

# mysql -u root -p
mysql> create database testing;
mysql> use testing
mysql> create table users(id int not null auto_increment, primary key(id), username varchar(30) not null);
mysql> insert into users (username) values ('foo');
mysql> insert into users (username) values ('bar');
mysql> exit
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,383评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,522评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,852评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,621评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,741评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,929评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,076评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,803评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,265评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,582评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,716评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,395评论 4 333
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,039评论 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,798评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,027评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,488评论 2 361
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,612评论 2 350

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,312评论 0 10
  • 1.A simple master-to-slave replication is currently being...
    Kevin关大大阅读 5,955评论 0 3
  • 我二爸招赘到隔壁县,在一次帮助他家拆房的过程中,我认识了一位聋哑女子,长的很是漂亮。 前一段时间,一次偶然的...
    控卫阅读 200评论 0 0
  • 凡是用reactjs开发的项目,但凡规模稍微大一些,都很可能要引入redux来管理组件状态的变迁和组件彼此之间的通...
    pengwang9阅读 670评论 0 1
  • 孟秋中,忽手红迹刺痒,以为蚊叮,不以为意。及一周,风团并起,肿胀异常,奇痒难忍,乃请假就医;输水三日,风团即消,恢...
    谦谦有理阅读 448评论 0 1