Linux中详细部署mysql主从

1.主从简介
在现代企业中,数据显得尤为重要,而存储数据的数据库选择又五花八门,但无论是何种数据库,均存在着一种隐患。
想几个问题:
用一台数据库存放数据,若此数据库服务器挂了导致数据丢失怎么办?
业务量大了,数据多了,访问的人多了,一台数据库无法保证服务质量了怎么办?


2.主从作用
实时灾备,用于故障切换
读写分离,提供查询服务
备份,避免影响业务


3.主从形式
Linux中详细部署mysql主从
一主一从
主主复制
一主多从---扩展系统读取的性能,因为读是在从库读取的
多主一从---5.7版本开始支持
联级复制


4.主从复制原理
Linux中详细部署mysql主从

1.主库将所有的写操作记录到binlog日志中并生成一个log dump线程,通过log dump线程将binlog日志传给从库的I/O线程
2.从库生成两个线程,一个I/O线程,一个SQL线程
3.I/O线程去请求主库的binlog,并将得到的binlog日志写入到relay log (中继日志)文件中
4.SQL线程,会读取relay log 文件中的日志,并解析成具体操作,来实现主从的操作一致,达到最终数据一致的目的。


*5.主从复制配置
主从复制配置步骤:
1.确保从数据库与主数据库里的数据一样
2.在主数据库里创建一个同步账号授权给从数据库使用
3.配置主数据库(修改配置文件)
4.配置从数据库(修改配置文件)


环境说明:

数据库角色IP应用与系统版本有无数据
主数据库192.168.209.12Centos7/redhat7/ mysql-5.7无数据
从数据库192.168.209.13Centos7/redhat7/ mysql-5.7无数据

需求:搭建两台mysql服务器,一台作为主服务器192.168.209.12,一台作为从服务器192.168.209.13,主服务器进行写操作,从服务器进行操作。
说明://分别在主从两台服务器上安装mysql-5.7版本

                  ** 主服务器和从服务器上分别都要安装mysql**
********************************mysql安装****************************
//关闭防火墙及selinux
//安装依赖 包
[root@lanzhiyong ~]# yum -y install ncurses-devel openssl-devel openssl cmake mariadb-devel

//创建用户和组
[root@lanzhiyong ~]# cd /usr/src/
[root@lanzhiyong src]# groupadd -r -g 306 mysql
[root@lanzhiyong src]# useradd -M -s /sbin/nologin -g 306 -u 306 mysql

//下载二进制格式的mysql软件包
[root@lanzhiyong src]# wget https://downloads.mysql.com/archives/get/file/mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

//解压软件至/usr/local
[root@lanzhiyong src]# tar xf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz  -C /usr/local/
[root@lanzhiyong src]# cd /usr/local/

//创建软连接
[root@lanzhiyong local]# ln -sv mysql-5.7.22-linux-glibc2.12-x86_64/ mysql

//修改目录/usr/local/mysql的属主属组
[root@lanzhiyong local]# chown -R mysql.mysql /usr/local/mysql
[root@lanzhiyong local]# ll /usr/local/mysql

//添加环境变量
[root@lanzhiyong ~]# ls /usr/local/mysql
[root@lanzhiyong ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@lanzhiyong ~]# source /etc/profile.d/mysql.sh
[root@lanzhiyong ~]# echo $PATH 

//建立数据存放目录
[root@lanzhiyong ~]# mkdir /opt/data
[root@lanzhiyong ~]# chown -R mysql.mysql /opt/data/
[root@lanzhiyong ~]# ll /opt/
总用量 0
drwxr-xr-x. 2 mysql mysql 6 8月  19 13:20 data

//初始化数据库 注意这个命令后会生成临时密码 要记住 jd?ajfrKY4pQ
[root@lanzhiyong ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/

//配置mysql
//软连接
[root@lanzhiyong ~]# ln -sv /usr/local/mysql/include/ /usr/local/include/mysql
[root@lanzhiyong ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@lanzhiyong ~]# ldconfig -v

//生成配置文件
[root@lanzhiyong ~]# cat > /etc/my.cnf << EOF
> [mysqld]
> basedir = /usr/local/mysql
> datadir = /opt/data
> socket = /tmp/mysql.sock
> port = 3306
> pid-file = /opt/data/mysql.pid
> user = mysql
> skip-name-resolve
> EOF

//配置服务启动脚本
[root@lanzhiyong ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@lanzhiyong ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@lanzhiyong ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld

//启动mysql
[root@lanzhiyong ~]# service mysqld start
[root@lanzhiyong ~]# ps -ef |grep mysql
[root@lanzhiyong ~]#  ss -antl
LISTEN      0      80                  :::3306                            :::*     

//修改密码 使用临时密码登入
[root@lanzhiyong ~]# mysql -uroot -p
jd?ajfrKY4pQ 这是以上步骤的临时密码
mysql> set password = password('lanzhiyong');

#############查看主库有哪些库###################3
[root@lanzhiyong ~]# mysql -uroot -p
Enter password:   //上面设置的密码

Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.22 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)

#####################查看从库有哪些库#############
[root@lan ~]# mysql -uroot -p
Enter password:

Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.22 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)

###############现在在主服务上添加四个数据库########
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| lan                |
| lanzhiyong         |
| mysql              |
| performance_schema |
| sys                |
| zhi                |
+--------------------+
7 rows in set (0.00 sec)

//进入lanzhiyong数据库里添加一个lanzhi表
mysql> use lanzhiyong;
Database changed
mysql> show tables;
+----------------------+
| Tables_in_lanzhiyong |
+----------------------+
| lanzhi               |
+----------------------+
1 row in set (0.00 sec)

//查看表的内容
mysql> select * from lanzhi;
+----+------+------+
| id | name | age  |
+----+------+------+
|  1 | lan  |   10 |
|  2 | cs   |   90 |
|  3 | lol  |   66 |
|  4 | pp   |   33 |
+----+------+------+
4 rows in set (0.00 sec)

//全备主库
//先退出之前的数据库,全备主库时需要另外打开一个终端,再开一台192.168.209.12 的服务器,给数据库加上读锁,避免在备份期间与其他人在写入导致数据不一致

   //加上读锁之后只能读不能写
 mysql> flush tables with read lock;
 Query OK, 0 rows affected (0.02 sec)

 //备份主库将备份文件传送到从库
[root@lanzhiyong ~]# mysqldump -uroot -p --all-databases > all.sql

[root@lanzhiyong ~]# scp all.sql root@192.168.209.13:/root/
The authenticity of host '192.168.209.13 (192.168.209.13)' can't be established.
ECDSA key fingerprint is                SHA256:lOImReX4QGLGm5Qibnn4osotw9PoMtSRGLRaK1JAs4w.
ECDSA key fingerprint is    MD5:e4:1a:5f:28:d1:e0:2a:28:50:1a:1e:9c:cd:23:03:9d.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.209.13' (ECDSA) to the   list of known hosts.
root@192.168.209.13's password:
all.sql                                                        100%  784KB      24.4MB/s   00:00

//解除主库的锁表状态 
  mysql> quit
Bye 

//从库 登入先 再读取主库传来的备份
[root@lan ~]# mysql -uroot -p
Enter password:
mysql> source all.sql;
#读取了之后 主库的所有数据在从库上确保一致

//在主数据库里创建一个同步账号授权给从数据库使用
mysql> create user 'repl'@'192.168.209.13' identified by  'repl123';
Query OK, 0 rows affected (0.01 sec)

//在主数据库授权复制给从数据库
mysql> grant replication slave on *.* to 'repl'@'192.168.209.13';
Query OK, 0 rows affected (0.00 sec)

//刷新权限
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

 ##############配置主数据库###################
[root@lanzhiyong ~]# vi /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
log-error=/opt/data/mysql.log    //添加错误日志目录
user = mysql
skip-name-resolve

 #replication
log-bin=mysql_bin     //添加启用binlog日志
server-id=3                //添加数据库服务器唯一标识符,主库的server-id值必须比从库的大
[root@lanzhiyong ~]# service mysqld restart
[root@lanzhiyong ~]# ss -antl
  State      Recv-Q Send-Q   Local Address:Port                  Peer     Address:Port              
LISTEN     0      128                  *:22                               *:*                  
LISTEN     0      100          127.0.0.1:25                               *:*                  
LISTEN     0      128                 :::22                              :::*                  
LISTEN     0      100                ::1:25                              :::*                  
LISTEN     0      80                  :::3306                            :::*  

//查看主库的状态
mysql> show master status;
+------------------+----------+--------------+------------------+---- ---------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |  Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql_bin.000001 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

##################配置从数据库##################
[root@lan ~]# vi /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
log-error=/opt/data/mysql.log      //添加错误日志目录
user = mysql
skip-name-resolve

#replication
server-id=5                //添加从库的唯一标识符,从库的server-id值必须小于主库的值
relay-log=mysql_relay_log       //添加中继日志文件

[root@lan ~]# service mysqld restart
[root@lan ~]# ss -antl
    State      Recv-Q Send-Q   Local Address:Port                  Peer     Address:Port              
LISTEN     0      128                  *:22                               *:*                  
LISTEN     0      100          127.0.0.1:25                               *:*                  
LISTEN     0      128                 :::22                              :::*                  
LISTEN     0      100                ::1:25                              :::*                  
LISTEN     0      80                  :::3306                            :::*     

//从库配置并启动从复制

mysql> change master to master_host='192.168.209.12',
    master_user='repl',
    master_password='repl123',
    master_log_file='mysql_bin.000001',
    master_log_pos=154;   
  Query OK, 0 rows affected, 2 warnings (0.03 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

//查看从服务器状态
mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.209.12
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql_bin.000002
Read_Master_Log_Pos: 154
Relay_Log_File: mysql_relay_log.000003
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql_bin.000002
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: 154
Relay_Log_Space: 740
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: 3
Master_UUID: 3bcb9f5a-b269-11e8-aeca-000c2947a37d
Master_Info_File: /opt/data/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)

#######主库插入记录验证#########

//在主库里的lanzhiyong库里lanzhi表里添加几条记录,验证是否能同步
mysql> select from lanzhi;
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | lan | 10 |
| 2 | cs | 90 |
| 3 | lol | 66 |
| 4 | pp | 33 |
+----+------+------+
4 rows in set (0.01 sec)
mysql> insert into lanzhi (id,name,age) values (5,'AA',23),(6,'BB',45),(7,'CC',100);
Query OK, 3 rows affected (0.03 sec)
Records: 3 Duplicates: 0 Warnings:
mysql> select
from lanzhi;
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | lan | 10 |
| 2 | cs | 90 |
| 3 | lol | 66 |
| 4 | pp | 33 |
| 5 | AA | 23 |
| 6 | BB | 45 |
| 7 | CC | 100 |
+----+------+------+
7 rows in set (0.00 sec)

############从库看是否同步###########3
mysql> select * from lanzhi;
+----+------+------+
| id | name | age |
+----+------+------+
| 1 | lan | 10 |
| 2 | cs | 90 |
| 3 | lol | 66 |
| 4 | pp | 33 |
| 5 | AA | 23 |
| 6 | BB | 45 |
| 7 | CC | 100 |
+----+------+------+
7 rows in set (0.01 sec)

转载于:https://blog.51cto.com/13833047/2171795

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值