www.linuxismybff.com

24Oct/090

mysql basic replication setup

mysql replication is fairly common and the benefits go without saying.  Below is what I have found to be the best process for configuring replication on two vanilla mysql devices.

Install mysql on both nodes.

yum install mysql-server -y

On both nodes confirm that mysql is started and listening for remote connection, not bound to loopback.

/etc/init.d/mysqld start
chkconfig mysqld on
netstat -ntlp | grep mysqld

Add the below to '/etc/my.cnf' on the master

#####Added for replication######
log-bin = /var/lib/mysqllogs/mysql-bin.log
log-bin-index = /var/lib/mysqllogs/mysql-bin.index
server-id=1
expire_logs_days=10
#####replication specific options completed#####

Create this directory on the master

mkdir /var/lib/mysqllogs
chown mysql:mysql /var/lib/mysqllogs

Restart mysql on the master to apply net settings

/etc/init.d/mysqld restart

Add replication user to mysql

mysql

GRANT REPLICATION SLAVE ON *.* TO 'replication'@'%' IDENTIFIED BY 'replicationpass';

flush privileges;

mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |       98 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

Backup master data to sync up slave

mysqldump --all-databases --master-data=1 | gzip -1 > /root/all.sql.gz

SCP data over to the slave

[root@master ~]# scp /root/all.sql.gz root@slave.linuxismybff.com:~
root@slave.linuxismybff.com's password:
all.sql.gz                                                                                                                         100%  133KB 132.8KB/s   00:00

Configure the slaves my.cnf

####Added for replication####
relay-log=/var/lib/mysqllogs/slave-relay-log
relay-log-index=/var/lib/mysqllogs/slave-relay-log.index
read_only=1 #This is option but suggested
server-id=2
####End of replication options####

Create this directory on the slave

mkdir /var/lib/mysqllogs

chown mysql:mysql /var/lib/mysqllogs

Restart mysql on the master to apply net settings

/etc/init.d/mysqld restart

Initialize replication on the slave

[root@slave ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.0.77 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> CHANGE MASTER TO MASTER_HOST='10.176.2.180', MASTER_USER='replication', MASTER_PASSWORD='replicationpass';
Query OK, 0 rows affected (0.08 sec)

On slave import the previous master dump.  Be aware that this will overwrite any databases that are already there.

[root@slave ~]# zcat /root/all.sql.gz | mysql
[root@slave ~]#

Start slave

[root@slave ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.0.77 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.176.2.180
Master_User: replication
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 169
Relay_Log_File: slave-relay-log.000002
Relay_Log_Pos: 235
Relay_Master_Log_File: mysql-bin.000001
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: 169
Relay_Log_Space: 235
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
1 row in set (0.01 sec)

Confirm that it is working, on both nodes confirm that listed databases are in sync

[root@master ~]# mysql -e "show databases;"
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+

[root@slave ~]# mysql -e "show databases;"
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+

Add a database on the master to confirm this change is replicated

[root@master ~]# mysql -e "create database repl;"
[root@master ~]# mysql -e "show databases;"
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| repl               |
| test               |
+--------------------+

[root@slave ~]# mysql -e "show databases;"
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| repl               |
| test               |
+--------------------+

Drop database on master and confirm that it is removed

[root@master ~]# mysql -e "drop database repl;"
[root@master ~]# mysql -e "show databases;"
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+

[root@slave ~]# mysql -e "show databases;"
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| test               |
+--------------------+

All done!  It is important to note that I did not think of all of this stuff up on my own.  It is a collection of data I have collected from mysql's page, how-to-forge, and misc notes I had laying around.  Hopefully it helps someone and I know that I'll refer to it on future installations.

Filed under: Mysql Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

(required)

No trackbacks yet.