一直没有自己配置过mysql复制,这两天空闲一些,在电脑上装了免费的VMWare Workstation Player,然后下载CentOS的最新版,开始配置一下。只记录了用到的命令,权当作流水账吧。
# ip addr // 查看IP地址
# yum searchifconfig
# yum installnet-tools
# yum installvim
# yum installtelnet
# yum installwget
# yum installgpm // 鼠标驱动
# systemctlenable gpm
# systemctlstart gpm
主服务器 : 192.168.5.128/24
从服务器1 : 192.168.5.129/24
从服务器2 : 192.168.5.130/24
主要参考了图书《高性能MySQL》第3版。图书中介绍的是Mysql,但CentOS缺省集成了MariaDB的YUM源,也就懒得换了,直接用MariaDB就好。
# yum installmariadb-server; mariadb-client
# vim/etc/my.cnf.d/server.cnf
[mysqld]
init_connect =’SET collation_connection = utf8_unicode_ci’
init_connect =‘SET NAMES utf8’
character-set-server= utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
# vim/etc/my.cnf.d/client.cnf
[client]
default-character-set= utf8
# vim/etc/my.cnf.d/mysql-client.cnf
[client]
default-character-set= utf8
# systemctlstart mariadb
# systemctlenable mariadb // 系统启动时自动启动
// 初始化mysql 实例
#mysql_secure_installation
// 允许 root 远程登陆
# mysql -uroot-p
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY'123456' ;
mysql> flush privileges ;
// Centos7 防火墙打开端口号
# firewall-cmd--zone=public --add-port=3306/tcp --permanent
# firewall-cmd--reload // 重启firewall
# firewall-cmd--list-ports // 查看已经开放的端口
# firewall-cmd--state // 查看默认防火墙状态
# mysql -uroot-p
mysql> GRANTREPLICATION SLAVE,
-> REPLICATION CLIENT ON *.*
-> TO repl@'192.168.5.%' IDENTIFIEDBY 'password';
# vim/etc/my.cnf.d/server.cnf
log_bin =mysql-bin
server_id = 128 // 直接用IP地址的最后8位
sync_binlog = 1
# vim/etc/my.cnf.d/server.cnf
# SQL 复制 Slave设置
log_bin =mysql-bin
server_id = 129 // 直接用IP地址的最后8位
relay_log =/var/lib/mysql/mysql-relay-bin
log_slave_updates= 1
read_only = 1
# mysql -uroot-p
// 设置如何连接主库
mysql> CHANGEMASTER TO MASTER_HOST='192.168.5.128',
-> MASTER_USER='repl',
-> MASTER_PASSWORD='password',
-> MASTER_LOG_FILE='mysql-bin.000001',
-> MASTER_LOG_POS=0;
// 启动复制
mysql> startslave;
// 查看复制状态
mysql> showslave status \G;
Slave_ IO_Running: Yes
Slave_ SQL_Running: Yes
Seconds_ Behind_Master: 0
// 查看主库的数据库是否已经复制过来
mysql> showdatabases;
mysql> use …;
mysql> showtables;