Hệ quản trị cơ sở dữ liệu

Replication
Dư Phương Hạnh
Bộ môn Hệ thống thông tin
Khoa CNTT, trường Đại học Công nghệ
Đại họ...
Outline





Replication concepts and principles
Setting up replication on master and slave servers
Managing replicati...
Replication concepts and
principles

3

Hệ quản trị CSDL @ BM HTTT
Introduce
 Beside performing regular backups of your
databases, you can also replicate your databases.
 It means you mai...
Replicating MySQL Databases
 Updates made to one database copy are
automatically propagated to all the other replicas.
 ...
Benefits of database replication
 Availability: Any replica can be used as a "hot"
backup.
– If the master database becom...
Benefits of database replication
 Load Balancing: Replicas can serve split loads, also
called load balancing, for heavily...
Availability
One database is
the master while
the other is
designated as a
slave. Only
updates to the
master are
logged.

...
Load Balancing
Read queries are
directed to either
the master or any
of the replicas
while update
queries are
directed onl...
MySQL Replication Model
MySQL replication is based on a number of principles:
1.Replication is a one-way, asynchronous pro...
MySQL Replication Model
4. The master server must be configured with a user account
that grants replication privileges to ...
MySQL Replication Model
7. The replicas connect to the master to read the
binary log and then apply the updates to catch u...
MySQL Replication Model
9. Each server that participates in the replication process must
be assigned a unique numerical se...
MySQL Replication Model
12. No slave server can ever have two master servers.
13. It is generally best to have the master ...
Setting up Replication
 Enable binary logging on the master server.
 Make a backup of the master database.
 Start a new...
Setting up Replication (2)
 Assign a unique numerical server ID to each server
that participates in the replication proce...
Replication Files on the Slave (1)
When replication is implemented, the slave server
maintains a set of files to support t...
Replication Files on the Slave (2)
2. master.info: Contains connection information such
as the master server hostname, use...
Implementing Replication Details

19

Hệ quản trị CSDL @ BM HTTT
Set up Replication User (on Master)
 To allow a master server to replicate data to a slave
server, you must set up a user...
Set up Replication User (on Master)
GRANT REPLICATION SLAVE ON *.* TO '<slave
account>'@'<slave host>' IDENTIFIED BY '<pas...
Making Initial Backup
 Make a backup of the databases that you want to replicate.
Use the --master-data option in the mys...
Configuration Changes on Master
(1)
 Shut down the master server.

 Modify the [mysqld] group in the option file on the ...
Configuration Changes on Master
(2)
 Two binlog-ignore-db options specify
[mysqld]
log-bin

that changes to the mysql and...
Configuration Changes on the Slave
 Shut down the slave server.
 Modify the option file on the slave server so that
the ...
Restore Backup on Slave
 Use the backup file created on Master to load the
databases into the slave server

26

Hệ quản t...
Set up Connection to Master
 Specify the settings that
will be used for the slave

 Syntax
CHANGE MASTER TO

server to c...
Start Replication on Slave
 The final is to start the replication process on the
slave server. To do so, execute the foll...
Verifying Replication
 Once replication is set up, update a table on the master
server and then confirm whether that chan...
Verifying Replication
 Managing the Master Server:
– RESET MASTER Statement
– SHOW MASTER STATUS Statement
– SHOW SLAVE H...
Verifying Replication
 Managing the Slave Server:
– SHOW SLAVE HOSTS Statement
– CHANGE MASTER Statement
– RESET SLAVE St...
Exercise
 Thực hiện đồng bộ hoá CSDL test của bài trước.
– Cấu hình slave server giả lập trên 1 máy tính
– Bật chế độ --l...
Upcoming SlideShare
Loading in …5
×

8.replication

651 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
651
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
13
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • CHANGE MASTER TOMASTER_HOST=‘’,MASTER_USER=&apos;&lt;user account&gt;&apos;,MASTER_PASSWORD=&apos;&lt;password&gt;&apos;,MASTER_LOG_FILE=&apos;&lt;log file&gt;&apos;,MASTER_LOG_POS=&lt;position&gt;;
  • 8.replication

    1. 1. Hệ quản trị cơ sở dữ liệu Replication Dư Phương Hạnh Bộ môn Hệ thống thông tin Khoa CNTT, trường Đại học Công nghệ Đại học Quốc gia Hanoi hanhdp@vnu.edu.vn
    2. 2. Outline     Replication concepts and principles Setting up replication on master and slave servers Managing replication Replication settings Read more at: http://mysql-tools.com/en/replication-in-mysql.html 2 Hệ quản trị CSDL @ BM HTTT
    3. 3. Replication concepts and principles 3 Hệ quản trị CSDL @ BM HTTT
    4. 4. Introduce  Beside performing regular backups of your databases, you can also replicate your databases.  It means you maintain a copy of the database that is kept up-to-date (synchronized) with the original database. If the original database becomes unavailable, the replicated database can continue to provide users immediate access to the same data with a minimal amount of downtime. 4 Hệ quản trị CSDL @ BM HTTT
    5. 5. Replicating MySQL Databases  Updates made to one database copy are automatically propagated to all the other replicas.  Generally, one of the replicas is designated as the master where Updates are directed to the master while read queries can be addressed to either the master or the slaves.  If replicas other than master handle the updates, then keeping the replicas identical becomes more complex. 5 Hệ quản trị CSDL @ BM HTTT
    6. 6. Benefits of database replication  Availability: Any replica can be used as a "hot" backup. – If the master database becomes unavailable, this replica can take over and can be designated as the master. The failures can be fixed and the failed replica can rejoin as a slave replica.  Backups: Replicas can be used as active backups and can be used to perform tedious offline backups to file systems without locking up the primary instance. 6 Hệ quản trị CSDL @ BM HTTT
    7. 7. Benefits of database replication  Load Balancing: Replicas can serve split loads, also called load balancing, for heavily used databases. – Read queries can be distributed to the different replicas while updates are handled by the master. This scenario works well when the number of read queries is far greater than the number of update queries.  Proximity: Some replicas can be closer to users leading to improved response time.  Security: It is harder for malicious users to damage 7 all the replicas. Hệ quản trị CSDL @ BM HTTT
    8. 8. Availability One database is the master while the other is designated as a slave. Only updates to the master are logged. 8 Hệ quản trị CSDL @ BM HTTT
    9. 9. Load Balancing Read queries are directed to either the master or any of the replicas while update queries are directed only to the master. 9 Hệ quản trị CSDL @ BM HTTT
    10. 10. MySQL Replication Model MySQL replication is based on a number of principles: 1.Replication is a one-way, asynchronous process  changes are always propagated from the master server to the slave server, but never the other way around. 2.The primary MySQL server acts as the Master server, and the servers that contain the copied databases are considered the Slave servers. 3.Data always moves from the master server to the slave server. As a result, only databases on the master server should be updated. The updates are then propagated to the slave servers 10 Hệ quản trị CSDL @ BM HTTT
    11. 11. MySQL Replication Model 4. The master server must be configured with a user account that grants replication privileges to the slave server. The account allows the slave server to access the master server in order to receive updates. 5. Replication is based on the master database server recording all updates in a binary log  binary logging must be enabled on the master server. The logged updates are used to synchronize the database on the slave server. 6. To avoid conflicts, update queries are directed to the master while read queries can either go to the master or to the slaves. 11 Hệ quản trị CSDL @ BM HTTT
    12. 12. MySQL Replication Model 7. The replicas connect to the master to read the binary log and then apply the updates to catch up with the master. 8. The slave server uses replication coordinates to track updates. – The coordinates are based on the name of a binary log file on the master server and the position in that file. The file and position represent where MySQL left off when the last update was performed on the slave server. The coordinates - along with other logon information - are 12 stored in the master.info file on the slave host.quản trị CSDL @ BM HTTT Hệ
    13. 13. MySQL Replication Model 9. Each server that participates in the replication process must be assigned a unique numerical server ID. You assign the ID by specifying the server-id option in the [mysqld] section of the option file for each server. 10. A master server can replicate data to one or more slave servers. 11. To set up replication, the master server and slave server must begin with databases in a synchronized state. In other words, the databases to be replicated must be identical when replication is initiated. 13 Hệ quản trị CSDL @ BM HTTT
    14. 14. MySQL Replication Model 12. No slave server can ever have two master servers. 13. It is generally best to have the master server and slave servers run the same version of MySQL. 14. There are two core types of replication format – Statement Based Replication (SBR): replicates entire SQL statements, – Row Based Replication (RBR): replicates the changed rows – You may also use a third variety, Mixed Based Replication (MBR), which is the default mode within MySQL 5.1.14 and 14 later. Hệ quản trị CSDL @ BM HTTT
    15. 15. Setting up Replication  Enable binary logging on the master server.  Make a backup of the master database.  Start a new binary log immediately after making the backup.  Set up a user account on the master server that grants replication privileges to the slave server. The account allows the slave server to access the master server in order to receive updates. 15 Hệ quản trị CSDL @ BM HTTT
    16. 16. Setting up Replication (2)  Assign a unique numerical server ID to each server that participates in the replication process.  Block all updates to the master.  Create a Slave instance.  Load the backup of the master database into the slave  Apply the updates from the binary log to the slave to sync up with the master.  Get both the master and slave running. 16 Hệ quản trị CSDL @ BM HTTT
    17. 17. Replication Files on the Slave (1) When replication is implemented, the slave server maintains a set of files to support the replication. MySQL automatically creates the three types of files on the slave server: 1.<host>-relay-bin.<extension>:Contains the statements to be used to synchronize the replicated database with the database on the master server, and then it is deleted. – 17 The relay log files receive their data from the binary log files on the master server. Hệ quản trị CSDL @ BM HTTT
    18. 18. Replication Files on the Slave (2) 2. master.info: Contains connection information such as the master server hostname, user account and its password. It also maintains information about the last binary log file on the master server to be accessed and the position in that file. 3. relay-log.info: Contains information about the relay log files and tracks the last position in those files in which the replicated database was updated. 18 Hệ quản trị CSDL @ BM HTTT
    19. 19. Implementing Replication Details 19 Hệ quản trị CSDL @ BM HTTT
    20. 20. Set up Replication User (on Master)  To allow a master server to replicate data to a slave server, you must set up a user account on the master server.  The slave server then uses that account to establish a connection to the master server 20 Hệ quản trị CSDL @ BM HTTT
    21. 21. Set up Replication User (on Master) GRANT REPLICATION SLAVE ON *.* TO '<slave account>'@'<slave host>' IDENTIFIED BY '<password>'; The REPLICATION SLAVE privilege at the global level allows all changes to a database to be replicated to the copy of the database on the slave server. TO clause defines the username on the account and host from which that account can connect. This is the host where the slave server resides. The IDENTIFIED BY clause then identifies the password that should be used when the slave server logs on to the master server. 21 Hệ quản trị CSDL @ BM HTTT
    22. 22. Making Initial Backup  Make a backup of the databases that you want to replicate. Use the --master-data option in the mysqldump command. The --master-data option adds a CHANGE MASTER statement similar to the following to your backup file: CHANGE MASTER TO MASTER_LOG_FILE='mastsrvbin.000201', MASTER_LOG_POS=64;  CHANGE MASTER statement identifies the binary log file and the position in that file at the time that the backup file is created. You use this information later when you set up replication on the slave server to synchronize the slave server with the master server. 22 Hệ quản trị CSDL @ BM HTTT
    23. 23. Configuration Changes on Master (1)  Shut down the master server.  Modify the [mysqld] group in the option file on the master server to specify a server ID for the master server. – The master server and any slave servers must each be assigned a unique numerical ID.  If you don't want to replicate a specific database, such as the mysql or test databases, you can add a binlog-ignoredb option for each database to prevent changes to that database from being logged to the binary file.  Restart the master server. 23 Hệ quản trị CSDL @ BM HTTT
    24. 24. Configuration Changes on Master (2)  Two binlog-ignore-db options specify [mysqld] log-bin that changes to the mysql and test binlog-db=sakila databases should not be logged to binlog-ignore-db=mysql the binary files. binlog-ignore-db=test server-id=masterserverID; log-bin option specifies  The server-id option specifies the numbered ID for the master server.  Note: If you use an existing option that binary logging should file, a server-id may already be be enabled. present. If multiple options are specified and the numerical IDs are different, replication might not work. 24 Hệ quản trị CSDL @ BM HTTT
    25. 25. Configuration Changes on the Slave  Shut down the slave server.  Modify the option file on the slave server so that the [mysqld] section includes the following settings: server-id=<slave server id>  Make certain that this server ID is different from the master server ID and different from any other slave server IDs. Also be sure that this is the only serverid option defined on the slave server.  Restart the slave server. 25 Hệ quản trị CSDL @ BM HTTT
    26. 26. Restore Backup on Slave  Use the backup file created on Master to load the databases into the slave server 26 Hệ quản trị CSDL @ BM HTTT
    27. 27. Set up Connection to Master  Specify the settings that will be used for the slave  Syntax CHANGE MASTER TO server to connect to the MASTER_HOST='<master host>', master server and MASTER_USER='<user account>', determine which binary log MASTER_PASSWORD='<passwor file to access. Launch the mysql client utility on the slave server, and execute CHANGE MASTER statement. d>', MASTER_LOG_FILE='<log file>', MASTER_LOG_POS=<position>;  The slave server adds this information to the master.info file, which is used when connecting to the master server. 27 Hệ quản trị CSDL @ BM HTTT
    28. 28. Start Replication on Slave  The final is to start the replication process on the slave server. To do so, execute the following SQL statement on the slave server: START SLAVE;  The statement initiates the threads that connect from the slave server to the master server. 28 Hệ quản trị CSDL @ BM HTTT
    29. 29. Verifying Replication  Once replication is set up, update a table on the master server and then confirm whether that change has been replicated to the slave server.  To support administering replication, MySQL provides a number of SQL statements that allow you to view information about the replication environment or take a specific action. MySQL supports statements for both the master server and the slave server. 29 Hệ quản trị CSDL @ BM HTTT
    30. 30. Verifying Replication  Managing the Master Server: – RESET MASTER Statement – SHOW MASTER STATUS Statement – SHOW SLAVE HOSTS Statement 30 Hệ quản trị CSDL @ BM HTTT
    31. 31. Verifying Replication  Managing the Slave Server: – SHOW SLAVE HOSTS Statement – CHANGE MASTER Statement – RESET SLAVE Statement – SHOW SLAVE STATUS Statement – START SLAVE Statement – STOP SLAVE Statement 31 Hệ quản trị CSDL @ BM HTTT
    32. 32. Exercise  Thực hiện đồng bộ hoá CSDL test của bài trước. – Cấu hình slave server giả lập trên 1 máy tính – Bật chế độ --log-bin trên master server – Backup CSDL test trên master server với option –masterdata – Tạo người dùng cho slave – Slave server thực hiện restore CSDL test – Khai báo thông tin để truy cập bin log thực hiện đồng bộ hoá – Bắt đầu tiến hành đồng bộ hoá 32 Hệ quản trị CSDL @ BM HTTT

    ×