MySQL主从复制

来源:互联网 发布:c语言随机数头文件 编辑:程序博客网 时间:2024/06/05 00:24

MySQL主从复制

一、简述原理:
MySQL主从复制原理图

这里写代码片

1、master记录二进制日志

2、slave的I/O线程读取master的二进制日志,并将其写入到中继日志中,SQL线程从中继日志中读取时间,并重放其中事件,更新slave的数据

二、准备工作:

关闭防火墙

#server iptables stop

关闭开机自启

#chkconfig iptables off

关闭selinux

#setenforce 0

在/etc/selinux/config 中,将SELINUX=enforcing改为SELINUX=disabled

同步时间

#ntpdate 202.120.2.101

=======================================================

三、安装mysql5.6

#cd /usr/local/src

解压mysql包

#tar -zxf MySQL-5.6.23-1.el6.x86_64.rpm-bundle.tar

用yum安装mysql,解决依赖关系

#yum install MySQL-shared-compat-5.6.23-1.el6.x86_64.rpm#yum install MySQL-server-5.6.23-1.el6.x86_64.rpm#yum install MySQL-client-5.6.23-1.el6.x86_64.rpm#yum install MySQL-devel-5.6.23-1.el6.x86_64.rpm#yum install MySQL-shared-5.6.23-1.el6.x86_64.rpm

创建数据目录

#mkdir -pv /home/mydata/data#chown -R mysql.mysql /home//mydata#chmod -R +w /home/mydata

四、修改配置文件

配置文件【主】

#cat /etc/my.cnf[mysqld]# Remove leading # and set to the amount of RAM for the most important data# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.# innodb_buffer_pool_size = 128M# Remove leading # to turn on a very important data integrity option: logging# changes to the binary log between backups.# log_bin# These are commonly set, remove the # and set as required.# basedir = .....datadir = /home/mydata/dataport = 3306socket = /var/lib/mysql/mysql.socklog-bin = master-binserver_id = 1skip-external-lockingkey_buffer_size = 256Mmax_allowed_packet = 1Mtable_open_cache = 256pid-file = /home/mydata/data/mysql.pid# Remove leading # to set options mainly useful for reporting servers.# The server defaults are faster for transactions and fast SELECTs.# Adjust sizes as needed, experiment to find the optimal values.# join_buffer_size = 128Msort_buffer_size = 1Mread_buffer_size = 1Mread_rnd_buffer_size = 1Mmyisam_sort_buffer_size = 64Mthread_cache_size = 8query_cache_size= 16Minnodb_file_per_table = onthread_concurrency = 8skip_name_resolve = onsql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

配置文件【从】

[mysqld]# Remove leading # and set to the amount of RAM for the most important data# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.# innodb_buffer_pool_size = 128M# Remove leading # to turn on a very important data integrity option: logging# changes to the binary log between backups.# log_bin# These are commonly set, remove the # and set as required.# basedir = .....datadir = /home/mydata/dataport = 3306server_id = 2pid-file = /home/mydata/data/mysql.pidrelay-log = relay-binsocket = /var/lib/mysql/mysql.sockskip-external-lockingkey_buffer_size = 256Mmax_allowed_packet = 1Mtable_open_cache = 256# Remove leading # to set options mainly useful for reporting servers.# The server defaults are faster for transactions and fast SELECTs.# Adjust sizes as needed, experiment to find the optimal values.# join_buffer_size = 128Msort_buffer_size = 1Mread_buffer_size = 1Mread_rnd_buffer_size = 1Mmyisam_sort_buffer_size = 64Mthread_cache_size = 8query_cache_size= 16Minnodb_file_per_table = onthread_concurrency = 8skip_name_resolve = onsql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

初始化MySQL

#/usr/bin/mysql_install_db --datadir=/usr/local/work/mydata --user=mysql

启动服务

#service mysql start

设置root密码

#mysql>SET PASSWORD FOR 'root'@'localhost' = PASSWORD('123456');

【主节点】授权复制权限账号给从节点

#mysql>grant replication client,replication slave on *.* to 'repuser'@'192.168.%.%' identified by 'reppasswd';#mysql>flush privileges;

查看状态

#mysql>show master status;Slave_IO_Running: YesSlave_SQL_Running: Yes

【从节点】指定主节点,复制账号

#mysql>change master to master_host='192.168.1.6',master_user='repuser',master_password='reppasswd',master_log_file='master-bin.000001',master_log_pos=120;查看状态#mysql>show slave status\GSlave_IO_Running: YesSlave_SQL_Running: Yes

备注:这里只讲到mysql主从复制,但没有讲到如何实现高可用,可以利用keepalived来实现。

原创粉丝点击