1安装MySQL
新的centos系统安装mysql依赖:
- perl perl-Module-Build
- autoconf
- libaio
- namuctl-libs
- net-tools
# yum install -y perl perl-Module-Build net-tools autoconf libaio numactl-libs
Mysql 安装完毕后密码文件会在.mysql_secret
启动mysql
# systemctl start mysql
# mysql -uroot -p密码
设置mysql初始密码:
mysql> set password = password(‘你的密码’)
远程登陆授权
mysql> grant all privileges on *.* to 'root'@'%' identified by 'abcd_123' with grant option;
mysql> flush privileges;
二进制日志+中继日志
Binlog(二进制日志,阿里otter/canal)
MySQL日志文件,里面记录DDL和DML(查询语句除外)
Relay-log(中继日志)
主要用于同步的中间过程。
mysql配置文件my.cnf
[client]
port = 3306
socket = /var/lib/mysql/mysql.sock
[mysqld]
server-id = 2 #无论主机还是从机server-id不能相同
port = 3306 #数据库端口
datadir = /var/lib/mysql
socket = /var/lib/mysql/mysql.sock
default-storage-engine = InnoDB
log-bin = mysql-bin #二进制日志名称
log-bin-index = mysql-bin.index #二进制日志索引
relay-log = mysql-relay #中继日志名称
relay-log-index = mysql-relay.index #索引
expire-logs-days = 10
max-binlog-size = 100M
max_binlog_cache_size = 8M
log-slave-updates = 1
binlog_cache_size = 4M
# use MIXED binlog
binlog_format = MIXED
#binlog_format = ROW
#replicate-do-db = db%.%
#replicate-ignore-db = mysql.%
# ignore tables
replicate-wild-ignore-table = mysql.%
sync_binlog = 1
relay_log_recovery = 1
log_slave_updates = 1
skip-name-resolve
sql_mode=STRICT_TRANS_TABLES
[mysqldump]
quick
max_allowed_packet = 32M
查询mysql用户列表
mysql> use mysql
mysql> select user,password,host from user;
+------+-------------------------------------------+--------------+
| user | password | host |
+------+-------------------------------------------+--------------+
| root | *1D7781F716B5315E62B337DE530D7B74CFEB52D3 | localhost |
| root | | 0dd980a68cce |
| root | | 127.0.0.1 |
| root | | ::1 |
| | | localhost |
| | | 0dd980a68cce |
| root | *1D7781F716B5315E62B337DE530D7B74CFEB52D3 | % |
+------+-------------------------------------------+--------------+
7 rows in set (0.00 sec)
主从复制过程(在主机上操作):
1复制my.cnf
cp /usr/share/mysql/my-default.cnf /etc/my.cnf
2创建同步复制的用户 %为通配符
mysql> create user 'repl'@'172.17.0.%' identified by 'abcd_123';
3给同步复制用户赋权
mysql> grant replication slave on *.* to 'repl'@'172.17.0.%' identified by 'abcd_123';
mysql> flush privileges;
开启binlog
配置时候注意几个坑:
Replication-do-db的坑,如果多个库则使用多行Replication-do-db进行配置
Replication-ignore-db的坑,如果忽略多个库则使用多行Replication-ignore-db进行配置
重启mysql
#sysctemctl restart mysql
#service mysql restart
查看日志事件
mysql> show binlog events;
+------------------+-----+-------------+-----------+-------------+---------------------------------------+
| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |
+------------------+-----+-------------+-----------+-------------+---------------------------------------+
| mysql-bin.000001 | 4 | Format_desc | 2 | 120 | Server ver: 5.6.41-log, Binlog ver: 4 |
+------------------+-----+-------------+-----------+-------------+---------------------------------------+
1 row in set (0.00 sec)
或者
mysql> show binlog events in 'mysql-bin.000001' from 4;
mysql> show binlog events in 'mysql-bin.000001' from 4\G;
从机操作
1复制my.cnf 修改server-id,重启mysql
2主从复制的最关键语句:
Stop slave;
Change master to
Master_host='172.17.0.2',
Master_user='repl',
Master_password='abcd_123',
Master_log_file='mysql-bin.000001',
Master_log_pos=4;
Start slave;
查看连接状态
mysql> show slave status\G;
主机
mysql> show processlist;
执行
# netstat -natp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp6 0 0 :::3306 :::* LISTEN 900/mysqld
tcp6 0 0 172.17.0.2:3306 172.17.0.3:41160 ESTABLISHED 900/mysqld
关于主主复制:
Ip 172.17.0.2 master1
Ip 172.17.0.3 master2
先要修改2台机器/etc/my.cnf
Master1上执行
创建同步复制的用户
mysql> create user 'repl'@'172.17.0.%' identified by 'abcd_123';
给同步复制用户赋权
mysql> grant replication slave on *.* to 'repl'@'172.17.0.%' identified by 'abcd_123';
mysql> flush privileges;
开启binlog
配置时候注意几个坑:
Replication-do-db的坑,如果多个库则使用多行Replication-do-db进行配置
Replication-ignore-db的坑,如果忽略多个库则使用多行Replication-ignore-db进行配置
重启mysql
#sysctemctl restart mysql
Master2上执行
主从复制的最关键语句:
Stop slave;
Change master to
Master_host=’172.17.0.2’,
Master_user=’repl’,
Master_password=’abcd_123’,
Master_log_file=’mysql-bin.000001’,
Master_log_pos=120;
Start slave;
Master2上执行
创建同步复制的用户
mysql> create user 'repl'@'172.17.0.%' identified by 'abcd_123';
给同步复制用户赋权
mysql> grant replication slave on *.* to 'repl'@'172.17.0.%' identified by 'abcd_123';
mysql> flush privileges;
开启binlog
配置时候注意几个坑:
Replication-do-db的坑,如果多个库则使用多行Replication-do-db进行配置
Replication-ignore-db的坑,如果忽略多个库则使用多行Replication-ignore-db进行配置
重启mysql
#sysctemctl restart mysql
Master1上执行
Stop slave;
Change master to
Master_host=’172.17.0.3’,
Master_user=’repl’,
Master_password=’abcd_123’,
Master_log_file=’mysql-bin.000001’,
Master_log_pos=120;
Start slave;
查看主机状态命令:
mysql> show master status \G;
查看从机状态命令:
mysql> show slave status \G;
查看mysql线程列表:
mysql> processlist;
查看二进制日志:
mysql> show binlog events in ‘日志文件’ from 位置;
查看中继日志:
mysql> show relaylog events in ‘日志文件’ from 位置;
使用mysql工具查看日志文件
# mysqlbinlog --start-position=位置 日志路径
# mysqlbinlog -start-position=120 /var/lib/mysql/mysql-bin.000001