面试官给下了死命令:三台服务器,搭建一个MySQL主从架构,能不能搞定?
我一看距离下一家面试还有20分钟。
行,10分钟给你搞定,并写好文档。
[root@itlaoxin162 ~]# uname -r 3.10.0-1062.12.1.el7.x86_64 内核是3.10.0 [root@itlaoxin162 ~]# cat /etc/redhat-release CentOS Linux release 7.7.1908 (Core) 系统版本是centos 7.7
master: 192.168.1.162
slave1; 192.168.1.163
slave2: 192.168.1.71
[root@itlaoxin162 ~]# wget https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
[root@itlaoxin162 ~]# rpm -ivh mysql57-community-release-el7-9.noarch.rpm 警告:mysql57-community-release-el7-9.noarch.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY 准备中... ################################# [100%] 正在升级/安装... 1:mysql57-community-release-el7-9 ################################# [100%]
删除旧的MySQL
[root@itlaoxin162 ~]# rpm -qa |grep mysql php-mysql-5.4.16-48.el7.x86_64 mysql57-community-release-el7-9.noarch
[root@itlaoxin162 ~]# rpm -e php-mysql-5.4.16-48.el7.x86_64
[root@itlaoxin162 ~]# yum -y install mysql-community-server
[root@itlaoxin162 ~]# systemctl start mysqld.service [root@itlaoxin162 ~]# systemctl status mysqld.service
[root@itlaoxin162 ~]# mysql -V mysql Ver 14.14 Distrib 5.7.34, for Linux (x86_64) using EditLine wrapper
MySQL
[root@itlaoxin162 ~]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 5 Server version: 5.7.34 MySQL Community Server (GPL) Copyright (c) 2000, 2021, Oracle and/or its affiliates.
修改密码为ITlaoxin123
mysql> alter user 'root'@'localhost' identified by 'ITlaoxin123' ; Query OK, 0 rows affected (0.01 sec)
此时在192.168.1.162服务器上安装MySQL5.7 就算完成了。
同样的,我们我们需要在从服务器192.168.1.63 和192.168.1.71上安装数据库。
安装数据库的方式和master一样,此处省略,请参考master的数据库安装方法。
三台MySQL搭建好之后,需要把时间同步开启,防火墙关闭:
关闭防火墙和selinux [root@itlaoxin-163 ~]# setenforce 0 [root@itlaoxin-163 ~]# systemctl stop firewalld 同步服务器: [root@itlaoxin-163 ~]# /usr/sbin/ntpdate 192.168.1.162 25 Apr 18:50:18 ntpdate[75307]: no server suitable for synchronization found [root@itlaoxin-163 ~]#
[root@itlaoxin162 ~]# tail -n 3 /etc/my.cnf server-id = 11 log-bin=master-bin log-slave-updates=true [root@itlaoxin162 ~]#
[root@itlaoxin162 ~]# systemctl restart mysqld
#### 给服务器授权 mysql> grant replication slave on *.* to 'myslave'@'192.168.1.%' identified by 'ZMedu123456'; Query OK, 0 rows affected, 1 warning (0.01 sec) 刷新 mysql> flush privileges; Query OK, 0 rows affected (0.01 sec) 查看主服务器的状态: mysql> show master status; +-------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +-------------------+----------+--------------+------------------+-------------------+ | master-bin.000001 | 602 | | | | +-------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
file列显示的日志名,position 列显示偏移量
[root@itlaoxin-163 ~]# tail -n 3 /etc/my.cnf server-id = 22 ## 这里的ID不能与主服务器相同 relay-log=relay-log-bin ## 从主服务器上同步日志到本地 relay-log-index=slave-relay-bin.index ## 定义relay-log的位置和名称 [root@itlaoxin-163 ~]#
登陆MySQL
[root@itlaoxin-163 ~]# mysql -uroot -p Enter password:
根据主服务器的结果来更改下面的master_log_file 和master_log_post 的参数
mysql> change master to master_host='192.168.1.162',master_user='myslave',master_password='ZMedu123456',master_log_file='master-bin.000001',master_log_pos=602; Query OK, 0 rows affected, 2 warnings (0.02 sec)
mysql> start slave; Query OK, 0 rows affected (0.35 sec) mysql>
mysql> show slave status \G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.162 Master_User: myslave Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-bin.000001 Read_Master_Log_Pos: 602 Relay_Log_File: relay-log-bin.000002 Relay_Log_Pos: 321 Relay_Master_Log_File: master-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes
192.168.1.62 主服务器
mysql> create database db_test; Query OK, 1 row affected (0.00 sec)
主服务器:
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | db_test | | mysql | | performance_schema | | sys | | test | +--------------------+ 6 rows in set (0.00 sec)
slave1:
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | db_test | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec)
slave 2服务器
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | db_test | | mysql | | performance_schema | | sys | +--------------------+ 5 rows in set (0.00 sec)
可以看到主从同步已经实现了。
十分钟搭建好后,面试官当场蒙了,问怎么做的,于是我把这个技巧告诉了他:
在xshell里只要开启一个建,三台可以一起执行命令:
搭建一台的功夫,三台都安装好了MySQL;
从服务器不管有多少台,在一台操作,所有的命令都会发送到其他服务器同步执行,你只需要看一下是否执行成功就ok
面试官又要亲自送我去地铁站,这次真不去了!