首页 > 数据库 >开启了TDE下的mysql主从部署

开启了TDE下的mysql主从部署

时间:2024-12-30 10:51:42浏览次数:5  
标签:opt 30 keyring TDE mysql mysql57 root 主从

环境:
OS:Centos 7
mysql:5.7.39

 

1.主库开启了TDE

mysql> show variables like '%keyring%';
+--------------------+------------------------------+
| Variable_name      | Value                        |
+--------------------+------------------------------+
| keyring_file_data  | /opt/mysql57/keyring/keyring |
| keyring_operations | ON                           |
+--------------------+------------------------------+
2 rows in set (0.00 sec)

mysql> show plugins;
+----------------------------+----------+--------------------+-----------------+---------+
| Name                       | Status   | Type               | Library         | License |
+----------------------------+----------+--------------------+-----------------+---------+
| keyring_file               | ACTIVE   | KEYRING            | keyring_file.so | GPL     |

 

2.主库创建复制账号
grant replication slave, replication client on *.* to repl@'192.168.1.%' identified by 'mysql';

 

3.主库备份
/opt/xtrabackup247/bin/innobackupex --defaults-file=/opt/mysql57/conf/my.cnf --user=root --socket=/opt/mysql57/data/mysql.sock --password=mysql -P3306 /tmp/xtrabackup_file

备注:秘钥文件是不会备份到备份文件的,备份目录文件如下:

[root@host135 xtrabackup_file]# ls
2024-12-30_09-15-30
[root@host135 2024-12-30_09-15-30]# ls
backup-my.cnf ibdata1 sys xtrabackup_info
db_test mysql xtrabackup_binlog_info xtrabackup_logfile
ib_buffer_pool performance_schema xtrabackup_checkpoints
[root@host135 2024-12-30_09-15-30]#

 

4.拷贝主库的备份文件到目标机器
scp -r 2024-12-30_09-15-30 root@192.168.1.134:/tmp/xtrabackup_file

5.目的库停掉msyql
若目的机器上部署了mysql,需要将其停掉
/opt/mysql57/bin/mysqladmin -h localhost -uroot -pmysql -P3306 -S /opt/mysql57/data/mysql.sock shutdown

清空data目录,我们恢复的时候需要恢复到该目录
[root@localhost mysql57]#cd /opt/mysql57
[root@localhost mysql57]#mv data bakdata
[root@localhost mysql57]#mkdir data

6.恢复
应用日志
/opt/xtrabackup-2.4.7/bin/innobackupex --defaults-file=/opt/mysql57/conf/my.cnf --user=root --apply-log /tmp/xtrabackup_file/2024-12-30_09-15-30
拷贝恢复文件到data目录
/opt/xtrabackup-2.4.7/bin/innobackupex --defaults-file=/opt/mysql57/conf/my.cnf --user=root --copy-back --rsync /tmp/xtrabackup_file/2024-12-30_09-15-30


7.修改权限
chown -R mysql:mysql /opt/mysql57

 

8.目的端不开开启TDE
vi my.cnf
注释如下两行,并删除 /opt/mysql57/keyring 目录下的文件
early-plugin-load=keyring_file.so
keyring_file_data=/opt/mysql57/keyring/keyring

修改为
#early-plugin-load=keyring_file.so
#keyring_file_data=/opt/mysql57/keyring/keyring


[root@localhost conf]# cd /opt/mysql57/keyring
[root@localhost keyring]# ls
bak_keyring keyring my_keyring
[root@localhost keyring]# rm *


9.启动目的库
[root@localhost conf]# /opt/mysql57/bin/mysqld_safe --defaults-file=/opt/mysql57/conf/my.cnf --user=mysql &

10.配置主从
/opt/mysql57/bin/mysql -h localhost -uroot -pmysql -P3306 -S /opt/mysql57/data/mysql.sock

mysql> reset slave;
mysql> reset master;

mysql> set global gtid_purged='a24c0186-c400-11ef-944f-52540051cd25:1,d6696ebd-fb91-11ee-b632-52540051cd25:1-29,f58f88cb-f478-11ed-b257-525400c8dc1f:1-200000';

上面的值可以从 xtrabackup_info 文件中获取:GTID of the last change '74f5f6a5-b1c2-11ed-b523-fa163eb498c0:1-135'
binlog_pos = filename 'binlog.000007', position '554', GTID of the last change 'a24c0186-c400-11ef-944f-52540051cd25:1,d6696ebd-fb91-11ee-b632-52540051cd25:1-29,f58f88cb-f478-11ed-b257-525400c8dc1f:1-200000'

 

mysql> select * from mysql.gtid_executed;
+--------------------------------------+----------------+--------------+
| source_uuid | interval_start | interval_end |
+--------------------------------------+----------------+--------------+
| a24c0186-c400-11ef-944f-52540051cd25 | 1 | 1 |
| d6696ebd-fb91-11ee-b632-52540051cd25 | 1 | 29 |
| f58f88cb-f478-11ed-b257-525400c8dc1f | 1 | 200000 |
+--------------------------------------+----------------+--------------+
3 rows in set (0.00 sec)

查询是否与 xtrabackup_info 记录的一致

 

mysql>change master to master_host='192.168.1.135',
       master_user='repl',
       master_password='mysql',
       master_port=3306,
       master_auto_position=1;

 

启动从库
mysql> start slave;
Query OK, 0 rows affected (0.27 sec)

发现端口错误,修改端口
mysql> stop slave;
mysql> change master to master_port=13306;
mysql> start slave;

 

11.查看目前从库状态

mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.1.135
                  Master_User: repl
                  Master_Port: 13306
                Connect_Retry: 60
              Master_Log_File: binlog.000007
          Read_Master_Log_Pos: 554
               Relay_Log_File: relaylog-binlog.000002
                Relay_Log_Pos: 405
        Relay_Master_Log_File: binlog.000007
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

 

目前主从复制是正常的,此时的从库是没有开启TDE的

mysql> show variables like '%keyring%';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| keyring_operations | ON |
+--------------------+-------+
1 row in set (0.00 sec)


下面我们在主库上对加密的表进行操作,看同步是否正常.

12.主库上对非加密表进行操作
mysql> insert into tb_no_tde(name) values('name11');
Query OK, 1 row affected (0.17 sec)

从库查询
mysql> select * from tb_no_tde where name='name11';
+----+--------+---------------------+---------------------+
| id | name | create_time | update_time |
+----+--------+---------------------+---------------------+
| 11 | name11 | 2024-12-30 09:57:01 | 2024-12-30 09:57:01 |
+----+--------+---------------------+---------------------+
1 row in set (0.00 sec)

可以看到从库是同步过来了.

13.主库上对加密表进行操作
insert into tb_tde(name) values('name11');

查看从库:
mysql> select * from tb_tde where name='name11';
ERROR 3185 (HY000): Can't find master key from keyring, please check in the server log if a keyring plugin is loaded and initialized successfully.

mysql> select * from tb_tde;
ERROR 3185 (HY000): Can't find master key from keyring, please check in the server log if a keyring plugin is loaded and initialized successfully.

从库应用报错误:
Last_Error: Error executing row event: 'Can't find master key from keyring, please check in the server log if a keyring plugin is loaded and initialized successfully.'

说明从库需要开启TDE,使用主库的秘钥

14.停掉从库并开启TDE
停掉从库应用
mysql> stop slave;
Query OK, 0 rows affected (0.01 sec)

关闭数据库
[root@localhost conf]# /opt/mysql57/bin/mysqladmin -h localhost -uroot -pmysql -P3306 -S /opt/mysql57/data/mysql.sock shutdown

启用TDE
vi my.cnf
添加如下两行
early-plugin-load=keyring_file.so
keyring_file_data=/opt/mysql57/keyring/keyring

将主库的秘钥文件拷贝过来(主库上操作),拷贝过来后注意修改权限
scp /opt/mysql57/keyring/keyring root@192.168.1.134:/opt/mysql57/keyring/

chown -R mysql:mysql /opt/mysql57/keyring


15.重新启动从库
/opt/mysql57/bin/mysqld_safe --defaults-file=/opt/mysql57/conf/my.cnf --user=mysql &

启动主从复制
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)


这个时候查询正常了.
mysql> use db_test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select * from tb_tde where name='name11';
+----+--------+---------------------+---------------------+
| id | name | create_time | update_time |
+----+--------+---------------------+---------------------+
| 11 | name11 | 2024-12-30 09:58:46 | 2024-12-30 09:58:46 |
+----+--------+---------------------+---------------------+
1 row in set (0.00 sec)


16.主库更新秘钥
查看主库的秘钥
[root@host135 keyring]# cd /opt/mysql57/keyring
[root@host135 keyring]# ls -al
total 4
drwxrwxr-x 2 mysql mysql 21 Dec 26 16:07 .
drwxrwxr-x 15 mysql mysql 218 Dec 27 11:05 ..
-rw-r----- 1 mysql mysql 155 Dec 26 16:07 keyring

从库的秘钥文件
[root@localhost data]# cd /opt/mysql57/keyring
[root@localhost keyring]# ls -al
total 4
drwxrwxr-x 2 mysql mysql 21 Dec 30 10:04 .
drwxrwxr-x 16 mysql mysql 240 Dec 30 09:21 ..
-rw-r----- 1 mysql mysql 155 Dec 30 10:04 keyring

主库执行如下语句
mysql> alter instance rotate innodb master key;
Query OK, 0 rows affected (0.15 sec)

再次查看主库的秘钥
[root@host135 keyring]# ls -al
total 4
drwxrwxr-x 2 mysql mysql 21 Dec 30 10:39 .
drwxrwxr-x 15 mysql mysql 218 Dec 27 11:05 ..
-rw-r----- 1 mysql mysql 283 Dec 30 10:39 keyring

重新生成了,时间戳发生了改变

从库
[root@localhost keyring]# ls -al
total 4
drwxrwxr-x 2 mysql mysql 21 Dec 30 10:39 .
drwxrwxr-x 16 mysql mysql 240 Dec 30 09:21 ..
-rw-r----- 1 mysql mysql 283 Dec 30 10:39 keyring
从库自动跟新过来了.


结论:
1.开启了TDE的主库,从库也需要开启TDE,而且使用相同的秘钥;
2.主库执行 alter instance rotate innodb master key 从库会自动同步,从库也可以执行该语句,好像不影响主从同步;

 

标签:opt,30,keyring,TDE,mysql,mysql57,root,主从
From: https://www.cnblogs.com/hxlasky/p/18640389

相关文章

  • wx.getDeviceInfo
    Objectwx.getDeviceInfo()基础库2.20.1开始支持,低版本需做兼容处理。小程序插件:支持,需要小程序基础库版本不低于2.21.3微信Windows版:支持微信Mac版:支持微信鸿蒙OS版:支持功能描述获取设备基础信息返回值Object属性类型说明最低版本abistring......
  • 【MySQL】表的约束
    表的约束一、介绍二、空属性1、介绍2、使用场景3、注意事项4、示例三、默认值约束(DEFAULT)四、列描述(COMMENT)1、介绍2、注意事项3、示例五、ZEROFILL1、介绍2、应用场景3、示例六、主键约束(PRIMARYKEY)1、介绍2、语法3、示例七、自动递增约束(AUTO_INCREMENT)1、介绍2、......
  • JAVA连接MYSQL数据库实现查询
    准备驱动(1)查看数据库版本号(2)根据数据库版下载对应版本驱动驱动下载网址:MySQL::DownloadMySQLConnector/J(ArchivedVersions)若没有则选择接近自己版本的低版本。说明:......
  • 【Nginx应用】nginx stream模块代理MySQL
    其实使用开发机器的nginx一样可以代理数据库,从而实现办公网访问数据库。一、stream模块介绍Nginx的TCP/UDP代理功能的模块分为核心模块和辅助模块。核心模块stream需要在编译配置时增加“--with-stream”参数进行编译【但是我测试的时候,我的开发机并不是这么安装的,直接yumiins......
  • 如何正确开启3306端口以允许外部访问MySQL数据库
    问题描述:我正在尝试配置云服务器上的MySQL数据库,使其能够接受来自外部网络的连接请求。但是,当我试图开放3306端口时遇到了困难。请问应该怎样正确地开启这个端口?需要注意哪些事项?解决方案:您好,针对您想要开启3306端口以允许外部访问MySQL数据库的需求,我们整理了一份详细的指南供......
  • 如何解决MySQL数据库导入时出现500错误?
    MySQL数据库在导入过程中出现500错误通常是由于多种原因引起的。以下是详细的排查步骤和解决方案,帮助您顺利导入SQL文件:检查文件大小和服务器配置:如果SQL文件过大,可能会导致超时或内存不足的问题。您可以尝试分批导入较小的SQL文件,或者增加服务器的内存和超时设置。对于较大......
  • emoji在mysql中存储失败的问题(mysql字符集替换)
    mysql字符集需要utf8mb4才能支持emoji存储,因为utf8一个字符最大3字节,但emoji4字节,所以需要升级成真正的UTF-8mysql的uft8最大只有3字节,是因为mysql开发认为3字节足以存储常见字符。后续的uft8mb4(mostbytes4)才是完整的4字节UTF-8修改表及所有字段的默认字符集#修改表及所有......
  • 恢复 MySQL 数据库后小工具无法响应
    在恢复MySQL数据库后,如果遇到小工具无法响应的问题,可能是由于数据库结构或数据完整性受到影响,或者是前端与后端之间的通信出现了问题。以下是详细的排查步骤和解决方案,帮助您恢复小工具的正常功能。确认数据库恢复成功:确保数据库恢复过程顺利完成,所有表和数据均已正确导入......
  • 安装mysql
    vim/etc/my.cnf配置文件如下[mysqld]bind-address=0.0.0.0port=3306#mysql端口号user=rootbasedir=/usr/local/app/mysql#mysql安装目录datadir=/usr/local/app/mysql/data#数据存放目录socket=/tmp/mysql.socklog-error=/usr/local/app/mysql/log/mysqld.log--......
  • mysql下载安装配置以及多版本
    mysql下载安装配置以及多版本1.下载https://downloads.mysql.com/archives/community/点击链接选择ProductVersion(产品版本),操作系统(OperatingSystem)和OSVersion(操作系统版本)点击ZIPArchive行末的Download下载压缩包版本即可,MSIInstaller是安装包版本(一般有压缩包......