1. Yum 安装:
# wget http://repo.mysql.com/mysql57-community-release-el6.rpm
# yum install -y mysql57-community-release-el6.rpm
# yum search mysql #找到mysql57-community-server
# yum install mysql57-community-server
2. 主从机上安装mysql后更新root密码:
grep 'temporary password' /var/log/mysqld.log
[root@localhost ~]# mysql -uroot -p
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Baichi123!';
3. 常用linux服务管理:
# 启动
service mysqld start
# 停止
service mysqld stop
# 重启
service mysqld restart
# 重新加载,但不是my.cnf的配置文件
service mysqld reload
4. MySQL 主从(Master-Slave)同步
(1)原理:
(2)配置方法
资源:
Master: 10.224.162.163 密码:root Baichi123!
Slave: 10.224.162.62
配置步骤:
i. Master配置bin-log,server-id
# vi /etc/my.cnf
log-bin=mysql-bin
server-id=163
ii. Slave配置bin-log, server-id
# vi /etc/my.cnf
log-bin=mysql-bin
server-id=62
iii. Master上配置slave账号权限
GRANT REPLICATION SLAVE ON *.* to 'sync'@'%' IDENTIFIED BY 'Baichi123!';
#登录mysql,查询master状态
show master status
#结果
File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 681 | | | |
iv. Slave上配置Master主机信息,和读取binlog信息
mysql> CHANGE master to master_host='10.224.162.163', master_user='sync',master_password='Baichi123!',master_log_file='mysql-bin.000002',master_log_pos=681;
# 让slave机制生效
Mysql>start slave;
#登录mysql,查询slave状态
mysql> show slave status;
以上步骤,主从数据库配置完成
(3)主从测试
步骤:
a. Master机建立数据库,在库中插入记录
CREATE DATABASE du;
CREATE TABLE component(id int(32), name char(32));
insert into component values(1, 'mmp');
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| du |
| mysql |
b. Slave机查询
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| du |
| mysql |
mysql> SELECT * from component;
+------+------+
| id | name |
+------+------+
| 1 | mmp |
+------+------+
1 row in set (0.00 sec)
(4)完成后续: 可以通过检查slave的Slave_IO_Running | Slave_SQL_Running进程查看是否有主从问题
(5)注意点:slave服务器中执行sql,master不会同步
https://dev.mysql.com/doc/refman/5.6/en/replication-gtids.html
5. Master-Master 双主配置:
(1)步骤:
a. 完全执行Master-Slave配置
b. 然后继续反过来继续执行iii, vi步骤, 完成双主配置
(2)注意:
a. 双主双写,由于同步时间间隔造成生成相同ID的数据