1) 准备工作
主从配置至少需要两台机器,每台机器上都需要安装mysql
安装vim
yum -y install vim
安装wget
yum -y install wget
创建数据库目录:
mkdir /home/mysql/log
mkdir /home/mysql/data
2) 下载mysql源安装包
wget http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
安装mysql源
yum localinstall mysql57-community-release-el7-11.noarch.rpm
检查mysql源安装是否正确
yum repolist enabled | grep"mysql"
安装mysql
yum install mysql-community-server
启动mysql
systemctl start mysqld
查看mysql状态
systemctl status mysqld
开机启动mysql
systemctl enable mysqld
systemctl daemon-reload
3)配置my.cnf
vim /etc/my.cnf
[mysqld]
port=3396
lower_case_table_names=1
datadir=/home/mysql/data
socket=/var/lib/mysql/mysql.sock //sock及pid目录为默认安装目录
pid-file=/var/run/mysqld/mysqld.pid
symbolic-links=0
log-error=/home/log/mysqld.log
character_set_server=utf8
init_connect='SET NAMES utf8'
max_connections=1000
修改目录权限
chown-R mysql:mysql /home/mysql/log/
chown-R mysql:mysql /home/mysql/data/
关闭SElinux
/usr/sbin/setenforce 0
或者:
vim /etc/selinux/config
将SELINUX=enforcing改为SELINUX=disabled
更改后需要重启服务器
重启mysql服务:
servicemysqld start
查询默认密码:
grep'temporary password' /home/mysql/log/mysqld.log
修改root密码
mysql> set password for 'root'@'localhost'=password('xxx123');
允许root远程登录:
mysql> grantall privileges on *.* to 'root' @'%' identified by 'xxx123';
mysql> flushprivileges;
4) 主从配置
master上创建主从复制账户
mysql> CREATEUSER 'replication'@'10.0.0.xxx' IDENTIFIED BY 'xxx123';
mysql> GRANT REPLICATION SLAVE ON *.* TO 'replication'@'10.0.0.xxx';
开启bin-log,设置server-id
[mysqld]
log-bin=mysql-bin
server-id=1�
slave 配置
停止mysql服务编辑配置文件:
service mysqld stop;
vim /etc/my.cnf
[mysqld]
server-id=2
启动mysql :
service mysqld start;
5)配置主从通信
查看master 状态:
要设置从库与主库进行通信,进行复制,使用必要的连接信息配置从库在从库上执行以下语句 :
mysql> CHANGE MASTER TO MASTER_HOST='10.0.0.xxx', MASTER_PORT=3396, MASTER_USER='replication', MASTER_PASSWORD='xxx123', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=779;
启动从服务器复制线程:
mysql> START SLAVE;
查看复制状态
mysql> show slave status\G
Slave_IO_State #从站的当前状态
Slave_IO_Running: Yes #读取主程序二进制日志的I/O线程是否正在运行Slave_SQL_Running: Yes #执行读取主服务器中二进制日志事件的SQL线程是否正在运行。与I/O线程一样Seconds_Behind_Master #是否为0,0就是已经同步了
测试主从复制:
在master上创建数据库:
mysql> CREATE DATABASE `test` DEFAULTCHARACTER SET utf8 COLLATE utf8_general_ci;
在slave 上查看是否同步:
show databases;
网友评论