安装网上的一些文章配置MySQL的主从同步机制,无奈重启从MySQL时出现异常,说不认识参数master_host /usr/sbin/mysqld: unknown variable master_host=10.0.2.160 原来我使用的是MySQL5.5,而大部分配置是基于5.5之前的版本的,Mysql版本从5.1.7以后开始就不
安装网上的一些文章配置MySQL的主从同步机制,无奈重启从MySQL时出现异常,说不认识参数master_host
/usr/sbin/mysqld: unknown variable ‘master_host=10.0.2.160′
原来我使用的是MySQL5.5,而大部分配置是基于5.5之前的版本的,Mysql版本从5.1.7以后开始就不支持“master-host”类似的参数。详情可以查看http://dev.mysql.com/doc/refman/5.5/en/replication-howto.html
MySQL 复制配置主从同步
A B 为两台 MySQL 服务器,均开启二进制日志,数据库版本 MySQL 5.5。
在两台服务器上编辑配置文件,以下配置添加到[mysqld]
一.MySQL数据库迁移
首先如果要同步已经存在的MySQL数据,我们最好先通过全量同步到从库。
同步的方法可以参考:
1.
2.MySQL下的数据库迁移
二.服务器配置
[A 主服务器10.0.2.160]
server-id = 1 binlog-ignore-db = mysql binlog-ignore-db =?information_schema sync-binlog = 1
[B 从服务器10.0.2.151]
server-id = 2 replicate-ignore-db = mysql replicate-ignore-db =?information_schema
重新启动MySQL服务。
使用命令?SHOW MASTER STATUS
查看主服务状态:
mysql> SHOW MASTER STATUS; +---------------------+----------+--------------+--------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +---------------------+----------+--------------+--------------------------+ | mysql-binlog.000001 | 525 | | mysql,information_schema | +---------------------+----------+--------------+--------------------------+ 1 row in set (0.00 sec)
主服务器A授权同步账户:
GRANT REPLICATION SLAVE ON *.* TO 'root'@'10.0.2.151' IDENTIFIED BY 'password'; FLUSH PRIVILEGES;
slave?start开启复制:
CHANGE MASTER TO MASTER_HOST='10.0.2.160',MASTER_USER='root', MASTER_PASSWORD='password',MASTER_LOG_FILE='?mysql-binlog.000001',MASTER_LOG_POS=106;
停止slave同步:mysql>slave?start
停止slave同步:mysql> slave stop
在slave服务器上使用show slave status查看slave同步的状态
mysql主从同步
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
如果上面为yes表示同步成功。
很不幸的是我第一次实验,上述参数都为No
解决:MySQL主从同步位置不一致
1.首先停掉Slave服务:
mysql> slave stop
2.到主服务器上查看主机状态:
记录File和Position对应的值。
mysql> SHOW MASTER STATUS; +---------------------+----------+--------------+--------------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +---------------------+----------+--------------+--------------------------+ | mysql-binlog.000002 | 958 | | mysql,information_schema | +---------------------+----------+--------------+--------------------------+ 1 row in set (0.00 sec)
3.到slave服务器上执行手动同步:
CHANGE MASTER TO MASTER_HOST='10.0.2.160',MASTER_USER='root', MASTER_PASSWORD='password',MASTER_LOG_FILE='?mysql-binlog.000002',MASTER_LOG_POS=958;
启动slave同步:slave start
再次查看slave状态发现:
Slave_IO_Running: Yes Slave_SQL_Running: Yes
如果上面为yes表示同步成功。
我们知道,因为DATA REPLICATION机制完全是基于在主上执行的增量SQL要被传播到辅服务器上,并且被成功运行。这就势必要求:在运行此机制前,主辅数据库中数据是一致的;以及在运行此机制中,辅数据库禁止来自其他的SQL(非主上传播过来SQL)的写操作。但是在运行中仍然可能遇到不一致的产生,这会导致通信无法正常继续下去。因此一旦主从出现问题,首先应该解决同步位置的问题,修复丢失的数据。
MySQL 复制配置双机互为主从
在两台服务器上编辑配置文件,以下配置添加到[mysqld]
一、服务器参数
[A 服务器 192.168.1.100]
server-id = 1 binlog-do-db = test binlog-ignore-db = mysql replicate-do-db = test replicate-ignore-db = mysql sync-binlog = 1
[B 服务器 192.168.1.101]
server-id = 2 binlog-do-db = test binlog-ignore-db = mysql replicate-do-db = test replicate-ignore-db = mysql sync-binlog = 1
二、操作步骤
# A B 服务器停止同步
STOP SLAVE;
# A B 服务器清空MASTER日志
RESET MASTER;
# A B 服务器清空SLAVE日志
RESET SLAVE;
# A 服务器授权同步账户 (我们会同步一次复制数据库文件,所以授权为192.168.1%)
GRANT REPLICATION SLAVE ON *.* TO 'master_slave'@'192.168.1%' IDENTIFIED BY 'master_slave123!@#'; FLUSH PRIVILEGES;
# A 服务器锁表(锁表状态下不能终止mysql进程,否则会失败)
FLUSH TABLES WITH READ LOCK;
# 如果使用SSH,需要重新开启,复制数据库文件。
tar -cvf /tmp/mysql-data.tar /www/mysql tar -xvf /tmp/mysql-data.tar -C /
# 查看 A 服务器主机状态(记录二进制开始文件,位置)
SHOW MASTER STATUS;
# B 服务器锁表(锁表状态下不能终止mysql进程,否则会失败)
FLUSH TABLES WITH READ LOCK;
# 修改 B 服务器配置 (修改为A服务器的主机状态)
CHANGE MASTER TO MASTER_HOST='192.168.1.100',MASTER_USER='master_slave', MASTER_PASSWORD='master_slave123!@#',MASTER_LOG_FILE='binlog.000001',MASTER_LOG_POS=106;
# 开启 B 服务器同步进程
START SLAVE;
# 查看 B 服务器同步状态是否正常
SHOW SLAVE STATUS;
# 查看 B 服务器主机(记录二进制开始文件,位置)
SHOW MASTER STATUS;
# 修改 A 服务器配置 (修改为B服务器的主机状态)
CHANGE MASTER TO MASTER_HOST='192.168.1.101',MASTER_USER='master_slave',MASTER_PASSWORD='master_slave123!@#',MASTER_LOG_FILE='binlog.000001',MASTER_LOG_POS=106;
# 开启 A 服务器同步进程
START SLAVE;
# 分别查看 A B 服务器同步状态,确定是否成功
SHOW SLAVE STATUS;SHOW MASTER STATUS;
# 解锁 A B 服务器
UNLOCK TABLES;
# 数据测试分别在 A B 服务器上创建表插入数据测试
DROP TABLE IF EXISTS `test`; CREATE TABLE `test` ( `id` int(11) NOT NULL AUTO_INCREMENT, `value` varchar(100) DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; INSERT INTO `test` VALUES ('1', 'Hello');
注意:
1. 数据库目录下的master.info的内容会覆盖命令行或my.cnf中指定的部分选项,更改配置需删除master.info
2. my.cnf中的master配置在MySQL 6.0以后会取消,官方建议使用动态的CHANGE MASTER
3. 如果只指定ignore-db而不指定do-db。则新创建数据库的操作也会同步。
互为同步配置实例:
1. A B 互为主从同步test, 不同步mysql:
两个数据库配置中均设置:binlog-do-db=test, binlog-ignore-db=mysql,replicate-do-db=test,replicate-ignore-db=mysql
2. A B 互为主从只同步test,不同步其他数据库,新创建的也不会同步
两个数据库配置中均设置:binlog-do-db=test,replicate-do-db=test
3. A B 互为主从不同步mysql, 同步其他数据库,譬如创建的新数据库也会同步
两个数据库配置中均设置:binlog-ignore-db=mysql,replicate-ignore-db=mysql
4. A B 互为主从同步所有数据库,包括新建的数据库
两个数据库配置中均不设置上述四项。
参考:
官网:http://dev.mysql.com/doc/refman/5.5/en/replication-howto-mysqldump.html
5.17之前的版本配置:http://www.drupal001.com/2012/03/mysql-master-slave-troubles/
MySQL 复制配置双机互为主从:http://www.ncq8.com/2010/10/250.html
原文地址:MySQL5.5主从同步配置及问题, 感谢原作者分享。

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

MySQL/InnoDB supports four transaction isolation levels: ReadUncommitted, ReadCommitted, RepeatableRead and Serializable. 1.ReadUncommitted allows reading of uncommitted data, which may cause dirty reading. 2. ReadCommitted avoids dirty reading, but non-repeatable reading may occur. 3.RepeatableRead is the default level, avoiding dirty reading and non-repeatable reading, but phantom reading may occur. 4. Serializable avoids all concurrency problems but reduces concurrency. Choosing the appropriate isolation level requires balancing data consistency and performance requirements.

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.


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

Zend Studio 13.0.1
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

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

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.