概念
- 什么是主从复制、所谓主从复制,简单的来说就是把数据多备份到另外一台电脑上
- 那为什么要做主从复制,说起这里我们就要说到单点了
- 那什么是单点,这个问题很抽象、所谓点单比如我们人自己的生命,就是个单点,一出现什么故障那很危险的,比喻下啦,勿怕,结果是什么大家都知道
- 比如我们的手机也是个单点,只要是唯一都是点单,单点是很危险的,这得看在什么情况下,有时候在这个世界上就需要单点、比如女朋友啦等等,哈哈,大家都懂得,但是一定是单点吗,那你就要努力了,呵呵
- 其实在互联网公司单点机房单点是很危险的,因为在互联网公司数据就决定了这个公司的命脉,就像我们忠所周知的什么什么程公司就差点倒闭,幸亏他有数据备份不然公司可能在一夜之间倒闭
- 就像某宝那他强大都知道,你可能还在里面买点小东西送给你什么什么朋友,男女都可以啦
- 但是他的数据一旦丢失,那效果都懂的,说了半天那为什么要做什么复制,什么什么主从
- 所谓主从就是就是为了防止单点,数据一旦丢失,我们还有另外一个备份
- 难道主从就只是为了备份用
- 当然不是,主从用处很多,等下会慢慢来介绍
- 下面我将会用到很多专业术语,看不懂没关系,慢慢来就可以啦,笔者我当然也是非常菜逼的呵呵
主从复制配置
- 首先我们要准备二台linux系统,我这里用的是CentOS7.3
- 下面是导图的顺序
[root@server ~]# ntpdate 172.16.0.1
[root@server ~]# yum install mariadb-server -y
- 安装成功以后,先不要着急启动我们先配置主服务器,执行命令,打开配置文件
[root@server ~]# vim /etc/my.cnf.d/server.cnf
# this is only for the mysqld standalone daemon
[mysqld]
server-id = 1 <<<<< ID号最好是唯一
log-bin = master-log <<<<< 开启二进制日志
skip_name_resolve = ON
- 配置玩以后保存退出,配置从服务器,同样是在下面配置文件配置
[root@server ~]# vim /etc/my.cnf.d/server.cnf
[server]
# this is only for the mysqld standalone daemon
[mysqld]
server-id = 2
relay-log = relay-log
skip_name-resolve = ON
[root@server ~]# systemctl start mariadb.service
[root@server ~]# ss -nutl
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
udp UNCONN 0 0 *:43362 *:*
udp UNCONN 0 0 *:68 *:*
udp UNCONN 0 0 :::28989 :::*
tcp LISTEN 0 50 *:3306 *:*
tcp LISTEN 0 128 *:22 *:*
tcp LISTEN 0 100 127.0.0.1:25 *:*
tcp LISTEN 0 128 :::22 :::*
tcp LISTEN 0 100 ::1:25 :::*
- 启动成功以后我们来连接,注意这里是没有设置账号密码的所以不需要输入,需要输入密码是-和-p
[root@server ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.52-MariaDB MariaDB Server
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
- 连接成功以后我们执行一个命令查询看,日志,因为我要从日志的以后来复制
MariaDB [(none)]> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-log.000003 | 245 | | |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
MariaDB [(none)]> GRANT REPLICATION CLIENT,REPLICATION SLAVE ON *.* TO 'lilin'@'172.16.251.190' IDENTIFIED BY 'li56412820';
MariaDB [(none)]> FLUSH PRIVILEGES;
[root@form ~]# systemctl start mariadb.service
[root@form ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.52-MariaDB MariaDB Server
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='172.16.252.133' , MASTER_USER='lilin',MASTER_PASSWORD='li5641282' , MASTER_LOG_FILE='master-log.000003' , MASTER_LOG_POS=678;
- 执行完毕以后他会在我们的从服务器一个路径里面会生成一个很重要的文件,那就是info文件里面会有你数据库的账号密码
[root@form ~]# cd /var/lib/mysql/
[root@form mysql]# ls
aria_log.00000001 ib_logfile0 mysql relay-log.000001 test
aria_log_control ib_logfile1 mysql.sock relay-log.index
ibdata1 master.info performance_schema relay-log.info
[root@form mysql]# cat master.info
18
master-log.000003
678
172.16.252.133
lilin
li5641282
3306
60
0
0
1800.000
0
- 还有一个文件就是relay-log,里面会保存位置,这里文件作用就是服务器重启了会自动保存自动启动
[root@form mysql]# cat relay-log.info
./relay-log.000001
4
master-log.000003
678
MariaDB [(none)]> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 172.16.252.133
Master_User: lilin
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-log.000003
Read_Master_Log_Pos: 678
Relay_Log_File: relay-log.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: master-log.000003
Slave_IO_Running: No <<<<<复制NO为没有启动
Slave_SQL_Running: No <<<<<重放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: 678
Relay_Log_Space: 245
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: 0
1 row in set (0.00 sec)
mysql> START SLAVE
网友评论