参考文章:http://www.howtoforge.com/how-to-set-up-mysql-database-replication-with-ssl-encryption-on-centos-5.4
外国人写文章真严谨,步骤详尽,讲解明了,我除了安装方式使用了源代码编译,其余部分基本都是照做,操作步骤写在这里,仅做记录。

测试环境准备:

准备两台计算机,一台master,一台slave,配置随便,反正centos只安装字符界面,不需要太好配置,当然配置越低,编译安装速度越慢,如果你找不到计算机,手头只有一台计算机,那么用virtualbox虚拟然后搭网桥一样可以模拟一台局域网计算机,为了学习技术,有条件上,没有条件也要创造条件上!

master服务器
192.168.90.216
centOS 5.3 x86_64
mysql-5.0.67

slave服务器
192.168.90.89
centOS 5.3 x86_64
mysql-5.0.67

编译安装mysql,主从服务器的操作都一样

  1. tar zxvf mysql-5.0.67.tgz
  2. cd mysql-5.0.67
  3. ./configure --prefix=/usr/local/mysql --sysconfdir=/etc --with-openssl --with-vio
  4. make
  5. make install

准备配置文件和启动脚本

  1. cp support-files/my-medium.cnf /etc/my.cnf
  2. cp support-files/mysql.server /etc/rc.d/init.d/mysqld

设置自动启动

  1. chmod 700 /etc/rc.d/init.d/mysqld
  2. chkconfig --add mysqld
  3. chkconfig --level 345 mysqld on

初始化授权表

  1. cd /usr/local/mysql/bin
  2. ./mysql_install_db --user=mysql

启动mysql

  1. service mysqld start

加入环境变量

  1. for i in *; do ln -s /usr/local/mysql/bin/$i /usr/bin/$i; done

给数据库root用户加上密码

  1. mysqladmin -u root password 密码

登录mysql检查

  1. mysql -u root -p
  2. Enter password:
  3. Welcome to the MySQL monitor.  Commands end with ; or \g.
  4. Your MySQL connection id is 2
  5. Server version: 5.0.67-log Source distribution
  6.  
  7. Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
  8.  
  9. mysql> show variables like '%ssl%';
  10. +---------------+----------+
  11. | Variable_name | Value    |
  12. +---------------+----------+
  13. | have_openssl  | DISABLED |
  14. | have_ssl      | DISABLED |
  15. | ssl_ca        |          |
  16. | ssl_capath    |          |
  17. | ssl_cert      |          |
  18. | ssl_cipher    |          |
  19. | ssl_key       |          |
  20. +---------------+----------+
  21. 7 rows in set (0.00 sec)

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

  1. user=mysql
  2. # Default to using old password format for compatibility with mysql 3.x
  3. # clients (those using the mysqlclient10 compatibility package).
  4. old_passwords=1
  5. ssl
  6.  
  7. [mysqld_safe]
  8. log-error=/var/log/mysqld.log
  9. pid-file=/var/run/mysqld/mysqld.pid

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

  1. mysql -u root -p
  2. Enter password:
  3. Welcome to the MySQL monitor.  Commands end with ; or \g.
  4. Your MySQL connection id is 1
  5. Server version: 5.0.67-log Source distribution
  6.  
  7. Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
  8.  
  9. mysql> show variables like '%ssl%';
  10. +---------------+-------+
  11. | Variable_name | Value |
  12. +---------------+-------+
  13. | have_openssl  | YES   |
  14. | have_ssl      | YES   |
  15. | ssl_ca        |       |
  16. | ssl_capath    |       |
  17. | ssl_cert      |       |
  18. | ssl_cipher    |       |
  19. | ssl_key       |       |
  20. +---------------+-------+
  21. 7 rows in set (0.00 sec)

输出结果显示YES,现在ssl被完美启动起来了。

对主从服务器进行配置

在master服务器上为mysql的bin-log创建存放日志的目录

  1. mkdir /var/log/mysql
  2. chown mysql:mysql /var/log/mysql

在master服务器上生成ssl秘钥

  1. mkdir -p /etc/mysql/newcerts
  2. cd /etc/mysql/newcerts
  3. openssl genrsa 2048 > ca-key.pem
  4. openssl req -new -x509 -nodes -days 1000 -key ca-key.pem > ca-cert.pem
  5. openssl req -newkey rsa:2048 -days 1000 -nodes -keyout server-key.pem > server-req.pem
  6. openssl x509 -req -in server-req.pem -days 1000 -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 > server-cert.pem
  7. openssl req -newkey rsa:2048 -days 1000 -nodes -keyout client-key.pem > client-req.pem
  8. openssl x509 -req -in client-req.pem -days 1000 -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 > client-cert.pem

查看一下都生成了什么文件

  1. ls -alh
  2. total 40K
  3. drwxr-xr-x 2 root root 4.0K Feb 20 15:08 .
  4. drwxr-xr-x 3 root root 4.0K Feb 20 15:02 ..
  5. -rw-r--r-- 1 root root 1.6K Feb 20 15:06 ca-cert.pem
  6. -rw-r--r-- 1 root root 1.7K Feb 20 15:03 ca-key.pem
  7. -rw-r--r-- 1 root root 1.3K Feb 20 15:08 client-cert.pem
  8. -rw-r--r-- 1 root root 1.7K Feb 20 15:08 client-key.pem
  9. -rw-r--r-- 1 root root 1.1K Feb 20 15:08 client-req.pem
  10. -rw-r--r-- 1 root root 1.3K Feb 20 15:07 server-cert.pem
  11. -rw-r--r-- 1 root root 1.7K Feb 20 15:07 server-key.pem
  12. -rw-r--r-- 1 root root 1.1K Feb 20 15:07 server-req.pem

好了,秘钥生成了,下面需要做的是把ca-cert.pem、client-cert.pem、and client-key.pem拷贝到slave服务器上,首先我们在slave服务器上创建同样的文件夹。

  1. mkdir -p /etc/mysql/newcerts

现在在master服务器上把秘钥文件拷贝到slave服务器上

  1. scp /etc/mysql/newcerts/ca-cert.pem /etc/mysql/newcerts/client-cert.pem /etc/mysql/newcerts/client-key.pem root@192.168.0.101:/etc/mysql/newcerts

我们继续修改master服务器上的mysql配置文件,打开/etc/my.cnf
在我们刚才添加的配置代码中增加三行,如下:

  1. user=mysql
  2. # Default to using old password format for compatibility with mysql 3.x
  3. # clients (those using the mysqlclient10 compatibility package).
  4. old_passwords=1
  5. ssl
  6. ssl-ca=/etc/mysql/newcerts/ca-cert.pem
  7. ssl-cert=/etc/mysql/newcerts/server-cert.pem
  8. ssl-key=/etc/mysql/newcerts/server-key.pem
  9.  
  10. [mysqld_safe]
  11. log-error=/var/log/mysqld.log
  12. pid-file=/var/run/mysqld/mysqld.pid

重新启动mysql
现在我们在master服务器上登录mysql,创建帐号并提供给slave服务器以便访问master服务器。

  1. mysql -u root -p

输入如下命令创建帐号:

  1. GRANT REPLICATION SLAVE ON *.* TO 'slave用户名'@'%' IDENTIFIED BY 'slave密码' REQUIRE SSL;
  2. FLUSH PRIVILEGES;
  3. quit;

我们继续修改master服务器的mysql配置文件,填写需要读写分离的数据库名。打开/etc/my.cnf,
修改我们前面填写的配置代码如下:

  1. user=mysql
  2. # Default to using old password format for compatibility with mysql 3.x
  3. # clients (those using the mysqlclient10 compatibility package).
  4. old_passwords=1
  5. ssl
  6. ssl-ca=/etc/mysql/newcerts/ca-cert.pem
  7. ssl-cert=/etc/mysql/newcerts/server-cert.pem
  8. ssl-key=/etc/mysql/newcerts/server-key.pem
  9.  
  10. server-id               = 1
  11. log_bin                 = /var/log/mysql/mysql-bin.log
  12. expire_logs_days        = 10
  13. max_binlog_size         = 100M
  14. binlog_do_db            = test
  15.  
  16. [mysqld_safe]
  17. log-error=/var/log/mysqld.log
  18. pid-file=/var/run/mysqld/mysqld.pid

重新启动master服务器上的mysql
登录mysql,把需要读写分离的数据库导出生成sql文件并提供给slave服务器用,操作期间需要锁表,等操作完毕,再解锁。

  1. mysql -u root -p
  2. USE test;
  3. FLUSH TABLES WITH READ LOCK;

继续查看master状态,查看mysql输出信息如下:

  1. mysql> show master status;
  2. +------------------+----------+--------------+------------------+
  3. | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
  4. +------------------+----------+--------------+------------------+
  5. | mysql-bin.000002 |       98 | test         |                  |
  6. +------------------+----------+--------------+------------------+
  7. 1 row in set (0.00 sec)

连接mysql的shell别关闭,因为一旦关闭mysql就解锁了。再打开一个终端窗口,导出sql文件并拷贝到slave服务器上。

  1. cd /tmp
  2. mysqldump -u root -p密码 --opt test > test.sql
  3. scp test.sql root@192.168.90.89:/tmp

好了,现在我们可以把master服务器上的mysql终端关闭退出了,继续输入:

  1. UNLOCK TABLES;
  2. quit;

让我们开始配置slave服务器,打开编辑mysql的配置文件/etc/my.cnf
在[mysqld]和[mysqldump]之间的章节加入如下配置代码:

  1. user=mysql
  2. # Default to using old password format for compatibility with mysql 3.x
  3. # clients (those using the mysqlclient10 compatibility package).
  4. old_passwords=1
  5. ssl
  6. # server-id 必须唯一,尤其要与master服务器上的配置区别开来
  7. server-id=2
  8. master-connect-retry=60
  9. replicate-do-db=test
  10.  
  11. [mysqld_safe]
  12. log-error=/var/log/mysqld.log
  13. pid-file=/var/run/mysqld/mysqld.pid

重新启动slave服务器上的mysql
接下来我们首先停掉slave服务器上的mysql slave服务

  1. mysqladmin --user=root --password=密码 stop-slave

然后我们导入sql文件

  1. mysql -u root -p密码 test < test.sql

现在登录slave服务器上的mysql

  1. mysql -u root -p

参考刚才在master服务器mysql终端输入show master status命令打印出来的结果,我们输入以下命令并执行:

  1. CHANGE MASTER TO MASTER_HOST='master服务器地址', MASTER_USER='slave用户名', MASTER_PASSWORD='slave密码', MASTER_LOG_FILE='打印结果的File值', MASTER_LOG_POS=打印结果的Position值, MASTER_SSL=1, MASTER_SSL_CA = '/etc/mysql/newcerts/ca-cert.pem', MASTER_SSL_CERT = '/etc/mysql/newcerts/client-cert.pem', MASTER_SSL_KEY = '/etc/mysql/newcerts/client-key.pem';

启动slave服务:

  1. START SLAVE;

现在来看一下slave的状态,mysql返回结果为:

  1. mysql> show slave status\G;
  2. *************************** 1. row ***************************
  3.              Slave_IO_State: Waiting for master to send event
  4.                 Master_Host: 192.168.90.216
  5.                 Master_User: slaveusr
  6.                 Master_Port: 3306
  7.               Connect_Retry: 60
  8.             Master_Log_File: mysql-bin.000002
  9.         Read_Master_Log_Pos: 98
  10.              Relay_Log_File: slave-relay-bin.000002
  11.               Relay_Log_Pos: 235
  12.       Relay_Master_Log_File: mysql-bin.000002
  13.            Slave_IO_Running: Yes
  14.           Slave_SQL_Running: Yes
  15.             Replicate_Do_DB: test
  16.         Replicate_Ignore_DB:
  17.          Replicate_Do_Table:
  18.      Replicate_Ignore_Table:
  19.     Replicate_Wild_Do_Table:
  20. Replicate_Wild_Ignore_Table:
  21.                  Last_Errno: 0
  22.                  Last_Error:
  23.                Skip_Counter: 0
  24.         Exec_Master_Log_Pos: 98
  25.             Relay_Log_Space: 235
  26.             Until_Condition: None
  27.              Until_Log_File:
  28.               Until_Log_Pos: 0
  29.          Master_SSL_Allowed: Yes
  30.          Master_SSL_CA_File: /etc/mysql/newcerts/ca-cert.pem
  31.          Master_SSL_CA_Path:
  32.             Master_SSL_Cert: /etc/mysql/newcerts/client-cert.pem
  33.           Master_SSL_Cipher:
  34.              Master_SSL_Key: /etc/mysql/newcerts/client-key.pem
  35.       Seconds_Behind_Master: 0
  36. 1 row in set (0.00 sec)

好了,现在我们在master服务器上操作mysql插入一条数据,slave服务器的mysql也会更新同样的一条数据,删除亦会同步,mysql replication配置完毕。