search
HomeDatabaseMysql TutorialMySQL基于SSL协议进行主从复制的详细操作教程_MySQL

当mysql跨越互联网进行复制时别人可以窃取到mysql的复制信息,这些信息是明文的,因此存在不安全性,这里通过ssl对复制的信息进行加密。当在客户没有固定ip而要访问服务器时,mysql要允许任意地址的访问,服务端和客户端通过证书验证可以防止暴力破解。

开始之前让我们先来回顾一下SSL协议客户端OpenSSL的安装过程:
安装openssl

mkdir /test/setup
cd /test/setup
tar zxvf openssl-0.9.8b.tar.gz
cd openssl-0.9.8b
./config
make && make install

开启mysql中ssl功能
登录Mysql查看

mysql> show variables like '%ssl%'; 

+---------------+----------+ 
| Variable_name | Value  | 
+---------------+----------+ 
| have_openssl | DISABLED | 
| have_ssl   | DISABLED | 
| ssl_ca    |     | 
| ssl_capath  |     | 
| ssl_cert   |     | 
| ssl_cipher  |     | 
| ssl_key    |     | 
+---------------+----------+

如果mysql输出如上所述,那么继续操作开启ssl;如果不是,重新编译安装mysql,注意生成makefile时填写参数正确。
退出mysql,编辑/etc/my.cnf
在[mysqld]和[mysqldump]之间,加入下列配置信息:

ssl

保存后重新启动mysql,再次登录mysql

mysql -uroot -p
mysql> show variables like '%ssl%'; 

+---------------+-------+ 
| Variable_name | Value | 
+---------------+-------+ 
| have_openssl | YES  | 
| have_ssl   | YES  | 
| ssl_ca    |    | 
| ssl_capath  |    | 
| ssl_cert   |    | 
| ssl_cipher  |    | 
| ssl_key    |    | 
+---------------+-------+

好了,下面进入正题:
mysql基于ssl复制
1、创建证书中心
在主服务器上创建证书中心

cd /etc/pki/CA

生成私钥

(umask 077;openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048)

生成自签证书,由于需要输入大量用户信息,因此编辑证书的配置文件,在私有的CA上创建证书要注意所有的用户信息要和CA中的一致,从国家到部门都要相同,否则会造成证书无法使用

vim /etc/pki/tls/openssh.cnf

 [ req_distinguished_name ]
 countryName     = Country Name (2 letter code)
 countryName_default = CN
 countryName_min   = 2
 countryName_max   = 2
 stateOrProvinceName = State or Province Name (full name)
 stateOrpovinceName_default = FJ
 localityName    = Locality Name (eg,city)
 localityName    = FZ
 O.organizationName = Organization Name (eg,company)
 O.organizationName_default = zdz
 organizationalUnitName   = Organizational Unit Name (eg,section)
 organizationalUnitName_default = zdz

生成自签证书

openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -out /etc/pki/CA/cacert.pem -days 3650

-x509是创建自签证书是需要的参数,在创建其他证书时不能加该参数

由于是自签证书因此要修改证书路径

vim /etc/pki/tls/openssl.cnf

 [ CA_defalut ]
 dir = /etc/pki/CA
 certs = $dir/certs   #存放生成证书的目录
 crl_dir = $dir/crl   #存放吊销证书的目录
 database = $dir/index.txt  #证书的索引文件
 new_certs_dir = $dir_newcerts  #新签的证书目录
 serial = $dir/serial  #序列号
 crl = $dir/crl.pem
 private_key = $dir/private/cakey.pem  #证书中心私钥文件

创建证书编号

mkdir certs crl newcerts
 touch index.txt
 echo 00 > serial

2、为主服务器创建证书
服务器的名称必须固定,在申请证书时要输入服务器名称,证书和服务器名称对应

创建私钥

mkdir /usr/local/mysql/ssl
 cd /usr/local/mysql/ssl
 (umask 077;openssl genrsa -out /usr/local/mysql/ssl/master.key 2048)

生成证书申请

openssl req -new -key master.key -out master.csr

在证书服务器上对master的证书进行签发

openssl ca -in master.csr -out master.crt -days 365

3、创建从服务器证书

(umask 077;openssl genrsa -out /usr/local/mysql/ssl/slave.key 2048)
 openssl req -new -key slave.key -out slave.csr

将从服务器的证书申请文件复制到证书服务器上进行签发

opessl ca -in slave.csr -out slave.crt -days 356

4、修改证书权限和mysql配置文件
将证书的公钥cacert.pem复制到主从服务器的目录下

cd /usr/local/mysql/ssl
 cp /etc/pki/CA/cacert.pem ./
 chown -R mysql:mysql master.crt master.key cacert.pem
 chmod 600 master.crt master.key cacert.pem
 vim /usr/local/mysql/my.cnf
 ssl
 ssl_ca         = /usr/local/mysql/ssl/cacrt.pem
 ssl_cert        = /usr/local/mysql/ssl/master.crt
 ssl_key         = /usr/local/mysql/ssl/master.key

修改从服务器配置

cd /usr/local/mysql/ssl
 cp /etc/pki/CA/cacert.pem ./
 chown -R mysql:mysql slave.crt slave.key cacert.pem
 chmod 600 slave.crt slave.key cacert.pem
 vim /usr/local/mysql/my.cnf
 ssl
 ssl_ca         = /usr/local/mysql/ssl/cacrt.pem
 ssl_cert        = /usr/local/mysql/ssl/slave.crt
 ssl_key         = /usr/local/mysql/ssl/slave.key

5、在主服务器上创建复制用户

grant replication slave on *.* to slave@'192.168.216.133' identified by 'slave' requere ssl;
 flush privileges;

查看主服务器当前二进制位置

mysql> show master status ;

 +-------------------------+------------+---------------------+--------------------------+--------------------------+
 | File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
 +-------------------------+------------+---------------------+--------------------------+--------------------------+
 | mysql-bin.000007 |   1015  |               |                  |                  |
 +-------------------------+------------+---------------------+--------------------------+---------------------------+
 1 row in set (0.00 sec)

6、在从服务器上开始复制

change master to
 master_host='192.168.216.132',
 master_user='slave',
 master_password='slave',
 master_log_file='mysql-bin.000007',
 master_log_pos=1015,
 master_ssl=1,
 master_ssl_ca=' /usr/local/mysql/ssl/cacrt.pem',
 master_ssl_cert='/usr/local/mysql/ssl/slave.crt',
 master_ssl_key='/usr/local/mysql/ssl/slave.key';
 start slave;

查看状态

20151223113001802.png (515×581)

错误1:

如果要确保证书没有问题可以通过建立测试的用户同ssl进行连接在主服务器上开一个权限很大的用户,进行ssl的登录测试

grant all privileges on *.* to root@'192.168.216.133′ identified by ‘root' require ssl;

[root@slave ssl]# mysql -uroot -proot -h192.168.216.133 –ssl-ca=cacrt.pem –ssl-cert=slave.crt –ssl-key=slave.key

Warning: Using a password on the command line interface can be insecure.

ERROR 2026 (HY000): SSL connection error: ASN: before date in the future

这是由于虚拟的时间不正确导致
如果这时候不使用ssl方式进行连接则会报出错误

[root@slave ssl]# mysql -uroot -proot -h192.168.216.133;

Warning: Using a password on the command line interface can be insecure.

ERROR 1045 (28000): Access denied for user ‘root'@'192.168.216.132′ (using password: YES)

错误2:

在配置文件中添加证书配置后执行 show variables like ‘%ssl%'显示

20151223113020248.png (426×277)

这是由于没有将证书的属主改为mysql,可以从日志中得知是无权限获取私钥

20151223113037083.png (695×95)

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Explain the ACID properties (Atomicity, Consistency, Isolation, Durability).Explain the ACID properties (Atomicity, Consistency, Isolation, Durability).Apr 16, 2025 am 12:20 AM

ACID attributes include atomicity, consistency, isolation and durability, and are the cornerstone of database design. 1. Atomicity ensures that the transaction is either completely successful or completely failed. 2. Consistency ensures that the database remains consistent before and after a transaction. 3. Isolation ensures that transactions do not interfere with each other. 4. Persistence ensures that data is permanently saved after transaction submission.

MySQL: Database Management System vs. Programming LanguageMySQL: Database Management System vs. Programming LanguageApr 16, 2025 am 12:19 AM

MySQL is not only a database management system (DBMS) but also closely related to programming languages. 1) As a DBMS, MySQL is used to store, organize and retrieve data, and optimizing indexes can improve query performance. 2) Combining SQL with programming languages, embedded in Python, using ORM tools such as SQLAlchemy can simplify operations. 3) Performance optimization includes indexing, querying, caching, library and table division and transaction management.

MySQL: Managing Data with SQL CommandsMySQL: Managing Data with SQL CommandsApr 16, 2025 am 12:19 AM

MySQL uses SQL commands to manage data. 1. Basic commands include SELECT, INSERT, UPDATE and DELETE. 2. Advanced usage involves JOIN, subquery and aggregate functions. 3. Common errors include syntax, logic and performance issues. 4. Optimization tips include using indexes, avoiding SELECT* and using LIMIT.

MySQL's Purpose: Storing and Managing Data EffectivelyMySQL's Purpose: Storing and Managing Data EffectivelyApr 16, 2025 am 12:16 AM

MySQL is an efficient relational database management system suitable for storing and managing data. Its advantages include high-performance queries, flexible transaction processing and rich data types. In practical applications, MySQL is often used in e-commerce platforms, social networks and content management systems, but attention should be paid to performance optimization, data security and scalability.

SQL and MySQL: Understanding the RelationshipSQL and MySQL: Understanding the RelationshipApr 16, 2025 am 12:14 AM

The relationship between SQL and MySQL is the relationship between standard languages ​​and specific implementations. 1.SQL is a standard language used to manage and operate relational databases, allowing data addition, deletion, modification and query. 2.MySQL is a specific database management system that uses SQL as its operating language and provides efficient data storage and management.

Explain the role of InnoDB redo logs and undo logs.Explain the role of InnoDB redo logs and undo logs.Apr 15, 2025 am 12:16 AM

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?Apr 15, 2025 am 12:15 AM

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

What is the Using temporary status in EXPLAIN and how to avoid it?What is the Using temporary status in EXPLAIN and how to avoid it?Apr 15, 2025 am 12:14 AM

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft