MySQL Server Replication with ssl

In: Bushan| Linux| MySQL| Replication

4 Mar 2010

via Bhushan

TYPES OF MYSQL REPLICATION

  • Statement-based Replication

  • Row-based Replication

  • Mixed

    To change the type of Replication modify my.cnf configuration file and change

    binlog_format=mixed | row | statement

mysql> SHOW VARIABLES LIKE ‘binlog_format’;

+—————+——-+

| Variable_name | Value |

+—————+——-+

| binlog_format | MIXED

+—————+——-+

Processes/Threads inside MySQL that are responsible for replication

  • MASTER - Binlog Dump Thread

  • SLAVE - I/O Thread

            • SQL Thread

Statements useful to check the status of these threads as replication goes:

mysql> SHOW PROCESSLIST\G

mysql> SHOW MASTER STATUS\G

mysql> SHOW SLAVE STATUS\G

Directories and File Locations

  • Datadir – /var/lib/mysql

  • General Log dir. - /var/log

  • Bin Log dir. - /var/log/mysql

  • Configuration file - /etc/mysql/my.cnf

  • SSL Certificates - /etc/mysql/ssl

  • Relay Log file - /var/lib/mysql/slavehost-relay-bin.NNNNNN

  • Status Files - master.info, relay-log.info

Note:

- All modification/updates to data should be done on Master only, and not on any Slave. Slave should be used for queries.

setup replication

MySQL MASTER = 192.168.1.100:3306

MySQL SLAVE = 192.168.1.111:3306

MASTER host

root@sage:~# mkdir /etc/mysql/ssl

root@sage:~# cd /etc/mysql/ssl/

root@sage:~# rm -rf *

Create CA certificate

root@sage:~# openssl genrsa 2048 > ca-key.pem

root@sage:~# openssl req -new -x509 -nodes -days 1000 -key ca-key.pem > ca-cert.pem

Create server certificate

root@sage:~# openssl req -newkey rsa:2048 -days 1000 -nodes -keyout server-key.pem > server-req.pem

root@sage:~# openssl x509 -req -in server-req.pem -days 1000 -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 > server-cert.pem

Copy ca-cert file to MySQL clients & slaves

root@sage:~# scp ca-cert.pem root@slave-host-IP:/etc/mysql/ssl/

Modify configuration file

root@sage:~# vi /etc/mysql/my.cnf

Enable Binary logging in Mixed format. And specify a Unique Server ID of Master

[mysqld]

log-bin = /var/log/mysql/mysql-bin

binlog_format = mixed

server-id = 1

ssl-key = /etc/mysql/ssl/server-key.pem

ssl-cert = /etc/mysql/ssl/server-cert.pem

ssl-ca = /etc/mysql/ssl/ca-cert.pem

Test SSL connectivity using MySQL-Client

root@sage:~# /etc/init.d/mysql restart

root@sage:~# mysql --ssl-ca=/etc/mysql/ssl/ca-cert.pem -u root -p

mysql> SHOW VARIABLES LIKE ‘%ssl%’;

+—————+——————————–+

| Variable_name | Value |

+—————+——————————–+

| have_openssl | YES |

| have_ssl | YES |

| ssl_ca | /etc/mysql/ssl/ca-cert.pem |

| ssl_capath | |

| ssl_cert | /etc/mysql/ssl/server-cert.pem |

| ssl_cipher | |

| ssl_key | /etc/mysql/ssl/server-key.pem |

+—————+——————————–+

mysql> SHOW STATUS LIKE ‘Ssl_cipher’;

+—————+——————–+

| Variable_name | Value |

+—————+——————–+

| Ssl_cipher | DHE-RSA-AES256-SHA |

+—————+——————–+

confirms that SSL is supported & enabled on MASTER

Create mysql user on master that has the privileges to do replication.

mysql -u root -p

mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO replssl@’%’ IDENTIFIED BY ‘replipass’ REQUIRE SSL;

If user already exists

mysql> GRANT USAGE ON *.* TO 'repl'@'%' REQUIRE SSL;

mysql> FLUSH PRIVILEGES;

mysql> SHOW GRANTS FOR repl;

Find the location where Master is writing now

mysql> show master status;

+—————-+——–+————-+—————+

| File | Position |Binlog_Do_DB |Binlog_Ignore_DB

+—————-+——–+————-+——————

|mysql-bin.000004| 7705 | |

+—————-+——–+————-+—————–

They are: mysql-bin.000004, 7705

Take snapshot of Mysql data on Master and then scp it to slave.

mysql> FLUSH TABLES WITH READ LOCK;

root@sage:~# tar czvf ~/mysql-snapshot.tar.gz /var/lib/mysql

mysql> UNLOCK TABLES;

Copy snapshot to the slave

root@sage:~# scp mysql-snapshot.tar/gz user@slave-IP:~

SLAVE side

To configure this host as a replication slave, you can choose between

two methods :

- Use the CHANGE MASTER TO command

CHANGE MASTER TO MASTER_HOST=<host>, MASTER_PORT=<port>,

MASTER_USER=<user>, MASTER_PASSWORD=<password> ……

OR

- Set the variables in /etc/mysql/my.cnf.

Create client certificate

root@sage:~# openssl req -newkey rsa:2048 -days 1000 -nodes -keyout client-key.pem > client-req.pem

root@sage:~# openssl x509 -req -in client-req.pem -days 1000 -CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 > client-cert.pem

    root@sage:~# vi /etc/mysql/my.cnf

[mysqld]

server-id = 2

master-host = 192.168.1.100

master-user = repl

master-password = replipass

master-port = 3306

log-bin = /var/log/mysql/mysql-bin

binlog_format = mixed

tmpdir = /tmp/

[client]

ssl-ca=/etc/mysql/ssl/ca-cert.pem

#ssl-key=/etc/mysql/ssl/client-key.pem

#ssl-cert=/etc/mysql/ssl/client-cert.pem

If the account has no special SSL requirements or was created using a GRANT statement that includes the REQUIRE SSL option, a client can connect securely by using just the --ssl-ca option:

shell> mysql --ssl-ca=cacert.pem

To require that a client certificate also be specified, create the account using the REQUIRE X509 option. Then the client must also specify the proper client key and certificate files or the server will reject the connection:

shell> mysql --ssl-ca=cacert.pem \
       --ssl-cert=client-cert.pem \
       --ssl-key=client-key.pem

In other words, the options are similar to those used for the server. Note that the Certificate Authority certificate has to be the same. “

Ref: http://dev.mysql.com/doc/refman/5.0/en/secure-using-ssl.html

root@sage:~# /etc/init.d/mysql restart

Test connectivity to Master from Slave

root@sage:~# mysql –ssl-ca=/etc/mysql/ssl/ca-cert.pem -u root -p -h 192.168.1.100

root@sage:~# mysql -u root -p

mysql> SLAVE STOP;

mysql> mysql> CHANGE MASTER TO MASTER_HOST=’192.168.1.100′, MASTER_PORT=3306, MASTER_USER=’replssl’, MASTER_PASSWORD=’1′,MASTER_LOG_FILE=’mysql-bin.000004′, MASTER_LOG_POS=7705, MASTER_SSL=1, MASTER_SSL_CA=’/etc/mysql/ssl/ca-cert.pem’;

mysql> START SLAVE;

mysql> SHOW SLAVE STATUS\G

mysql> SHOW PROCESSLIST\G

Note : If we have given only GRANT … REQUIRE SSL to replication user then MASTER_SSL=1, MASTER_SSL_CA are to be specidfied. ITo require that a client certificate also be specified, create the account using the REQUIRE X509 option.

Share and Enjoy:

  • Print this article!
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Live
  • MySpace
  • Netvibes
  • Reddit
  • Slashdot
  • SphereIt
  • StumbleUpon
  • Suggest to Techmeme via Twitter
  • Technorati
  • Twitter
  • Yahoo! Bookmarks

Related Posts:

  • No Related Posts

32 Responses to MySQL Server Replication with ssl

Avatar

Alexander7

July 21st, 2011 at 2:56 pm

buy@generic.LEVITRA” rel=”nofollow”>…

Need cheap generic LEVITRA?…

Avatar

ANGEL

July 29th, 2011 at 1:10 am

Order@Coral.Calcium.Online” rel=”nofollow”>..

Buygeneric pills…

Avatar

DUSTIN

July 29th, 2011 at 7:27 pm

Cheap@Generic.Abilify” rel=”nofollow”>..

Buygeneric drugs…

Avatar

EDUARDO

July 29th, 2011 at 8:16 pm

Cheap@Abilify.5mg.10mg.15mg.20mg.30mg” rel=”nofollow”>..

Buygeneric pills…

Avatar

BRANDON

July 29th, 2011 at 8:42 pm

Buy@Abilify.5mg.10mg.15mg.20mg.30mg” rel=”nofollow”>..

Buyno prescription…

Avatar

SALVADOR

July 30th, 2011 at 6:12 pm

Cheap@Acai.Online” rel=”nofollow”>…

Buynow…

Avatar

LEON

October 19th, 2011 at 10:53 am

Avatar

BILLY

October 19th, 2011 at 2:13 pm

hiv aids in california in 1997

Buy_generic pills…

Avatar

FRANCIS

October 19th, 2011 at 3:53 pm

Avatar

STUART

October 21st, 2011 at 6:25 am

viagra in the uk

Buy_no prescription…

Avatar

WESLEY

October 22nd, 2011 at 10:45 am

Avatar

KENNETH

October 22nd, 2011 at 2:49 pm

arimidex and marathon

Buy_generic drugs…

Avatar

FREDERICK

October 25th, 2011 at 5:26 am

good and bad drugs

Buy_without prescription…

Avatar

HOWARD

October 25th, 2011 at 7:06 am

is hepatitis b contagious

Buy_generic pills…

Avatar

BILLY

October 25th, 2011 at 1:46 pm

drug rehab center british columbia

Buy_generic drugs…

Avatar

RENE

October 28th, 2011 at 10:06 am

terbutaline autism claim

Buy_no prescription…

Avatar

ANDREW

October 28th, 2011 at 11:46 am

iv zofran

Buy_drugs without prescription…

Avatar

AUSTIN

October 30th, 2011 at 6:42 pm

does birth control control moods

Buy_generic drugs…

Avatar

JOSHUA

October 30th, 2011 at 10:03 pm

chf vs pulmonary edema

Buy_generic drugs…

Avatar

DUSTIN

October 31st, 2011 at 9:23 pm

weight loss tricks for 40 somethings

Buy_drugs without prescription…

Avatar

MARCUS

November 3rd, 2011 at 1:03 am

hypertension police ma

Buy_drugs without prescription…

Avatar

ALBERT

November 4th, 2011 at 6:10 am

psychostimulant treatment of depression

Buy_drugs without prescription…

Avatar

CLINTON

November 4th, 2011 at 9:30 am

copd lung sounds

Buy_it now…

Avatar

BRENT

November 5th, 2011 at 6:27 am

images of tongue cancer

Buy_generic meds…

Avatar

VERNON

November 7th, 2011 at 11:47 am

Avatar

RICK

November 7th, 2011 at 1:27 pm

forensic drug test swab

Buy_no prescription…

Avatar

ROGER

November 11th, 2011 at 8:01 pm

how to make glucose water

Buy_it now…

Avatar

RUSSELL

November 11th, 2011 at 11:21 pm

dr loss phil ultimate weight

Buy_no prescription…

Avatar

JIM

November 12th, 2011 at 4:01 pm

indomethacin opthalmic drops

Buy_generic pills…

Avatar

FERNANDO

November 13th, 2011 at 12:21 am

hormone shot for prostate cancer

Buy_generic drugs…

Avatar

COREY

November 13th, 2011 at 5:01 pm

tobacco alternatives herbal

Buy_generic drugs…

Avatar

JOHNNIE

November 16th, 2011 at 10:19 am

black molley slang drug name

Buy_generic drugs…

Comment Form

Recent Posts