|NO.Z.00093|——————————|^^ 部署 ^^|——|Linux&MySQL集群.V01|——|主从备份|

一、MySQL 安装部署
### --- 技术点:bin-log日志

~~~     开启主服务器的bin-log日志记录功能,将主服务器的bin-log日志传到从服务器,
~~~     从服务器根据日志内容将数据还原到本地。
### --- 主从服务器:
~~~     从服务器主动把主服务器上的数据同步到本地(备份)
~~~     从服务器分摊主服务器的查询压力(负载均衡)

### --- 主主服务器
~~~     均摊写压力

一、mysql主从备份:部署主服务器
### --- 前提条件:安装了 mysql,开启了二进制日志
### --- 环境规划:

~~~     server21:20.20.20.21:主服务器
~~~     server22:20.20.20.22:从服务器
### --- 部署主:server21

[root@server21 ~]# yum install -y mysql mysql-server
[root@server21 ~]# vim /etc/my.cnf  
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-bin=mysql-bin                                   // 启动二进制日志
server-id=21                                        // 服务器的ID号

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid 
[root@server21 ~]# chkconfig mysqld on
[root@server21 ~]# service mysqld start
二、部署从服务器
### --- 部署从:server22

[root@server22 ~]# yum install -y mysql mysql-server
[root@server22 ~]# vim /etc/my.cnf 
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-bin=mysql-bin                                   // 启动二进制日志
server-id=22                                        // 服务器的ID号

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid 
[root@server22 ~]# chkconfig mysqld on 
[root@server22 ~]# service mysqld start 
三、在主服务器上授权,从服务器保存授权信息
### --- 在主服务器授权

[root@server21 ~]# mysql
mysql> grant replication slave on *.* to slave@'20.20.20.22' identified by '123456';
Query OK, 0 rows affected (0.00 sec)                // replication:特殊的授权权限,*.*:所有的库和表
mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 |      257 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
### --- Position:257,在从服务器上确认
### --- 从服务器保存授权信息

[root@server22 ~]# mysql
mysql> change master to
    -> master_user='slave',                         // 超级用户
    -> master_password='123456',                    // 密码
    -> master_host='20.20.20.21',                   // 主服务器地址
    -> master_log_file='mysql-bin.000003',          // 主服务器使用的二进制日志
    -> master_log_pos=257;                          // 当前日志大小和Position数据一致
 Query OK, 0 rows affected (0.06 sec)
### --- 之后再从服务器会产生授权信息文件

[root@server22 ~]# cat /var/lib/mysql/master.info   #master.info为授权信息
15
mysql-bin.000003
257
20.20.20.21
slave
123456
3306
60
0
0
### --- 开启从服务器 start slave,并查看

[root@server22 ~]# mysql
mysql> start slave;                                 // 启动从服务器
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;                         // 查看从服务器的内容
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 20.20.20.21
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 257
               Relay_Log_File: mysqld-relay-bin.000002
                Relay_Log_Pos: 251
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes                  // 线程已启用,两个都为yes即OK;
            Slave_SQL_Running: Yes                  // 线程已启用,两个都为yes即OK;
          Exec_Master_Log_Pos: 257
              Relay_Log_Space: 407
四、验证主从备份数据实现:测试
### --- 从服务器上查看已有数据库

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+
3 rows in set (0.01 sec)
### --- 主服务器上创建abc数据库

mysql> create database abc;
Query OK, 1 row affected (0.00 sec)
### --- 在从服务器上查看主服务器上创建的数据库是否已经生成

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| abc                |
| mysql              |
| test               |
+--------------------+
4 rows in set (0.00 sec)
### --- 主服务器上删除abc数据库

mysql> drop database abc;
Query OK, 0 rows affected (0.01 sec)
### --- 从服务器上查看主服务器是否删除成功,数据库是否已经同步OK

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+
3 rows in set (0.00 sec)

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on   yanqi_vip  阅读(11)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· DeepSeek 开源周回顾「GitHub 热点速览」
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示