主mysql开启binlog日志,创建同步用户;从机上配置同步用户并指定同步主机的binlog文件及起始位置即可使用同步用户同步主mysql的binlog日志,配置组成mysql主备集群。 主备集群上主机可以读写,但是从机只能读不能写,因为是单向同步。一般主从集群只会使用主Mysql,从机实际上只起到备份数据的作用,如果有读写分离可以分担一部分读的压力。 下面使用两台机器做一主一从的mysql主从集群搭建示例: 一、先分别安装mysql
sudo mkdir -p /opt/MPP
sudo chown -R mysql:mysql /opt/MPP
sudo chmod -R 751 /opt/MPP
tar -zxvf mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz
sudo groupadd mysql
sudo useradd -r -g mysql mysql
id mysql
sudo mkdir -p /data01/MPP/mysql/data
sudo chown -R mysql: mysql /data01/MPP/mysql
sudo chmod -R 775 /data01/MPP/
cd /opt/MPP/mysql-5.7.37-linux-glibc2.12-x86_64
sudo chmod -R 775 /opt/MPP/mysql-5.7.37-linux-glibc2.12-x86_64
cd /opt/MPP/mysql-5.7.37-linux-glibc2.12-x86_64
sudo bin/mysqld --initialize --user=mysql --basedir=/opt/MPP/mysql-5.7.37-linux-glibc2.12-x86_64 --datadir=/data01/MPP/mysql/data
sudo bin/mysql_ssl_rsa_setup --datadir=/data01/MPP/mysql/data
vi /etc/my.cnf
[mysqld]
datadir = /data01/MPP/mysql/data
port = 3306
skip-name-resolve
bind-address=0.0.0.0
lower_case_table_names=1
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
log_timestamps=SYSTEM
max_allowed_packet = 120M
socket =/data01/MPP/mysql/mysql.sock
character-set-server=utf8
default-time_zone = '+8:00'
server_id=1
log-bin=/data12/MPP/mysql/arch/mysql-bin
binlog-format=ROW
expire_logs_days=120
relay_log_purge=off
relay-log = /data12/MPP/mysql/relay/relay-bin
relay_log_info_repository = table
log_slave_updates= 1
log-bin-trust-function-creators=1
max_connections=50000
default-storage-engine = InnoDB
[mysqld_safe]
log-error = /tmp/mysql-error.log
[client]
port = 3306
socket=/data01/MPP/mysql/mysql.sock
[mysqld_safe]
pid-file=/data01/MPP/mysql/mysql93.pid
sudo chmod 771 /data01/MPP/mysql
sudo su - mysql
/opt/MPP/mysql-5.7.37-linux-glibc2.12-x86_64/bin/mysqld_safe --defaults-file=/etc/my.cnf &
/opt/MPP/mysql-5.7.37-linux-glibc2.12-x86_64/bin/mysql -uroot -p
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456' PASSWORD EXPIRE NEVER;
-- 开启远程访问
use mysql;
update mysql.user set host='%' where user='root';
flush privileges;
exit;
cd /opt/MPP/mysql-5.7.37-linux-glibc2.12-x86_64
sudo cp support-files/mysql.server /etc/init.d/mysql
sudo chown mysql:mysql /etc/init.d/mysql
sudo service mysql status
二、主从同步配置
-
主机master配置
CREATE USER 'repl'@'10.37.62.94' IDENTIFIED BY '123456';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'10.37.62.94';
FLUSH PRIVILEGES;
show master status;
-
从机slave配置
从机的my.cnf配置注意
从mysql和主mysql的配置基本相同,除了以下几个配置: #从机server-id要和主机不同 server-id=2; log_slave_updates=1 #开启同步函数 log_bin_trust_function_creators = 1
配置同步
change master to master_host='10.37.62.93', master_user='repl', master_password='123456', master_log_file='mysql-bin.000001', master_log_pos=2920;
start slave;
show slave status;
slave_IO_Running和Slave_SQL_Running都是Yes说明同步成功。
测试
从主机建表插入数据,然后进入从机查看数据是否同步。
|