我正在尝试用Mariadb进行主从复制。
在主服务器上,我使用以下内容创建了证书/密钥:
openssl genrsa 2048 > ca-key.pem
openssl req -sha1 -new -x509 -nodes -days 10000 -key ca-key.pem > ca-cert.pem
openssl req -sha1 -newkey rsa:2048 -days 10000 -nodes -keyout server-key.pem > server-req.pem
openssl rsa -in server-key.pem -out server-key.pem
openssl x509 -sha1 -req -in server-req.pem -days 10000 -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 > server-cert.pem
我在/etc/mysql/certs/
中创建一个文件夹,并相应地修改文件/etc/mysql/mariadb.conf.d/50-server.cnf
,添加:
ssl = on
ssl-ca=/etc/mysql/certs/ca-cert.pem
ssl-cert=/etc/mysql/certs/server-cert.pem
ssl-key=/etc/mysql/certs/server-key.pem
server-id = 1
log_bin = /var/log/mysql/mysql-bin.log
log_error = /var/log/mysql/error.log
binlog_do_db = my_db
bind-address = my_ip_server_address
在我为副本服务器创建证书/密钥后:
openssl req -sha1 -newkey rsa:2048 -days 730 -nodes -keyout client-key.pem > client-req.pem
openssl rsa -in client-key.pem -out client-key.pem
openssl x509 -sha1 -req -in client-req.pem -days 730 -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 > client-cert.pem
我将这些文件发送到副本服务器,并使用以下内容配置副本服务器:
[mysqld]
server-id = 2
log_bin = /var/log/mysql/mysql-bin.log
relay-log = /var/log/mysql/mysql-relay-bin.log
binlog_do_db = my_db
还有
[client]
ssl-ca = /etc/mysql/certs/ca.pem
ssl-cert = /etc/mysql/certs/client-cert.pem
ssl-key = /etc/mysql/certs/client-key.pem
在我尝试将从副本服务器连接到主服务器后:mysql -h my_host -u my_user -p
连接成功,如果写入status;
,则得到:
--------------
mysql Ver 15.1 Distrib 10.5.15-MariaDB, for debian-linux-gnu (x86_64) using EditLine wrapper
Connection id: 11828
Current database:
Current user: my_replica_user
SSL: Cipher in use is DHE-RSA-AES256-SHA
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server: MariaDB
Server version: 10.3.34-MariaDB-0+deb10u1-log Debian 10
Protocol version: 10
Connection: my_server via TCP/IP
Server characterset: utf8mb4
Db characterset: utf8mb4
Client characterset: utf8
Conn. characterset: utf8
TCP port: 3306
Uptime: 15 hours 37 min 31 sec
Threads: 12 Questions: 239736 Slow queries: 0 Opens: 813 Flush tables: 1 Open tables: 807 Queries per second avg: 4.261
--------------
所以,我决定从以下几个方面着手:
CHANGE MASTER TO MASTER_HOST='my_host',
MASTER_USER='my_user_replica',
MASTER_PASSWORD='my_user_replica_password',
MASTER_PORT=3306,
MASTER_LOG_FILE='mysql-bin.000019',
MASTER_LOG_POS=670,
MASTER_CONNECT_RETRY=10,
MASTER_SSL_VERIFY_SERVER_CERT=1,
MASTER_SSL=1,
MASTER_SSL_CA='/etc/mysql/certs/ca.pem',
MASTER_SSL_CERT='/etc/mysql/certs/client-cert.pem',
MASTER_SSL_KEY='/etc/mysql/certs/client-key.pem';
"但是“”当我做一个SHOW REPLICA STATUS\G;
“
***************************1. row***************************
Slave_IO_State: Connecting to master
Master_Host: my_host
Master_User: my_user_replica
Master_Port: 3306
Connect_Retry: 10
Master_Log_File: mysql-bin.000019
Read_Master_Log_Pos: 670
Relay_Log_File: mysql-relay-bin.000003
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000019
Slave_IO_Running: Connecting
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 670
Relay_Log_Space: 256
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: Yes
Master_SSL_CA_File: /etc/mysql/certs/ca.pem
Master_SSL_CA_Path:
Master_SSL_Cert: /etc/mysql/certs/client-cert.pem
Master_SSL_Cipher:
Master_SSL_Key: /etc/mysql/certs/client-key.pem
Seconds_Behind_Master: NULL
Master_SSL_Verify_Server_Cert: Yes
Last_IO_Errno: 2026
Last_IO_Error: error connecting to master 'my_user_replica@my_host:3306' - retry-time: 10 maximum-retries: 86400 message: SSL connection error: SSL certificate validation failure
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 0
Master_SSL_Crl: /etc/mysql/certs/ca.pem
Master_SSL_Crlpath:
Using_Gtid: No
Gtid_IO_Pos:
Replicate_Do_Domain_Ids:
Replicate_Ignore_Domain_Ids:
Parallel_Mode: optimistic
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Slave_DDL_Groups: 0
Slave_Non_Transactional_Groups: 0
Slave_Transactional_Groups: 0
你能帮我让它工作吗?为什么我可以远程连接与mysql -h my_host -u my_user_replica -p
和mysql不能与副本?
- 谢谢-谢谢
1条答案
按热度按时间xkrw2x1b1#
您的问题是
MASTER_SSL_VERIFY_SERVER_CERT=1
,您的命令行连接工作正常,因为您没有使用--ssl-verify-server-cert
选项。此选项生效后,它将检查您要连接的主机名(此处为:“my_host”)与证书主题字符串的CN值(CommonName)匹配,或者与证书中的SAN(ServerAlternateNames)条目之一(如果使用了x509扩展)匹配。
我看不到您在生成证书时输入的CN,但我敢打赌它不是“my_host”(或者在您编辑此帖子之前的实际主机名,因为带下划线的名称不是有效的主机名)
因此,您可以选择删除
MASTER_SSL_VERIFY_SERVER_CERT
选项,也可以使用正确的服务器名称作为服务器证书的CN来生成服务器证书