mysql主从备份不加密

本文介绍了两种MySQL主从备份的配置方法,一种不加密,另一种使用SSL进行加密。包括主库和从库的配置步骤,如修改my.cnf文件,创建备份用户,设置权限,以及如何同步和启动从库。
摘要由CSDN通过智能技术生成
方式一:主从备份不加密

主库:1.   [mysqld] #my.cnf 文件
#########
server-id=1 #主库标识
log-bin=mysql-bin #记录日志的方式  mysql 5.6版本
log_slave_updates=1

expire-logs-days=7 #日志存储时间

2.###重启mysql;创建备份的用户

mysql> CREATE USER 'slavebak'@'%' IDENTIFIED BY 'Te7OLYv4ptDrH8eVzh1h8bLhXwTPh2';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'slavebak'@'%' ;
mysql> FLUSH PRIVILEGES;

3.导出要备份的数据库

4,mysql > SHOW MASTER STATUS; #查出主库当前的备份文件和节点

mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000005 | 676 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

从库: 1. [mysqld] #my.cnf 文件
####
server-id=2 #从库标识,不能和主库一样
binlog-do-db=mysql   //备份的数据库名,可以添加多个或者 
binlog-ignore-db=test //不做备份的库

2.重启mysql,导入备份的数据库
3.
mysql> CHANGE MASTER TO MASTER_HOST='192.168.6.98',master_port=3306, MASTER_USER='bak', MASTER_PASSWORD='bak', MASTER_LOG_FILE='mysql-bin.000005', MASTER_LOG_POS=120; ##(LOG_FILE和LOG_POS 要以主库查询出的为准)

4. 启动Slave
mysql> start slave;

(如果需要修改 信息)
5.stop slave 停止slave,reset slave 重置slave;
再次执行  CHANGE 后 start slave


方式二:主从备份加密


主库:1.生成密钥
mkdir -p /usr/local/mysql/sslcert
cd /usr/local/mysql/sslcert
openssl genrsa 2048 > ca-key.pem
openssl req -new -x509 -nodes -days 10000 -key ca-key.pem > ca-cert.pem
openssl req -newkey rsa:2048 -days 10000 -nodes -keyout server-key.pem > server-req.pem
openssl x509 -req -in server-req.pem -days 10000 -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 > server-cert.pem
openssl req -newkey rsa:2048 -days 10000 -nodes -keyout client-key.pem > client-req.pem
openssl x509 -req -in client-req.pem -days 10000 -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 > client-cert.pem

###
openssl rsa -in server-key.pem -out server-key.pem

tar -czvf sslcert.tar.gz sslcert  

2.
[mysqld] #my.cnf 文件
#########
server-id=1 #主库标识
log-bin=mysql-bin #记录日志的方式  mysql 5.6版本
log_slave_updates=1

expire-logs-days=7 #日志存储时间

ssl
ssl-ca=/usr/local/mysql/sslcert/ca-cert.pem
ssl-cert=/usr/local/mysql/sslcert/server-cert.pem
ssl-key=/usr/local/mysql/sslcert/server-key.pem

3.###重启mysql,然后: mysql> show variables like '%ssl%';

+---------------+------------------------------------------+
| Variable_name | Value |
+---------------+------------------------------------------+
| have_openssl | YES |
| have_ssl | YES |
| ssl_ca | /usr/local/mysql/sslcert/ca-cert.pem |
| ssl_capath | |
| ssl_cert | /usr/local/mysql/sslcert/server-cert.pem |
| ssl_cipher | |
| ssl_crl | |
| ssl_crlpath | |
| ssl_key | /usr/local/mysql/sslcert/server-key.pem |
+---------------+------------------------------------------+
9 rows in set (0.00 sec)

mysql> CREATE USER 'slavebak'@'%' IDENTIFIED BY 'Te7OLYv4ptDrH8eVzh1h8bLhXwTPh2';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'slavebak'@'%' REQUIRE SSL;
mysql> FLUSH PRIVILEGES;

4.导出数据库

5.#############
mysql> FLUSH TABLES WITH READ LOCK;

mysql > SHOW MASTER STATUS;

mysql> UNLOCK TABLES;


从库:
 1. [mysqld] #my.cnf 文件
####
server-id=2 #从库标识,不能和主库一样
binlog-do-db=mysql   //备份的数据库名,可以添加多个或者 
binlog-ignore-db=test //不做备份的库

2.重启mysql,导入备份的数据库

3.
mysql> CHANGE MASTER TO MASTER_HOST='120.25.95.207',MASTER_PORT=24306,MASTER_USER='slavebak', MASTER_PASSWORD='Te7OLYv4ptDrH8eVzh1h8bLhXwTPh2', MASTER_LOG_FILE='mysql-bin.000069', MASTER_LOG_POS=917224584, MASTER_SSL=1, MASTER_SSL_CA = '/usr/local/mysql/sslcert/ca-cert.pem', MASTER_SSL_CERT = '/usr/local/mysql/sslcert/client-cert.pem', MASTER_SSL_KEY = '/usr/local/mysql/sslcert/client-key.pem';

4. 启动Slave
mysql> start slave;

(如果需要修改 信息)
5.stop slave 停止slave,reset slave 重置slave;
再次执行  CHANGE 后 start slave




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值