How To Access MailCleaner MySQL

Are you running a MailCleaner Anti Spam Gateway on your network? If that is the case, you may have other systems that could benefit from directly connecting to the MySQL instance on you MailCleaner Server. Below is our MailCleaner MySQL configuration script that will set the MySQL root password and add the proper firewall rules to allow remote access. In order to use the script below, edit the script with your desired root_pass and access_subnet and the copy the script to your server and make executable.

#!/bin/bash
#
# mc-mysql-remote
#
#

# set custom variables
root_pass='your-root-password'
# for access from anywhere set 0.0.0.0/0
access_subnet='your-access-subnet'

# variables don't edit
mysql='/opt/mysql5/bin/mysql'
master='/var/mailcleaner/run/mysql_master/mysqld.sock'
slave='/var/mailcleaner/run/mysql_slave/mysqld.sock'
spath='/usr/mailcleaner/etc/init.d'

# restart mysql with nopass option
echo "[*] Starting mysql with nopass option"
$spath/mysql_master restart nopass &>/dev/null
$spath/mysql_slave restart nopass &>/dev/null
sleep 5

# login to mysql and set root passwords
echo "[*] Setting mysql root password"
$mysql -S $master -uroot -e "use mysql; update user set password=PASSWORD('$root_pass') where User='root';"
$mysql -S $slave -uroot -e "use mysql; update user set password=PASSWORD('$root_pass') where User='root';"
sleep 2

# restart mysql with no options
echo "[*] Starting mysql normally"
$spath/mysql_master restart &>/dev/null
$spath/mysql_slave restart &>/dev/null
sleep 5

# add root access from anywhere
echo "[*] Configuring mysql root access"
$mysql -S $master -uroot -p"$root_pass" -e "use mysql; grant all on *.* to 'root'@'%' identified by '$root_pass'; flush privileges;"
$mysql -S $slave -uroot -p"$root_pass" -e "use mysql; grant all on *.* to 'root'@'%' identified by '$root_pass'; flush privileges;"
sleep 2

# set firewall rules to allow access
echo "[*] Configuring firewall rules"
$mysql -S $master -uroot -p"$root_pass" -e "use mc_config; insert into external_access (service,port,protocol,allowed_ip) values ('mysql','3306:3307','TCP','$access_subnet');"
$mysql -S $slave -uroot -p"$root_pass" -e "use mc_config; insert into external_access (service,port,protocol,allowed_ip) values ('mysql','3306:3307','TCP','$access_subnet');"
sleep 2

# restart firewall
echo "[*] Flushing firewall rules"
$spath/firewall restart &>/dev/null
Did you find this article useful? Why not share it with your friends?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.