实验所需主机: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:

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

The steps to build a MySQL database include: 1. Create a database and table, 2. Insert data, and 3. Conduct queries. First, use the CREATEDATABASE and CREATETABLE statements to create the database and table, then use the INSERTINTO statement to insert the data, and finally use the SELECT statement to query the data.

MySQL is suitable for beginners because it is easy to use and powerful. 1.MySQL is a relational database, and uses SQL for CRUD operations. 2. It is simple to install and requires the root user password to be configured. 3. Use INSERT, UPDATE, DELETE, and SELECT to perform data operations. 4. ORDERBY, WHERE and JOIN can be used for complex queries. 5. Debugging requires checking the syntax and use EXPLAIN to analyze the query. 6. Optimization suggestions include using indexes, choosing the right data type and good programming habits.

MySQL is suitable for beginners because: 1) easy to install and configure, 2) rich learning resources, 3) intuitive SQL syntax, 4) powerful tool support. Nevertheless, beginners need to overcome challenges such as database design, query optimization, security management, and data backup.

Yes,SQLisaprogramminglanguagespecializedfordatamanagement.1)It'sdeclarative,focusingonwhattoachieveratherthanhow.2)SQLisessentialforquerying,inserting,updating,anddeletingdatainrelationaldatabases.3)Whileuser-friendly,itrequiresoptimizationtoavoidper

ACID attributes include atomicity, consistency, isolation and durability, and are the cornerstone of database design. 1. Atomicity ensures that the transaction is either completely successful or completely failed. 2. Consistency ensures that the database remains consistent before and after a transaction. 3. Isolation ensures that transactions do not interfere with each other. 4. Persistence ensures that data is permanently saved after transaction submission.

MySQL is not only a database management system (DBMS) but also closely related to programming languages. 1) As a DBMS, MySQL is used to store, organize and retrieve data, and optimizing indexes can improve query performance. 2) Combining SQL with programming languages, embedded in Python, using ORM tools such as SQLAlchemy can simplify operations. 3) Performance optimization includes indexing, querying, caching, library and table division and transaction management.

MySQL uses SQL commands to manage data. 1. Basic commands include SELECT, INSERT, UPDATE and DELETE. 2. Advanced usage involves JOIN, subquery and aggregate functions. 3. Common errors include syntax, logic and performance issues. 4. Optimization tips include using indexes, avoiding SELECT* and using LIMIT.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Linux new version
SublimeText3 Linux latest version

Zend Studio 13.0.1
Powerful PHP integrated development environment