Home Database Mysql Tutorial MySQL基于SSL协议进行主从复制的详细操作教程_MySQL

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

May 27, 2016 pm 01:46 PM
mysql ssl copy Tutorial

当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

Copy after login

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

mysql> show variables like '%ssl%'; 
Copy after login

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

Copy after login

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

ssl
Copy after login

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

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

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

Copy after login

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

cd /etc/pki/CA
Copy after login

生成私钥

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

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

vim /etc/pki/tls/openssh.cnf
Copy after login

 [ 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
Copy after login

生成自签证书

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

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

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

vim /etc/pki/tls/openssl.cnf
Copy after login

 [ 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  #证书中心私钥文件
Copy after login

创建证书编号

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

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

创建私钥

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

生成证书申请

openssl req -new -key master.key -out master.csr
Copy after login

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

openssl ca -in master.csr -out master.crt -days 365
Copy after login

3、创建从服务器证书

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

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

opessl ca -in slave.csr -out slave.crt -days 356
Copy after login

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
Copy after login

修改从服务器配置

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
Copy after login

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

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

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

mysql> show master status ;
Copy after login

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

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;
Copy after login

查看状态

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

Copy after login

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

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

Copy after login

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

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

Copy after login

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)

Copy after login

错误2:

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

20151223113020248.png (426×277)

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

20151223113037083.png (695×95)

Statement of this Website
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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

How to use single threaded redis How to use single threaded redis Apr 10, 2025 pm 07:12 PM

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL and SQL: Essential Skills for Developers MySQL and SQL: Essential Skills for Developers Apr 10, 2025 am 09:30 AM

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.

Monitor Redis Droplet with Redis Exporter Service Monitor Redis Droplet with Redis Exporter Service Apr 10, 2025 pm 01:36 PM

Effective monitoring of Redis databases is critical to maintaining optimal performance, identifying potential bottlenecks, and ensuring overall system reliability. Redis Exporter Service is a powerful utility designed to monitor Redis databases using Prometheus. This tutorial will guide you through the complete setup and configuration of Redis Exporter Service, ensuring you seamlessly build monitoring solutions. By studying this tutorial, you will achieve fully operational monitoring settings

See all articles