MySQL主从,半同步,主主复制
我们知道,MySQL数据库的二进制日志记录着每一个明确或者潜在可能导致数据库发生改变的sql语句,因此我们可以基于二进制日志来实
MySQL Replication
我们知道,MySQL数据库的二进制日志记录着每一个明确或者潜在可能导致数据库发生改变的sql语句,因此我们可以基于二进制日志来实现mysql的主从一致。而我们在此提到的mysql的复制的简单过程就是:
首先mysql的主服务器(Master)上有一个叫dump的线程,负责将二进制日志读取进来,发送给slave.
而后mysql的从服务器(Slave)上有个I/O线程 ,负责接收Master传送过来的二进制日志数据,并且将此写入到本地的中继日志中,此时会有一个SQL线程,每次中继日志读取一句sql语句, 并在从Slave上应用,以实现主从服务器的一致。
首先需要提供2台mysql版本一致的mysql服务器,,在此选用的mysql-5.5.20,平台为RedHat5.4
一、安装mysql-5.5.20
# groupadd -r mysql
# useradd -g mysql -r -s /sbin/nologin –M mysql
//新建用户以安全方式运行进程
# chown -R mysql:mysql /mydata/data
# tar xf mysql-5.5.20-linux2.6-i686.tar.gz -C /usr/local
//将下载好的mysql软件解压安装
# cd /usr/local/
# ln -sv mysql-5.5.20-linux2.6-i686 mysql
# cd mysql
# chown -R mysql:mysql .
# scripts/mysql_install_db --user=mysql --datadir=/mydata/mysql
//初始化mysql,此处使用的/mydata/mysql存放mysql的数据,建议将/mydata挂载至一个逻辑卷,方便数据备份
# chown -R root .
# cd /usr/local/mysql
# cp support-files/my-large.cnf /etc/my.cnf
#vim /etc/my.cnf
thread_concurrency = 2 //修改此项,我们的cpu为双核的
datadir = /mydata/data //添加此项,指定数据存放的目录
# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
# chkconfig --add mysqld
# chkconfig mysqld on
二:为使用mysql的安装符合系统使用规范,并将其开发组件导出给系统使用,需如下操作
#vim/etc/man.config //输出mysql的man手册至man命令的查找路径
MANPATH /usr/local/mysql/man //添加此行
# ln -sv /usr/local/mysql/include /usr/include/mysql
//输出mysql的头文件至系统头文件路径/usr/include
# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
//输出mysql的库文件给系统库查找路径
# ldconfig //让系统重新载入系统库
#vim /etc/profile //修改PATH环境变量,让系统可以直接使用mysql的相关命令
PATH=$PATH:/usr/local/mysql/bin //添加此行
三:实现主从复制
master ip:192.168.1.10
slave ip:192.168.1.11
On Master :(在主服务器的操作)
#vim /etc/my.cnf 确保证如下选项:
server-id = 1 //主服务器的身份标识
log-bin=mysql-bin //开启二进制日志
sync_binlog=1 //事务提交立即写入到二进制日志
innodb_flush_logs_at_trx_commit=1 //事务提交之时,立即将二进制日志写入磁盘
#mysql –uroot –p
mysql> grant replication client,replication slave on *.* to cclo@’192.168.1.%’ identified by ‘12345’;
//此处创建一个用户名为cclo,密码为12345的用户,其权限为client和replication
mysql> flush privileges;
mysql> show grants for cclo@’192.168.1.%’; //验证此用户的权限
On Slave:(在从服务器的操作)
#vim /etc/my.cnf
[mysqld]
server-id = 11 // 数值只要保证和Master不同即可
#log-bin=mysql-bin //slave的二进制日志一般用不到,暂时关闭
relay-log=relay-bin //开启中继日志
relay-log-index=relay-bin.index
read_only=1 //此项为阻止普通用户写从服务器
skip_slave_start=1
//mysql服务关闭后,当服务开启时跳过自动启动主从复制(如需自动启动,此项略过)
#service mysqld restart
#mysql –uroot –p
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| MySQL-bin.000003 | 374 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
mysql> change master to master_host='192.168.1.10',master_user='cclo',master_password='12345',master_log_file='mysql-bin.000003',master_log_pos=374;
//指定Master的ip及复制的用户及二进制日志的位置
mysq> start slave; //启动主从复制
mysql > show slave status; //查询从服务器的工作属性,确保以下2项
Slave_IO_Runing: Yes
Slave_SQL_Runing: Yes //确保此2项为Yes的状态

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics

Data backup and failure recovery: Discussion on the importance of MySQL master-slave replication in cluster mode Introduction: In recent years, with the continuous growth of data scale and complexity, database backup and failure recovery have become particularly important. In distributed systems, MySQL master-slave replication is widely used in cluster mode to provide high availability and fault tolerance. This article will explore the importance of MySQL master-slave replication in cluster mode and give some code examples. 1. Basic principles and advantages of MySQL master-slave replication MySQL master-slave replication is a general

Cope with high concurrency with ease: Analysis of the performance advantages of MySQL master-slave replication as a cluster technology. With the rapid development of the Internet, user visits to websites and applications have shown an explosive growth trend. In this high-concurrency situation, how to ensure system stability and performance has become an important task for every developer and system administrator. In databases, MySQL master-slave replication technology is widely used and has become one of the effective solutions to deal with high concurrency. This article will explore the performance advantages of MySQL master-slave replication as a cluster technology. first

Decrypting MySQL master-slave replication: Revealing its key implementation mechanism in cluster mode Introduction: In modern database systems, high availability and flexibility of data are very important. As an open source relational database management system, MySQL has a wide range of applications in meeting user needs. MySQL's master-slave replication is a very critical part of the MySQL database architecture and is used to achieve data backup and high availability. This article will focus on revealing the key implementation mechanism of MySQL master-slave replication, especially in cluster mode.

Optimizing database performance: The best way to use MySQL master-slave replication in cluster technology Abstract: With the rapid development of the Internet, database performance issues have become the focus of various enterprises and organizations. MySQL master-slave replication technology plays an important role in solving database performance bottlenecks. This article will introduce the concepts and principles of MySQL master-slave replication, as well as the best use methods in cluster technology, to help readers optimize database performance. 1. Introduction As the amount of data continues to increase, database performance problems have become increasingly prominent. How to optimize numbers

Is MySQL master-slave replication a cluster technology or a load balancing technology? Summary of analysis and differences: MySQL master-slave replication is a database replication technology used to synchronize database data on multiple servers. This article will analyze and distinguish the differences between MySQL master-slave replication, cluster technology and load balancing technology in terms of technical principles, application scenarios and functional characteristics. Introduction: In modern Internet applications, high availability and scalability of databases are crucial. MySQL master-slave replication is one of the common solutions, however,

Detailed explanation of the functions and advantages of MySQL master-slave replication in cluster technology Introduction MySQL is a powerful relational database management system that is widely used in various large-scale websites and applications. As the amount of data increases and access requests increase, the pressure on a single MySQL server gradually increases. In order to improve the performance and reliability of the database, people begin to adopt cluster technology, among which MySQL master-slave replication is one of the commonly used technologies. means. MySQL master-slave replication principle MySQL master-slave replication refers to the

With the rapid development of the Internet, the amount of data in application systems is increasing, and the requirements for database performance and reliability are also getting higher and higher. As one of the most commonly used open source relational databases, MySQL has high performance and stability and is widely used in various enterprise-level applications. As a commonly used data replication solution, MySQL master-slave replication can improve data reliability and read and write performance, and is widely used in large-scale data applications. The cluster feature of MySQL master-slave replication refers to synchronizing the data of the master database through the replication mechanism.

Tapping into the cluster technology potential of MySQL master-slave replication: Comparative evaluation of open source solutions and commercial solutions. With the continuous development of Internet business and the increasing amount of data, the demand for database cluster solutions has become increasingly strong. MySQL master-slave replication technology just meets this demand. It can process the read and write operations of the database separately on multiple nodes, improving the read performance and availability of the database. This article will explore the potential of cluster technology in MySQL master-slave replication, and conduct a comparative evaluation of open source solutions and commercial solutions.
