实验环境:
两台虚拟机:node1:172.16.103.2 使用编译方式安装的mariadb-10.0.13 作为主服务器使用
node2:172.16.103.3 同样适用编译安装mariadb-10.0.13 作为从服务器使用
实验过程:
一、编译安装mariadb:
由于之前使用过的二进制格式的mariadb不支持ssl功能,需要编译安装mariadb:
1、准备工作,解决依赖关系,创建数据库使用的数据目录,创建用户:
# yum -y groupinstall "Development tools"# yum -y groupinstall "Server Platform Development"
# useradd -r mysql# mkdir -pv /mydata/data# chown -R mysql.mysql /mydata/data
2、安装编译使用的工具软件
# yum install -y cmake # tar xf mariadb-10.0.13.tar.gz# cd mariadb-10.0.13.tar.gz# cmake . -DMYSQL_DATA=/mydata/data -DWITH_SSL=system #编译时要添加上SSL功能# make && make install
二、在主服务器节点上创建私有CA,并为该主从节点的mysql服务器生成证书,过程如下:
1、创建私有CA
# cd /etc/pki/CA# (umask 077;openssl genrsa -out private/cakey.pem 1024)# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 3000# touch index.txt# echo 01 > serial
2、为主节点的mysql生成证书及秘钥文件,由于主节点和CA服务器是一台主机,所以就直接在本机上为主节点的证书请求文件进行了签署:
# cd /usr/local/mysql# mkdir ssl# cd ssl# (umask 077;openssl genrsa -out mysql.key 1024)# openssl req -new -key mysql.key -out mysql.csr# openssl ca -in mysql.csr -out mysql.crt -days 1000# cp /etc/pki/CA/cacert.pem ./
主节点的秘钥文件及证书文件等准备完毕,在该目录下连同CA服务器的证书一共有四个文件:
[root@node1 ssl]# lscacert.pem mysql.csr mysql.crt mysql.key
这几个文件的属主属组需要改变为mysql用户mysql组,便于mysql服务器使用,如果不修改文件的属主数组的话,连接到数据库中以后如果查看have_ssl参数会显示为DISABLED,所以是必须要调整的:
# chown -R mysql.mysql ./
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE '%ssl%';+---------------+---------------------------------+| Variable_name | Value |+---------------+---------------------------------+| have_openssl | YES || have_ssl | DISABLED || ssl_ca | /usr/local/mysql/ssl/cacert.pem || ssl_capath | || ssl_cert | /usr/local/mysql/ssl/mysql.crt || ssl_cipher | || ssl_crl | || ssl_crlpath | || ssl_key | /usr/local/mysql/ssl/mysql.key |+---------------+---------------------------------+
3、为从节点生成秘钥文件及签署请求文件,并由CA服务器签署生成证书文件:
在node2上的操作:
# cd /usr/local/mysql# mkdir ssl# cd ssl# (umask 077;openssl genrsa -out mysql.key 1024)# openssl req -new -key mysql.key -out mysql.csr# scp mysql.csr node1:/tmp
在node1上为node2生成的mysql.csr文件生成证书:
# cd /tmp# openssl ca -in mysql.csr -out mysql.crt -days 1000# scp mysql.crt node2:/usr/local/mysql/ssl/# scp /etc/pki/CA/cacert.pem node2:/usr/local/mysql/ssl/
同样,也在node2上将这几个证书相关的文件属主属组都修改为mysql
# chown -R mysql.mysql /usr/local/mysql/ssl/*
三、前面的编译和秘钥及证书准备工作完成后,现在可以配置数据库了,两个节点上的数据库配置基本相同,唯一要注意的不同之处是server-id,不能相同,否则在启动从节点的IO及mysql线程后会报错,提示server-id有冲突的:
# cd /usr/local/mysql# cp support-files/mysql.server /etc/rc.d/init.d/mysqld# chmod +x /etc/rc.d/init.d/mysqld# mkdir /etc/mysql# cp support-files/my-large.cnf /etc/mysql/my.cnf
编辑配置文件添加数据目录的路径及启用ssl功能,在mysqld段中添加如下几行内容:
# vim /etc/mysql/my.cnfdatadir = /mydata/datasslssl_ca = /usr/local/mysql/ssl/cacert.pemssl_cert = /usr/local/mysql/ssl/mysql.crtssl_key = /usr/local/mysql/ssl/mysql.key
在主节点上使用的server-id为:
server-id = 1
从节点上使用的server-id为:
server-id = 2
在主节点node1上先启动mysqld,:
# service mysqld start# /usr/local/mysql/bin/mysql
连接到数据库以后,可以先查看ssl相关的变量以确定在配置文件中的设定是否已经工作:
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE '%ssl%';+---------------+---------------------------------+| Variable_name | Value |+---------------+---------------------------------+| have_openssl | YES || have_ssl | YES || ssl_ca | /usr/local/mysql/ssl/cacert.pem || ssl_capath | || ssl_cert | /usr/local/mysql/ssl/mysql.crt || ssl_cipher | || ssl_crl | || ssl_crlpath | || ssl_key | /usr/local/mysql/ssl/mysql.key |+---------------+---------------------------------+
然后创建用于数据库复制的账号,账号创建时要指定使用ssl的方式来连接:
MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO repluser@'172.16.103.3' IDENTIFIED BY 'replp@ss' REQUIRE SSL;
这个账号在创建完成后,在从节点上连接测试一下:
[root@node2 ~]# /usr/local/mysql/bin/mysql -urepluser -h172.16.103.2 -p Enter password: ERROR 1045 (28000): Access denied for user 'repluser'@'node2.cluster.com' (using password: YES)
我们在授权账号的时候,要求登录时必须使用ssl方式连接,在未使用ssl方式仅是提供了密码的情况下就会报以上的错误信息,说明我们的ssl认证功能正常。
[root@node2 ~]# /usr/local/mysql/bin/mysql -urepluser -h172.16.103.2 --ssl-ca=/usr/local/mysql/ssl/cacert.pem --ssl-cert=/usr/local/mysql/ssl/mysql.crt --ssl-key=/usr/local/mysql/ssl/mysql.key -pEnter password: Welcome to the MariaDB monitor. Commands end with ; or \g.Your MariaDB connection id is 6Server version: 10.0.13-MariaDB-log Source distributionCopyright (c) 2000, 2014, Oracle, SkySQL Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MariaDB [(none)]> \q
如果输入账号密码时使用了ssl功能就可以正常连接。
连接从节点node2的数据库:
# /usr/local/mysql/bin/mysqlMariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='172.16.103.2',MASTER_USER='repluser',MASTER_PASSWORD='replp@ss',MASTER_LOG_FILE='mysql-bin.000004',MASTER_LOG_POS=665,MASTER_SSL=1,MASTER_SSL_CA='/usr/local/mysql/ssl/cacert.pem',MASTER_SSL_CERT='/usr/local/mysql/ssl/mysql.crt',MASTER_SSL_KEY='/usr/local/mysql/ssl/mysql.key';MariaDB [(none)]> START SLAVE;Query OK, 0 rows affected (0.00 sec)MariaDB [(none)]> SHOW SLAVE STATUS\G*************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 172.16.103.2 Master_User: repluser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000006 Read_Master_Log_Pos: 326 Relay_Log_File: node2-relay-bin.000004 Relay_Log_Pos: 613 Relay_Master_Log_File: mysql-bin.000006 Slave_IO_Running: Yes 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: 326 Relay_Log_Space: 1275 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: Yes Master_SSL_CA_File: /usr/local/mysql/ssl/cacert.pem Master_SSL_CA_Path: Master_SSL_Cert: /usr/local/mysql/ssl/mysql.crt Master_SSL_Cipher: Master_SSL_Key: /usr/local/mysql/ssl/mysql.key Seconds_Behind_Master: 0Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 Master_SSL_Crl: /usr/local/mysql/ssl/cacert.pem Master_SSL_Crlpath: Using_Gtid: No Gtid_IO_Pos:
在主节点上创建个数据库,然后在从节点上验证,查看数据是否可以正常同步:
在node1上的操作:
MariaDB [(none)]> CREATE DATABASE tdb;
在node2上查看结果,可以看到tdb数据:
MariaDB [(none)]> SHOW DATABASES;+--------------------+| Database |+--------------------+| information_schema || mysql || performance_schema || tdb || test |+--------------------+