CentOS 6.3下MySQL主从复制笔记
MySQL主从复制结构是基于mysql bin-log日志基础上,从库通过打开IO进程收到主库的bin-log日志增量信息,并保存到本地relay log,而
MySQL主从复制结构是基于mysql bin-log日志基础上,从库通过打开IO进程收到主库的bin-log日志增量信息,并保存到本地relay log,而后再通过打开MYSQL进程从relay log上获取的增量信息并翻译成SQL语句后写到从数据库。
主从复制结构,实际上可以实现两个功能:
1.从库充当主库的数据库备份实例
2.读写分离,主库负责正常读写数据,从库只负责读数据
实际生产环境,,因为很多应用实际读数据库的次数远大于写数据库的次数,所以在项目开发初期,编写程序时做一个判断,对所有读的操作全部推到从库,若从库无法获取数据,则再向主库获取数据,从而在一定意义上实现读写分离,缓解主库的IO压力。
所以生产环境下推荐使用这种架构。
系统环境:CentOS6.3 x64
数据库: mysql-5.6.10
mysql master:192.168.100.90
mysql slave:192.168.100.91
一.部署环境:
1.关闭iptables和SELINUX
# service iptables stop
# setenforce 0
# vi /etc/sysconfig/selinux
---------------
SELINUX=disabled
---------------
2.安装配置mysql传送门:
二.主mysql配置:(mysql master)
修改mysql配置文件:
# vi /etc/my.cnf
添加:
-----------------
# Replication Master Server
# bin日志路径
log-bin = /usr/local/mysql/log/bin.log
# 服务器ID号
server-id = 1
# 忽略mysql系统库复制
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
------------
重启服务
# service mysqld restart
登录mysql后台:
# mysql -u root -p123456
查看此刻登录账号:
> select user();
在master为slave添加同步帐号:
> grant replication slave on *.* to 'slave'@'192.168.100.91' identified by '123456';
查看创建的用户:
> select user.host from mysql.user;
查看权限:
> show grants for 'slave'@'192.168.100.91';
mysql锁表只读(其他账户登录mysql后无法进行写表操作,防止备份数据库后,主mysql表更新,导致和从数据库内容不一致)
> flush tables with read lock;
查看锁表倒计时时间:
> show variables like '%timeout%';
------------------------
....
wait_timeout | 28800
------------------------
将master的数据库表全部备份导出,并传送到slave服务器上。
# /usr/local/mysql/bin/mysqldump -u root -p123456 --opt --flush-logs --all-database > /root/allbak.sql
# cd ~
# scp allbak.sql root@192.168.100.91:/root
查看mysql偏移量(数据库如果有写操作,偏移值会递增)
# mysql -u root -p123456 -e "show master status"
----------------------
+------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------+----------+--------------+------------------+-------------------+
| bin.000009 | 120 | | | |
+------------+----------+--------------+------------------+-------------------+
----------------------
保证FILE列和Position列与从库配置一致:

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

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

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

With the rapid development of the Internet, Web applications are increasingly integrating database operations. MySQL is a world-renowned relational database system that is widely used. In highly concurrent web applications, MySQL master-slave replication is an important way to improve database performance and availability. This article will introduce how to use PHP to implement master-slave replication of MySQL database. 1. What is MySQL master-slave replication? MySQL master-slave replication refers to copying data from one MySQL database server to another server.

Building a Highly Available MySQL Cluster: Best Practice Guide for Master-Slave Replication and Load Balancing In recent years, with the rapid development of the Internet, the database has become one of the core data storage and processing engines for most web applications. In this scenario, high availability and load balancing have become important considerations in database architecture design. As one of the most popular open source relational databases, MySQL's cluster deployment solution has attracted much attention. This article will introduce how to implement a highly available database cluster through MySQL master-slave replication and load balancing.

MySQL database is a very popular relational database management system that supports a variety of data replication technologies, among which the more commonly used is master-slave replication technology. This article will introduce the data master-slave replication technology in MySQL, including principles, implementation methods, common problems and countermeasures. 1. Principle of master-slave replication technology The master-slave replication technology in MySQL can copy the data of a MySQL database to other servers to achieve data backup, load balancing, read-write separation and other functions. Its basic principle is to convert the main database

Redis is an open source memory-based key-value storage system that is commonly used in scenarios such as caching, queuing, and real-time data processing. In large-scale applications, in order to improve the availability and performance of Redis, it is often necessary to adopt a distributed architecture, in which master-slave replication is a commonly used mechanism. This article will introduce the master-slave replication function of Redis, including definition, principle, configuration and application scenarios. 1. Definition of Redis master-slave replication refers to automatically synchronizing the data of one Redis node (i.e. master node) to other nodes (i.e. slave node).

How to configure master-slave replication of MySQL database? Master-slave replication of MySQL database is a common data backup and high availability solution. By configuring master-slave replication, you can synchronize data from one MySQL server (master server) to another (slave server), thereby improving database availability and performance. The following describes how to configure master-slave replication in a MySQL database and provides corresponding code examples. Make sure the MySQL server is installed and started. First, make sure MySQL is installed on your system.

Master-slave replication and high-availability architecture in MySQL As Internet applications and data volumes continue to grow, the high availability and scalability of the database are becoming more and more important. As a widely used open source relational database, MySQL provides master-slave replication and high-availability architecture solutions. Master-slave replication refers to the process of using a MySQL database instance as the master database and replicating its data to one or more slave databases (slave). This replication method can achieve redundant backup of data and separation of reading and writing.

Memcached is an open source, high-performance distributed memory object caching system that can be used to speed up web applications and performs particularly well in large-scale data caching. For this system, master-slave replication is a very important function, which can be used to ensure data reliability and high availability. This article will introduce how to use PHP to implement master-slave replication of Memcached database. Introduction to the master-slave mode The master-slave mode is a distributed structure of the Memcached server. It consists of at least two servers: one

Load balancing and disaster recovery in cluster mode: in-depth analysis and practice of MySQL master-slave replication. With the rapid development of the Internet industry, the demand for data storage and processing is getting higher and higher. In response to high concurrent access and massive data storage, cluster mode has become a common solution. Load balancing and disaster recovery are important components of the cluster system, and MySQL master-slave replication is a widely used method. This article will delve into load balancing and disaster recovery in cluster mode, focusing on the principle of MySQL master-slave replication.
