MySql Master-Master Replication

July 9, 2008 by manojsamtani

MySQL DataBase Master-Master Replication

Required Packages

mysql

mysql-server

Master1 server ip: 192.168.0.82

Master2 server ip: 192.168.0.83

Slave username: user

Slave password: user

Your data directory is: /var/lib/mysql/

In Master1 Database machine edit /etc/my.cnf :

# let’s make it so auto increment columns behave by having different increments on both servers

auto_increment_increment=2

auto_increment_offset=1

# Replication Master Server

# binary logging is required for replication

log-bin=/var/log/master1-bin

binlog-ignore-db=mysql

binlog-ignore-db=test

# required unique id between 1 and 2^32 – 1

server-id = 1

#following is the slave settings so this server can connect to master2

master-host = 192.168.0.83

master-user = slaveuser

master-password = slavepw

master-port = 3306

Save and exit.

In Master2 Database machine edit /etc/my.cnf :

# let’s make it so auto increment columns behave by having different increments on both servers

auto_increment_increment=2

auto_increment_offset=2

# Replication Master Server

# binary logging is required for replication

log-bin=/var/log/master2-bin

#Ignore database to replicate

binlog-ignore-db=mysql

binlog-ignore-db=test

# required unique id between 1 and 2^32 – 1

server-id = 2

#following is the slave settings so this server can connect to master1

master-host = 192.168.0.82

master-user = user

master-password = user

master-port = 3306

Save and exit.

Enter the following command on Master1 to create/grant user level access on the database to Master2.

mysql> grant replication slave on *.* to slaveuser@'192.168.0.83' identified by 'slavepw';

mysql>FLUSH PRIVILEGES;

Now, enter the following command on Master2 for create/grant user level access on the database to Master1.

mysql> grant replication slave on *.* to user@'192.168.0.82' identified by 'user';

mysql>FLUSH PRIVILEGES;

Now make slave both machines to each other.

On Master1 enter the following:

mysql> show master status;

+----------------------+----------+--------------+-----------------------+

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+----------------------+----------+--------------+-----------------------+

| mysql-bin-log.000017 | 289 | | mysql,test,mysql,test |

+----------------------+----------+--------------+-----------------------+

1 row in set (0.00 sec)

NOTE:This mysql-bin-log file used to read data by the slave servers to replicate Database. Copy file name and position i.e; (289).

Now make Master2 the slave of Master1.

Enter the following command on Master2:

mysql> stop slave;

mysql> CHANGE MASTER TO MASTER_HOST='192.168.0.82', MASTER_USER='user', MASTER_PASSWORD='user', MASTER_LOG_FILE='mysql-bin-log.0000017', MASTER_LOG_POS=98;

mysql>start slave;

mysql>show slave status\G;

It will show you something inside the output:

Master_Log_File: mysql-bin-log.000017

Read_Master_Log_Pos: 289

Relay_Log_File: localhost-relay-bin.000026

Relay_Log_Pos: 239

Relay_Master_Log_File: mysql-bin-log.000017

|

|

Seconds_Behind_Master: 0

If it is showing Seconds_Behind_Master not “NULL” then this slave is working fine.

Then for making Master1 slave to Master2, we need Master2 machine’s mysql-bin-log and position, for that run the following command for required information:

mysql> show master status;

+----------------------+----------+--------------+-----------------------+

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+----------------------+----------+--------------+-----------------------+

| mysql-bin-log.000002 | 574 | | mysql,test,mysql,test |

+----------------------+----------+--------------+-----------------------+

1 row in set (0.00 sec)

NOTE:This mysql-bin-log file used to read data by the slave servers to replicate database. Copy file name and position i.e; (574).

Now make Master1 the slave of Master2

Enter the following command on Master1:

mysql> stop slave;

mysql> CHANGE MASTER TO MASTER_HOST='192.168.0.83', MASTER_USER='user', MASTER_PASSWORD='user', MASTER_LOG_FILE='mysql-bin-log.000002', MASTER_LOG_POS=98;

mysql> start slave;

mysql>show slave status\G;

It will show you something inside the output:


Master_Log_File: mysql-bin-log.000002

Read_Master_Log_Pos: 574

Relay_Log_File: mysqld-relay-bin.000003

Relay_Log_Pos: 239

Relay_Master_Log_File: mysql-bin-log.000002

|

|

Seconds_Behind_Master: 0

If it is showing Seconds_Behind_Master not “NULL” then this slave is working fine.

And now do some testing like create/delete database and tables on one machine and check on other, is it showing the changes???? if both are showing same data then that means your replication is working fine.

DomainKeys with Sendmail

July 9, 2008 by manojsamtani

Required Packages
Sendmail full package like sendmail, sendmail-devel etc..
(should be 8.13 or above version)
OpenSSL
SASL Packages

Download the Package of dk-milter (dk-milter-1.0.0.tar.gz)

tar zxvf dk-milter-1.0.0.tar.gz
cd dk-milter-1.0.0
cd devtools/Site
and add/modify the following lines to site.config.m4
APPENDDEF(`conf_sendmail_ENVDEF’, `-DMILTER’)
APPENDDEF(`confENVDEF’,`-DSASL -DSTARTTLS’)
APPENDDEF(`confLIBS’, `-lsasl -lssl -lcrypto’)
APPENDDEF(`confINCDIRS’, `-I/usr/local/ssl/include ‘)
APPENDDEF(`confLIBDIRS’, `-L/usr/local/ssl/lib ‘)
APPENDDEF(`confENVDEF’, `-D_FFR_ANTICIPATE_SENDMAIL_MUNGE ‘)

Add the following lines to dk-milter-1.0.0/dk-filter/Makefile.m4

APPENDDEF(`confLIBS’, `-lssl -lcrypto’)

dnl Enable and edit these paths as per your configuration:

APPENDDEF(`confINCDIRS’, `-I/usr/local/ssl/include ‘)
APPENDDEF(`confLIBDIRS’, `-L/usr/local/ssl/lib ‘)
bldPUSH_SMLIB(`dk’)
APPENDDEF(`confINCDIRS’, `-I../libdk/ ‘)
bldPUSH_SMLIB(`sm’)
APPENDDEF(`confINCDIRS’, `-I/usr/local/sendmail/include’)
APPENDDEF(`confLIBDIRS’, `-L/usr/local/sendmail/libmilter’)
APPENDDEF(`confLIBDIRS’, `-L/usr/local/sendmail/libsm’)
APPENDDEF(`confLIBDIRS’, `-L/usr/local/sendmail/libsmdb’)
APPENDDEF(`confLIBDIRS’, `-L/usr/local/sendmail/libsmutil’)
And add the following line(s) to sendmail.mc
INPUT_MAIL_FILTER(`dk-filter’, `S=inet:5000@localhost’)
dk-filter is domain key filter and 5000 is port on which dk-filter daemon will listen…

Then goto the dk-milter-1.0.0 directory and run the following commands
1) ./Build
2) ./Build install

Now create servers public/private key pair for the domain using the following command:
1) openssl genrsa -out rsa.private 1024
2) openssl rsa -in rsa.private -out rsa.public -pubout -outform PEM
Now change private key name to like test.com_key.pem

Now time to DNS TXT record entry with public key

mail._domainkey.test.com. 1D IN TXT “k=rsa; t=y;
p=MHwwDQYJKoZIhvcNAQEBBQADawAwaA
JhAJUf/A69N7tYPcoursysqYFzBl3+yzCCUsNQA
Y6TIcD02OEix1xETOOubJ1Z5i7UrR3E36aEeeH
A9tdDu1P+xvQ6PoQEXeevGWfs61j4lVRQErx4K
m+KKcqja6nSgZbflQIDAQAB”

Replace test.com with your domain, and *mail* in the DNS record *mail*._domainkey.test.com. is selector to select the key.

Create a user for dk-milter like dkuser and run the following command to start dk-milter for your server.
dk-filter -l -p inet:5000@localhost -c nofws -d test.com -D -H -s
/usr/share/ssl/private/test.com_key.pem -S mail -u dkuser -m MTA

restart sendmail service.

Send mail to any Yahoo!/GMail ID and check to see whether the
authentication-results header shows result as pass or fail.