目录
一、 架构
二、 安装Mysql server
三、 同步DB1和DB2数据库
四、 复制配置
五、 复制验证
首页 数据库 mysql教程 Master-MasterMySQL复制安装配置试验_MySQL

Master-MasterMySQL复制安装配置试验_MySQL

Jun 01, 2016 pm 01:02 PM

 

一、 架构

\

 

二、 安装Mysql server

下面以在DB1(192.168.0.10)的配置为例,DB2的配置基本一样,只要修改my.cnf中的server_id =2

1. 安装mysql-server

db1# apt-get install mysql-server

注:

? 如果安装有问题,执行apt-getupdate更新源后再重试

? 安装过程中需要输入root密码,设置后记住(123456)

2. 验证数据库安装

db1# mysql -p

Enter password:

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 42

Server version: 5.5.38-0ubuntu0.12.04.1 (Ubuntu)

 

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

 

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

(db1)mysql> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| mysql |

| performance_schema |

+--------------------+

3 rows in set (0.00 sec)

(db1)mysql>

3. 创建测试数据库

CREATE DATABASE /*!32312 IF NOT EXISTS*/`test` /*!40100 DEFAULT CHARACTER SET latin1 */;

USE `test`;

/*Table structure for table `user` */ 

DROP TABLE IF EXISTS `user`; 

CREATE TABLE `user` (

`name` varchar(16) DEFAULT NULL,

`age` int(11) DEFAULT NULL

) ENGINE=InnoDB DEFAULT CHARSET=utf8;

此步骤略

4. my.cnf配置修改

以下红色部分为修改的配置

The MySQL database server configuration file.

#

# You can copy this to one of:

# - "/etc/mysql/my.cnf" to set global options,

# - "~/.my.cnf" to set user-specific options.

#

# One can use all long options that the program supports.

# Run program with --help to get a list of available options and with

# --print-defaults to see which it would actually understand and use.

#

# For explanations see

# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

 

# This will be passed to all mysql clients

# It has been reported that passwords should be enclosed with ticks/quotes

# escpecially if they contain "#" chars...

# Remember to edit /etc/mysql/debian.cnf when changing the socket location.

[client]

port = 3306

socket = /var/run/mysqld/mysqld.sock 

# Here is entries for some specific programs

# The following values assume you have at least 32M ram 

# This was formally known as [safe_mysqld]. Both versions are currently parsed.

[mysqld_safe]

socket = /var/run/mysqld/mysqld.sock

nice = 0 

[mysqld]

#

# * Basic Settings

#

user = mysql

pid-file = /var/run/mysqld/mysqld.pid

socket = /var/run/mysqld/mysqld.sock

port = 3306

basedir = /usr

datadir = /var/lib/mysql

tmpdir = /tmp

lc-messages-dir = /usr/share/mysql

skip-external-locking

#

# Instead of skip-networking the default is now to listen only on

# localhost which is more compatible and is not less secure.

#bind-address = 127.0.0.1

bind-address =0.0.0.0

#

# * Fine Tuning

#

key_buffer = 16M

max_allowed_packet = 16M

thread_stack = 192K

thread_cache_size = 8

# This replaces the startup script and checks MyISAM tables if needed

# the first time they are touched

myisam-recover = BACKUP

#max_connections = 100

#table_cache = 64

#thread_concurrency = 10

#

# * Query Cache Configuration

#

query_cache_limit = 1M

query_cache_size = 16M

#

# * Logging and Replication

#

# Both location gets rotated by the cronjob.

# Be aware that this log type is a performance killer.

# As of 5.1 you can enable the log at runtime!

#general_log_file = /var/log/mysql/mysql.log

#general_log = 1

#

# Error log - should be very few entries.

#

log_error = /var/log/mysql/error.log

#

# Here you can see queries with especially long duration

#log_slow_queries = /var/log/mysql/mysql-slow.log

#long_query_time = 2

#log-queries-not-using-indexes

#

# The following can be used as easy to replay backup logs or for replication.

# note: if you are setting up a replication slave, see README.Debian about

# other settings you may need to change.

#server-id = 1

#log_bin = /var/log/mysql/mysql-bin.log

expire_logs_days = 10

max_binlog_size = 100M

#binlog_do_db = include_database_name

#binlog_ignore_db = include_database_name

#

# * InnoDB

#

# InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/.

# Read the manual for more InnoDB related options. There are many!

#

# * Security Features

#

# Read the manual, too, if you want chroot!

# chroot = /var/lib/mysql/

#

# For generating SSL certificates I recommend the OpenSSL GUI "tinyca".

#

# ssl-ca=/etc/mysql/cacert.pem

# ssl-cert=/etc/mysql/server-cert.pem

# ssl-key=/etc/mysql/server-key.pem 

server_id = 1

log_bin = /var/log/mysql/mysql-bin.log

log_bin_index = /var/log/mysql/mysql-bin.log.index

relay_log = /var/log/mysql/mysql-relay-bin

relay_log_index = /var/log/mysql/mysql-relay-bin.index

expire_logs_days = 10

max_binlog_size = 100M

log_slave_updates = 1

auto_increment_increment = 2

auto_increment_offset = 1 

[mysqldump]

quick

quote-names

max_allowed_packet = 16M 

[mysql]

#no-auto-rehash # faster start of mysql but no tab completition 

[isamchk]

key_buffer = 16M 

#

# * IMPORTANT: Additional settings that can override those from this file!

# The files must end with '.cnf', otherwise they'll be ignored.

#

!includedir /etc/mysql/conf.d/

5. 创建三种角色的用户

表格 1

角色

功能

权限

monitor user

MMM(Multi-Master replication manager of MYSQL)监控各主控的健康状况

REPLICATION CLIENT

agent user

MMM代理用来设置只读属性,复制主控等

SUPER, REPLICATION CLIENT, PROCESS

replicate user

用来复制

REPLICATION SLAVE

表格 2

(db1)mysql> GRANT REPLICATION CLIENT ON *.* TO 'mmm_monitor'@'115.29.198.150' IDENTIFIED BY '123456';

Query OK, 0 rows affected (0.00 sec)

 

(db1)mysql> GRANT SUPER, REPLICATION CLIENT, PROCESS ON *.* TO 'mmm_agent'@'42.96.%.%' IDENTIFIED BY '123456';

Query OK, 0 rows affected (0.00 sec)

 

(db1)mysql> GRANT REPLICATION SLAVE ON *.* TO 'replication'@'42.96.%.%' IDENTIFIED BY '123456';

Query OK, 0 rows affected (0.00 sec)

三、 同步DB1和DB2数据库

首先假设DB1包含正确的数据(即使是空数据库),进行DB1和DB2直接的数据同步。

1. 以下在DB1所在服务器上执行数据导出

(db1)mysql> FLUSH TABLES WITH READ LOCK;

(db1)mysql> SHOW MASTER STATUS;

+------------------+----------+--------------+------------------+

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000002 | 616 | | |

+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

(db1)mysql>

2. 另开一个命令窗口导出数据

(db1)# mysqldump -u root -p --all-databases > /tmp/database-backup.sql

3. 解锁第一个窗口

(db1)mysql> UNLOCK TABLES;

Query OK, 0 rows affected (0.00 sec)

 

(db1)mysql>

4. 将DB1导出的数据导入DB2

1) 拷贝到DB2

(db1)# scp database-backup.sql root@192.168.0.11:/tmp/

The authenticity of host '192.168.0.11 (192.168.0.11)' can't be established.

ECDSA key fingerprint is 55:84:03:9e:d9:74:cc:cd:03:59:23:3f:df:d9:77:a5.

Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added '192.168.0.11' (ECDSA) to the list of known hosts.

root@192.168.0.11's password:

database-backup.sql 100% 528KB 527.9KB/s 00:00

(db1):/tmp#

2) 导入DB2

(db2)# mysql -u root -p

Enter password:

(db2)#

3) 应用权限

(db2)mysql> FLUSH PRIVILEGES;

Query OK, 0 rows affected (0.00 sec)

4) 拷贝debian.cnf

将/etc/mysql/debian.cnf 从 DB1拷贝到DB2, 这个文件是用来启动和关闭mysql用的。

(db1)# scp debian.cnf root@192.168.0.11:/tmp/

在DB2上备份原来的debian.cnf,然后使用从DB1拷贝过来的debian.cnf

(db2)# mv /etc/mysql/debian.cnf /etc/mysql/debian.cnf.orign

(db2)# cp -f debian.cnf /etc/mysql/debian.cnf

上述步骤完成后准备工作都做好了,可以开始配置复制。

四、 复制配置

1. 在DB2上执行:

(db2)mysql> CHANGE MASTER TO master_host='192.168.0.10', master_port=3306, master_user='replication', master_password='123456', master_log_file='mysql-bin.000002', master_log_pos=616;

Query OK, 0 rows affected (0.04 sec)

注:master_log_file='mysql-bin.000002', master_log_pos=616 信息来自于在DB1上执行

mysql> show master status;

2. 在DB2上启动SLAVE

(db2)mysql> START SLAVE;

Query OK, 0 rows affected (0.00 sec)

3. DB2上检查复制进程

(db2)mysql> SHOW SLAVE STATUS\G

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.0.10

Master_User: replication

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000002

Read_Master_Log_Pos: 616

Relay_Log_File: mysql-relay-bin.000002

Relay_Log_Pos: 253

Relay_Master_Log_File: mysql-bin.000002

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

Replicate_Do_DB:

Replicate_Ignore_DB:

Replicate_Do_Table:

Replicate_Ignore_Table:

Replicate_Wild_Do_Table:

Replicate_Wild_Ignore_Table:

Last_Errno: 0

Last_Error:

Skip_Counter: 0

Exec_Master_Log_Pos: 616

Relay_Log_Space: 409

Until_Condition: None

Until_Log_File:

Until_Log_Pos: 0

Master_SSL_Allowed: No

Master_SSL_CA_File:

Master_SSL_CA_Path:

Master_SSL_Cert:

Master_SSL_Cipher:

Master_SSL_Key:

Seconds_Behind_Master: 0

Master_SSL_Verify_Server_Cert: No

Last_IO_Errno: 0

Last_IO_Error:

Last_SQL_Errno: 0

Last_SQL_Error:

Replicate_Ignore_Server_Ids:

Master_Server_Id: 1

1 row in set (0.00 sec)

4. 配置从DB2复制到DB1

1) DB2状态

(db2)mysql> SHOW MASTER STATUS;

+------------------+----------+--------------+------------------+

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000002 | 107 | | |

+------------------+----------+--------------+------------------+

1 row in set (0.01 sec)

mysql>

2) DB1复制的配置、启动和检查

(db1)mysql> CHANGE MASTER TO master_host = '192.168.0.11', master_port=3306, master_user='replication',

-> master_password='123456', master_log_file='mysql-bin.000002', master_log_pos=107;

Query OK, 0 rows affected (0.05 sec)

 

mysql> START SLAVE;

Query OK, 0 rows affected (0.00 sec)

 

mysql> SHOW SLAVE STATUS\G

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: 192.168.0.11

Master_User: replication

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000002

Read_Master_Log_Pos: 107

Relay_Log_File: mysql-relay-bin.000002

Relay_Log_Pos: 253

Relay_Master_Log_File: mysql-bin.000002

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

Replicate_Do_DB:

Replicate_Ignore_DB:

Replicate_Do_Table:

Replicate_Ignore_Table:

Replicate_Wild_Do_Table:

Replicate_Wild_Ignore_Table:

Last_Errno: 0

Last_Error:

Skip_Counter: 0

Exec_Master_Log_Pos: 107

Relay_Log_Space: 409

Until_Condition: None

Until_Log_File:

Until_Log_Pos: 0

Master_SSL_Allowed: No

Master_SSL_CA_File:

Master_SSL_CA_Path:

Master_SSL_Cert:

Master_SSL_Cipher:

Master_SSL_Key:

Seconds_Behind_Master: 0

Master_SSL_Verify_Server_Cert: No

Last_IO_Errno: 0

Last_IO_Error:

Last_SQL_Errno: 0

Last_SQL_Error:

Replicate_Ignore_Server_Ids:

Master_Server_Id: 2

1 row in set (0.00 sec)

 

mysql>

上述步骤完成MASTER-MASTER的复制配置,下面进行测试。

五、 复制验证

1. 在DB1上插入一条数据

(db1)mysql> select * from user;

Empty set (0.00 sec)

(db1)mysql> insert into user(name,age) values('user1',20);

Query OK, 1 row affected (0.03 sec)

(db1)mysql>

2. 在DB2上检查

(db2)mysql> select * from user;

Empty set (0.00 sec)

(db2)mysql> select * from user;

+-------+------+

| name | age |

+-------+------+

| user1 | 20 |

+-------+------+

1 row in set (0.00 sec)

(db2)mysql>

表面在DB1插入的(user1,20)这条记录已经被复制到DB2中。

3. 在DB2上插入一条数据

(db2)mysql> insert into user(name,age) values('user2',30);

Query OK, 1 row affected (0.02 sec

4. 在DB1上进行检查

(db1)mysql> select * from user;

+-------+------+

| name | age |

+-------+------+

| user1 | 20 |

| user2 | 30 |

+-------+------+

2 rows in set (0.00 sec)

表面在DB2插入的(user2,30)这条记录已经被复制到DB1中。

上述测试表面,DB1DB2的MM配置完全成功。

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

与MySQL中使用索引相比,全表扫描何时可以更快? 与MySQL中使用索引相比,全表扫描何时可以更快? Apr 09, 2025 am 12:05 AM

全表扫描在MySQL中可能比使用索引更快,具体情况包括:1)数据量较小时;2)查询返回大量数据时;3)索引列不具备高选择性时;4)复杂查询时。通过分析查询计划、优化索引、避免过度索引和定期维护表,可以在实际应用中做出最优选择。

说明InnoDB全文搜索功能。 说明InnoDB全文搜索功能。 Apr 02, 2025 pm 06:09 PM

InnoDB的全文搜索功能非常强大,能够显着提高数据库查询效率和处理大量文本数据的能力。 1)InnoDB通过倒排索引实现全文搜索,支持基本和高级搜索查询。 2)使用MATCH和AGAINST关键字进行搜索,支持布尔模式和短语搜索。 3)优化方法包括使用分词技术、定期重建索引和调整缓存大小,以提升性能和准确性。

可以在 Windows 7 上安装 mysql 吗 可以在 Windows 7 上安装 mysql 吗 Apr 08, 2025 pm 03:21 PM

是的,可以在 Windows 7 上安装 MySQL,虽然微软已停止支持 Windows 7,但 MySQL 仍兼容它。不过,安装过程中需要注意以下几点:下载适用于 Windows 的 MySQL 安装程序。选择合适的 MySQL 版本(社区版或企业版)。安装过程中选择适当的安装目录和字符集。设置 root 用户密码,并妥善保管。连接数据库进行测试。注意 Windows 7 上的兼容性问题和安全性问题,建议升级到受支持的操作系统。

InnoDB中的聚类索引和非簇索引(次级索引)之间的差异。 InnoDB中的聚类索引和非簇索引(次级索引)之间的差异。 Apr 02, 2025 pm 06:25 PM

聚集索引和非聚集索引的区别在于:1.聚集索引将数据行存储在索引结构中,适合按主键查询和范围查询。2.非聚集索引存储索引键值和数据行的指针,适用于非主键列查询。

mysql:简单的概念,用于轻松学习 mysql:简单的概念,用于轻松学习 Apr 10, 2025 am 09:29 AM

MySQL是一个开源的关系型数据库管理系统。1)创建数据库和表:使用CREATEDATABASE和CREATETABLE命令。2)基本操作:INSERT、UPDATE、DELETE和SELECT。3)高级操作:JOIN、子查询和事务处理。4)调试技巧:检查语法、数据类型和权限。5)优化建议:使用索引、避免SELECT*和使用事务。

mysql用户和数据库的关系 mysql用户和数据库的关系 Apr 08, 2025 pm 07:15 PM

MySQL 数据库中,用户和数据库的关系通过权限和表定义。用户拥有用户名和密码,用于访问数据库。权限通过 GRANT 命令授予,而表由 CREATE TABLE 命令创建。要建立用户和数据库之间的关系,需创建数据库、创建用户,然后授予权限。

mysql 和 mariadb 可以共存吗 mysql 和 mariadb 可以共存吗 Apr 08, 2025 pm 02:27 PM

MySQL 和 MariaDB 可以共存,但需要谨慎配置。关键在于为每个数据库分配不同的端口号和数据目录,并调整内存分配和缓存大小等参数。连接池、应用程序配置和版本差异也需要考虑,需要仔细测试和规划以避免陷阱。在资源有限的情况下,同时运行两个数据库可能会导致性能问题。

说明不同类型的MySQL索引(B树,哈希,全文,空间)。 说明不同类型的MySQL索引(B树,哈希,全文,空间)。 Apr 02, 2025 pm 07:05 PM

MySQL支持四种索引类型:B-Tree、Hash、Full-text和Spatial。1.B-Tree索引适用于等值查找、范围查询和排序。2.Hash索引适用于等值查找,但不支持范围查询和排序。3.Full-text索引用于全文搜索,适合处理大量文本数据。4.Spatial索引用于地理空间数据查询,适用于GIS应用。

See all articles