欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MySQL数据库主从同步实战过程详解

程序员文章站 2022-11-22 16:52:36
本文实例讲述了mysql数据库主从同步实战过程。分享给大家供大家参考,具体如下:接上一篇:mysql数据库入门之备份数据库安装环境说明系统环境:[root@~]# cat /etc/redhat-re...

本文实例讲述了mysql数据库主从同步实战过程。分享给大家供大家参考,具体如下:

接上一篇:mysql数据库入门之备份数据库

安装环境说明

系统环境:

[root@~]# cat /etc/redhat-release 
centos release 6.5 (final)
[root@~]# uname -r
2.6.32-431.el6.x86_64

数据库:

由于是模拟环境,主从库在同一台服务器上,服务器ip地址192.168.1.7

  • 主库使用3306端口
  • 从库使用3307端口
  • 数据库数据目录/data

安装mysql数据库服务

下载软件包

今天我们是用二进制安装包进行布署mysql数据库服务,其它方式的安装布署方法请参考前面的文章

[root@~]#wget http://mirrors.sohu.com/mysql/mysql-5.5/mysql-5.5.51-linux2.6-x86_64.tar.gz 

创建数据目录、软件安装目录

[root@~]#mkdir /data{3306,3307} -p
[root@~]#mkdri /application

解压软件

[root@~]#tar zxf mysql-5.5.51-linux2.6-x86_64.tar.gz 
[root@~]#mv mysql-5.5.51-linux2.6-x86_64 /application/mysql-5.5.51
[root@~]#ln -s /application/mysql-5.5.51 /application/mysql

创建用户

[root@~]#groupadd mysql
[root@~]#useradd -g mysql -m mysql

初始化数据库

[root@~]#/application/mysql/scripts/mysql_install_db --basedir=/application/mysql --datadir=/data/3306/data --user=mysql

[root@~]#/application/mysql/scripts/mysql_install_db --basedir=/application/mysql --datadir=/data/3307/data --user=mysql

创建配置文件

[root@~]#vi /data/3306/my.cnf
[client]
port   = 3306
socket   = /data/3306/mysql.sock

[mysql]
no-auto-rehash

[mysqld]
user = mysql
port = 3306
socket = /data/3306/mysql.sock
basedir = /application/mysql
datadir = /data/3306/data
open_files_limit = 1024
back_log = 600

max_connections = 800
max_connect_errors = 3000
table_cache = 614
external-locking = false
max_allowed_packet =8m
sort_buffer_size = 1m
join_buffer_size = 1m
thread_cache_size = 100
thread_concurrency = 2
query_cache_size = 2m
query_cache_limit = 1m
query_cache_min_res_unit = 2k
thread_stack = 192k
tmp_table_size = 2m
max_heap_table_size = 2m
long_query_time = 1

pid-file = /data/3306/mysql.pid
log-bin = /data/3306/mysql-bin
#主从同步的关键点,从库上不需要开启
relay-log = /data/3306/relay-bin
relay-log-info-file = /data/3306/relay-log.info
binlog_cache_size = 1m
max_binlog_cache_size = 1m
max_binlog_size = 2m
expire_logs_days = 7
key_buffer_size = 16m
read_buffer_size = 1m
read_rnd_buffer_size = 1m
bulk_insert_buffer_size = 1m
lower_case_table_names = 1
skip-name-resolve
slave-skip-errors = 1032,1062
replicate-ignore-db=mysql
server-id = 1 #主库从库id 不可相同

[mysqldump]
quick
max_allowed_packet = 2m

[mysqld_safe]
log-error=/data/3306/mysql3306.err
pid-file=/data/3306/mysqld.pid

数据库启动脚本:

[root@~]#vi /data/3306/mysql
#!/bin/sh
port=3306
user="root"
pwd="123456"
path="/application/mysql/bin"
sock="/data/${port}/mysql.sock"

start_mysql()
{
 if [ ! -e "$sock" ];then
  printf "starting mysql...\n"
  /bin/sh ${path}/mysqld_safe --defaults-file=/data/${port}/my.cnf 2>&1 > /dev/null &
 else
  printf "mysql is running...\n"
  exit
 fi
}
stop_mysql()
{
 if [ ! -e "$sock" ];then
  printf "mysql is stopped...\n"
  exit
 else
  printf "stoping mysql...\n"
  ${path}/mysqladmin -u ${user} -p${pwd} -s /data/${port}/mysql.sock shutdown
 fi
}
restart_mysql()
{
 printf "restarting mysql...\n"
 stop_mysql
 sleep 2
 start_mysql
}
case $1 in
start)
 start_mysql
;;
stop)
 stop_mysql
;;
restart)
 restart_mysql
;;
*)
 printf "usage: /data/${port}/mysql {start|stop|restart}\n"
esac

备注:主从库配置文件与启动文件一样,只需修改端口与server-id即可完成配置

授权目录并增加启动文件可执行权限

[root@~]#chown -r mysql.mysql /data
[root@~]#find /data -name mysql -exex chmod +x {} \;

启动数据库

[root@~]#/data/3306/mysql start
[root@~]#/data/3307/mysql start

修改默认数据库密码

[root@~]#mysqladmin -uroot password '123456' -s /data/3306/mysql.sock
[root@~]#mysqladmin -uroot password '123456' -s /data/3307/mysql.sock

测试登陆,可以登陆两个数据库即可完成全部安装过程

配置主库

1)备份主库

mkdir /backup

登陆主库创建用步同户并授权

[root@~]#mysql -uroot -p123456 -s /data/3306/mysql.sock

mysql> grant replication slave on *.* to rep@'192.168.1.%' identified by'123456';

query ok, 0 rows affected (0.00 sec)

mysql> flush privileges;

query ok, 0 rows affected (0.00 sec)

执行锁表操作

[root@~]#/application/mysql/bin/mysql -uroot -p123456 -s /data/3306/mysql.sock -e "flush table with read lock;"

备份主库

[root@~]#/application/mysql/bin/mysql -uroot -p123456 -s /data/3306/mysql.sock -e "show master status;" >/backup/mysql.log

[root@~]#/application/mysql/bin/mysqldump -uroot -p123456 -s /data/3306/mysql.sock -a -b |gzip >/backup/mysql.sql.gz

解除锁表状态

[root@~]#/application/mysql/bin/mysql -uroot -p123456 -s /data/3306/mysql.sock -e "unlock tables;" 

备注:以上操作也可以登陆主库进行,但是需要注意的是,执行锁表操作后,需要另开启一个窗口进行数据备份,不可直接退出,防止有数据写入导致备份的数据不完整。最好是使用非交互式操作。

配置从库实现主从同步

将主库的备份文件解压并恢复数据库

[root@backup ]#gzip -d mysql.sql.gz

[root@backup ]#/application/mysql/bin/mysql -uroot -p123456 -s /data/3307/mysql.sock < mysql.sql

查看log日志

[root@backup ]#cat mysql.log
+------------------+----------+--------------+------------------+
| file    | position | binlog_do_db | binlog_ignore_db |
+------------------+----------+--------------+------------------+
| mysql-bin.000002 |  424 |    |     |
+------------------+----------+--------------+------------------+

登陆从库执行下面的操作

mysql> change master to
 -> master_host='192.168.1.7',  #服务器ip
 -> master_port=3306,    #主库端口
 -> master_user='rep',    #同步的用户
 -> master_password='123456',  #同步的用户密码
 -> master_log_file=' mysql-bin.000002', #binlog文件
 -> master_log_pos=424;     #位置点
mysql> start slave;    #开启同步

等待60s后查看同步状态

[root@backup ]# mysql -s /data/3307/mysql.sock -e "show slave status\g"|egrep "seconds_behind_master|_running"
   slave_io_running: yes
   slave_sql_running: yes
   seconds_behind_master: 0

只要出现上述情况说明主从同步成功

测试主从同步

主库创建一个数据库

[root@backup ~]# mysql -s /data/3306/mysql.sock -e "create database tongbuku"

[root@backup ~]# mysql -s /data/3306/mysql.sock -e "show databases"
+-----------------------------+
| database     |
+-----------------------------+
| information_schema   |
| mysql      |
| performance_schema   |
| test      |
| tongbuku     |
+-----------------------------+

查看从库同步情况

[root@backup ~]# mysql -s /data/3307/mysql.sock -e "show databases"
+-----------------------------+
| database     |
+-----------------------------+
| information_schema   |
| mysql      |
| performance_schema   |
| test      |
| tongbuku     |
+-----------------------------+

表明主从同步状态正常,也可以在主库新的数据表中创建表,再插入新的数据来测试主从同步状态