Home Database Mysql Tutorial MySQL AB复制详述

MySQL AB复制详述

Jun 07, 2016 pm 03:10 PM
mysql copy database

实验所需主机:master:vm1 172.25.254.1slave1:vm2 172.25.254.2slave2:vm3 172.25.254.3在三台主机上分别安装mysql-serveryu

实验所需主机:
master:vm1 172.25.254.1
slave1:vm2 172.25.254.2
slave2:vm3 172.25.254.3
在三台主机上分别安装mysql-server
yum install -y mysql-server

1.master端的配置
[root@vm1 ~]# vim /etc/my.cnf
[mysqld]下面加入
log-bin=mysql-bin              #启动二进制日志系统
server-id=1            #必须为 1 到 232–1 之间的一个正整数值
binlog-do-db=westos            #二进制需要同步的数据库名,如果需要同步多个库,例如要再同步                        test库,再添加一行“binlog-do-db=test”,以此类推
binlog-ignore-db=mysql          #禁止同步 mysql 数据库


启动master端数据库
[root@vm1 ~]# /etc/init.d/mysqld start
给数据库授权,让172.25.254.网段的主机以用户westos,密码westos来访问数据库

mysql> grant replication slave, reload,super on *.* to westos@'172.25.254.%' identified by 'westos';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> show master status;
+------------------+----------+--------------+------------------+
| File            | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 |      348 | westos      | mysql            |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
记录 File 和 Position 的值,下面会用到


2.slave1(vm2)配置
[root@vm2 ~]# vim /etc/my.cnf
[mysqld]加入

server-id=2

启动slave1数据库
[root@vm2 ~]# /etc/init.d/mysqld start

在数据库中做如下操作
mysql> change master to master_host='172.25.254.1', master_user='westos', master_password='westos', master_log_file='mysql-bin.000003', master_log_pos=348;
Query OK, 0 rows affected (0.57 sec)
###master_log_file 为master端数据库的二进制文件(可以在master端 cd /var/lib/mysql mysqlbinlog mysql-bin.000003 进行查看) master_log_pos 为数据库要同步的点


mysql> slave start;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status\G;
...

            Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

如果都是 yes,表示从库的 I/O,Slave_SQL 线程都正确开启.表明数据库开始同步

注意:在同步之前确保 master 与 slave 上的数据一致性。


3.测试
在master端进行操作,查看slave1(vm2)端数据库是否同步

master(vm1)端

mysql> create database westos;
Query OK, 1 row affected (0.00 sec)

mysql> use westos
Database changed
mysql> create table users (
    -> username varchar (15) not null,
    -> password varchar (25) not null
    -> );
Query OK, 0 rows affected (0.24 sec)

mysql> insert into users values ('user1','123');
Query OK, 1 row affected (0.00 sec)

mysql> insert into users values ('user2','456');
Query OK, 1 row affected (0.00 sec)

mysql> select * from users;
+----------+----------+
| username | password |
+----------+----------+
| user1    | 123      |
| user2    | 456      |
+----------+----------+
2 rows in set (0.00 sec)

slave1(vm2)端

mysql> show databases;
+--------------------+
| Database          |
+--------------------+
| information_schema |
| mysql              |
| test              |
| westos            |
+--------------------+
4 rows in set (0.00 sec)

mysql> use westos
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from users;
+----------+----------+
| username | password |
+----------+----------+
| user1    | 123      |
| user2    | 456      |
+----------+----------+
2 rows in set (0.00 sec)

如果写操作较少,而读操作很多时,可以采取这种结构,一个做master,,其他的做slave。可以将读操作分布到其它的 slave,从而减小master 的压力。但是,当 slave 增加到一定数量时,slave 对 master 的负载以及网络带宽都会成为一个严重的问题。这种结构虽然简单,但是,它却非常灵活,足够满足大多数应用需求。

4.当设置 log_slave_updates 时,你可以让 slave 扮演其它 slave 的 master。此时,slave 把 SQL 线程执行的事件写进行自己的二进制日志(binary log),然后,其它的 slave 可以获取这些事件并执行它,从而有效缓解master 的压力。其配置方法如下:

添加slave2(vm3)
1)由于 master 上已经有数据,而新加的 slave2 没有,必须在配置复制前同步数据。
同步数据有两种方法:
第一种:
在 master 上执行以下命令:
[root@vm1 ~]# mysqldump westos > westos.bak
[root@vm1 ~]# scp westos.bak 172.25.254.3:

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

How to save JSON data to database in Golang? How to save JSON data to database in Golang? Jun 06, 2024 am 11:24 AM

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

How to use database callback functions in Golang? How to use database callback functions in Golang? Jun 03, 2024 pm 02:20 PM

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.

PHP connections to different databases: MySQL, PostgreSQL, Oracle and more PHP connections to different databases: MySQL, PostgreSQL, Oracle and more Jun 01, 2024 pm 03:02 PM

PHP database connection guide: MySQL: Install the MySQLi extension and create a connection (servername, username, password, dbname). PostgreSQL: Install the PgSQL extension and create a connection (host, dbname, user, password). Oracle: Install the OracleOCI8 extension and create a connection (servername, username, password). Practical case: Obtain MySQL data, PostgreSQL query, OracleOCI8 update record.

How to handle database connections and operations using C++? How to handle database connections and operations using C++? Jun 01, 2024 pm 07:24 PM

Use the DataAccessObjects (DAO) library in C++ to connect and operate the database, including establishing database connections, executing SQL queries, inserting new records and updating existing records. The specific steps are: 1. Include necessary library statements; 2. Open the database file; 3. Create a Recordset object to execute SQL queries or manipulate data; 4. Traverse the results or update records according to specific needs.

How to connect to remote database using Golang? How to connect to remote database using Golang? Jun 01, 2024 pm 08:31 PM

Through the Go standard library database/sql package, you can connect to remote databases such as MySQL, PostgreSQL or SQLite: create a connection string containing database connection information. Use the sql.Open() function to open a database connection. Perform database operations such as SQL queries and insert operations. Use defer to close the database connection to release resources.

See all articles