mysql主从搭建(二进制脚本安装)

Posted 小怪獣55

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql主从搭建(二进制脚本安装)相关的知识,希望对你有一定的参考价值。

1.二进制方式脚本安装mysql

1.1.准备文件

配置文件:my.cnf

安装包:mysql-5.6.42-linux-glibc2.12-x86_64.tar.gz

安装脚本:mysql-install.sh

1.2.my.cnf

[mysqld]
socket=/data/mysql/mysql.sock
user=mysql
symbolic-links=0
datadir=/data/mysql
innodb_file_per_table=1
max_connections=10000

[client]
port=3306
socket=/var/lib/mysql/mysql.sock

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/tmp/mysql.sock

1.3.mysql-install.sh

#!/bin/bash
DIR=`pwd`
NAME="mysql-5.6.42-linux-glibc2.12-x86_64.tar.gz"
FULL_NAME=$DIR/$NAME
DATA_DIR="/data/mysql"
apt-get install libaio* -y

if [ -f $FULL_NAME ];then
echo "安装文件存在"
else
echo "安装文件不存在"
exit 3
fi
if [ -h /usr/local/mysql ];then
echo "Mysql 已经安装"
exit 3
else
tar xvf $FULL_NAME -C /usr/local/src
ln -sv /usr/local/src/mysql-5.6.42-linux-glibc2.12-x86_64 /usr/local/mysql
if id mysql;then
echo "mysql 用户已经存在,跳过创建用户过程"
else
useradd mysql -s /sbin/nologin
fi

if id mysql;then
chown -R mysql.mysql /usr/local/mysql/* -R
if [ ! -d /data/mysql ];then
mkdir -pv /data/mysql /var/lib/mysql && chown -R mysql.mysql /data/mysql -R
/usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/data/mysql --basedir=/usr/local/mysql/
cp /usr/local/src/mysql-5.6.42-linux-glibc2.12-x86_64/support-files/mysql.server /etc/init.d/mysqld
chmod a+x /etc/init.d/mysqld
cp $DIR/my.cnf /etc/my.cnf
ln -sv /usr/local/mysql/bin/mysql /usr/bin/mysql
ln -sv /data/mysql/mysql.sock /var/lib/mysql/mysql.sock
/etc/init.d/mysqld start
else
echo "MySQL数据目录已经存在,"
exit 3
fi
fi
fi

2.主数据库配置

2.1.配置文件修改

vim /etc/my.cnf
-------------------------------------
server_id=53
log_bin=/data/logbin/mysql_bin_log #注意/data/logbin/目录权限,改为mysql
-------------------------------------

systemctl daemon-reload

2.2.授权及用户创建

mysql> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO rsync-user@192.168.47.% IDENTIFIED BY 123456;

2.3.测试

#创建一个库测试
create database taowenwu;

2.4.查看二进制Position

mysql> show master status;
+----------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------------+----------+--------------+------------------+-------------------+
| mysql_bin_log.000001 | 309 | | | |
+----------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

2.5.备份

/usr/local/mysql/bin/mysqldump --all-databases --single_transaction --flush-logs --master-data=2 --lock-tables > /data/backup.sql
scp /data/backup.sql 192.168.47.54:/data/

3.从数据库配置

3.1.配置文件修改

vim /etc/my.cnf
-------------------------------------
server_id=54
-------------------------------------

3.2.数据还原

root@ubuntu:/data# mysql <  /data/backup.sql

3.3.使用有复制权限的用户账号连接至主服务器,并启动复制线程

CHANGE MASTER TO MASTER_HOST=192.168.47.53, \\
MASTER_USER=rsync-user,MASTER_PASSWORD=123456, \\
MASTER_LOG_FILE=mysql_bin_log.000001,MASTER_LOG_POS=309;

#启动
START SLAVE

3.4.查看

mysql> show slave status\\G;

mysql主从搭建(二进制脚本安装)_mysql主从搭建(二进制脚本安装)

以上是关于mysql主从搭建(二进制脚本安装)的主要内容,如果未能解决你的问题,请参考以下文章

mycat系列一基于 Docker 搭建 MySQL 主从复制的详细教程

搭建mysql主从并编写监控主从状态脚本

mysql 5.7 主从复制搭建及原理

centos 7.4 二进制搭建mysql5.7 主从

mysql集群搭建docker-compose方案

如何在一台windows主机上搭建mysql主从复制