1、安装 mysql
shell>apt-get install mysql-server |
2、配置 root 用户远程登录,将 /etc/mysql/mysql.conf.d/mysqld.cnf 中 bind-address = 127.0.0.1 注释掉,然后执行下面语句给 root 授权,允许 root 用户通过任何 IP 和密码“root”远程登录
mysql>grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option;
mysql>flush privieges; # 刷新授权信息
|
3、修改 master(192.168.49.130) 配置文件 /etc/mysql/mysql.conf.d/mysqld.cnf,在文件的最下方加上下面的配置
server_id=1 # 全局唯一的
log-bin=master-mysql-bin # 开启logbin功能并设置logbin文件的名称
binlog_ignore_db=mysql # 复制过滤,不同步mysql系统自带的数据库
binlog_cache_size=1M
binlog_format=mixed # 混合型复制模式,默认采用基于语句的复制,一旦发现基于语句的无法精确的复制时,就会采用基于行的复制。
|
4、接下来重启mysql服务并用客户端登录
shell>service mysql restart |
5、slave想要同步master上的数据首先肯定需要权限,所以我们要在master上面开通权限,允许 slave 使用 root 用户和密码“root”登录
mysql>grant replication slave, replication client on *.* to 'root'@'*' identified by 'root';
mysql>flush privieges; # 刷新授权信息
|
6、查看 master 状态,show master status,关于对数据库修改的sql全部记录在了 bin-log 里面,那么我们就每次都把bin-log里面的sql全部执行一遍?肯定不是,我们肯定要记录我上次复制到哪儿里了,然后我下次再从这个点开始同步,就像我们玩单机游戏闯了3关以后要存下档一样,下次玩我们继续从第三关开始玩,这是一个意思。
mysql>show master status\G;
+-------------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------------+----------+--------------+------------------+-------------------+
| master-mysql-bin.000004 | 154 | | mysql | |
+-------------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
|
7、修改 slave(192.168.49.131) 配置文件 /etc/mysql/mysql.conf.d/mysqld.cnf,在文件的最下方加上下面的配置
server_id=2 # 全局唯一的
log-bin=slave-mysql-bin # 开启了二进制日志,实际上不开也没关系,因为我们这个slave只是做slave,如果你这台slave还有可能要做别人的master的话那么必须开启
binlog_ignore_db=mysql
binlog_cache_size=1M
binlog_format=mixed
relay_log=mysql-relay-bin # 配置中继日志,用来存放从master的bin-log那边同步来的数据
log_slave_updates=1
read_only=1
|
8、重启 slave 服务
shell>service mysql restart |
9、登录 slave 数据库,执行下面命令连接 master
change master to master_host='192.168.49.130', # master 服务器的 IP
master_user='root', # 登录 master 服务器的用户
master_password='root', # 登录 master 服务器的密码
master_port=3306, # master 服务器的端口
master_log_file='master-mysql-bin.000001', # 通过show master status看到的 File
master_log_pos=154, # 通过show master status看到的position
master_connect_retry=30;
|
10、查看 slave 状态,Slave_IO_Running: No、Slave_SQL_Running: No 说明 slave 还没有启动
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.49.130
Master_User: root
Master_Port: 3306
Connect_Retry: 30
Master_Log_File: master-mysql-bin.000004
Read_Master_Log_Pos: 958
Relay_Log_File: mysql-relay-bin.000006
Relay_Log_Pos: 1131
Relay_Master_Log_File: master-mysql-bin.000004
Slave_IO_Running: No
Slave_SQL_Running: No
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 958
Relay_Log_Space: 1338
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 598381b1-fc1b-11e7-b7b8-000c29625440
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
|
11、启动 slave
start slave; |
12、再次查看 slave 状态,Slave_IO_Running: Yes、Slave_SQL_Running: Yes 说明 slave 启动成功,并等待 master 的
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.49.130
Master_User: root
Master_Port: 3306
Connect_Retry: 30
Master_Log_File: master-mysql-bin.000004
Read_Master_Log_Pos: 958
Relay_Log_File: mysql-relay-bin.000006
Relay_Log_Pos: 1131
Relay_Master_Log_File: master-mysql-bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 958
Relay_Log_Space: 1338
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 598381b1-fc1b-11e7-b7b8-000c29625440
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
|
12、停止 slave
stop slave; |
13、在 master 上面创建数据库并创建表,然后随便插入几条数据,再在 master 机器上查看状态,可以看见 slave 的连接信息
mysql> show processlist\G;
*************************** 1. row ***************************
Id: 9
User: root
Host: localhost
db: test
Command: Query
Time: 0
State: starting
Info: show processlist
*************************** 2. row ***************************
Id: 10
User: root
Host: 192.168.49.131:34626
db: NULL
Command: Binlog Dump
Time: 14
State: Master has sent all binlog to slave; waiting for more updates
Info: NULL
2 rows in set (0.00 sec)
|
14、在 slave 上查看有没有数据,如果有就说明主从复制搭建成功,这种模式适合在中小规模的集群下运行。对性能要求高的可以参考别的方案。