Home Server MySQL Database server

Index.

17-Jul-2011: Initial release.
27-Feb-2012: Added tuning paragraph.
25-Nov-2012: adjusted for Slackware.

 

Introduction.

For several applications we need a SQL database server, the most used server is MySQL. Several applications that will be installed on our Home Server use MySQL. After installation of the server, the setup is secured. Then we configure the server so that it can be accessed from the workstations and the DMZ network. For workstations it can be useful for office applications like Excel by using a database connector. For the DMZ network you should think about dynamic web servers applications like WordPress, Joomla etc.

 

Installation.

Install MySQL from the Slackware distribution, then install the initial database and start the server.

root@homsrv:~# /usr/bin/mysql_install_db
Installing MySQL system tables...
OK
Filling help tables...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h homsrv password 'new-password'

Alternatively you can run:
/usr/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd /usr/mysql-test ; perl mysql-test-run.pl

Please report any problems with the /usr/bin/mysqlbug script!

root@homsrv:~# chown -R mysql:mysql /var/lib/mysql/*
root@homsrv:~# chmod 755 /etc/rc.d/rc.mysqld 
root@homsrv:~# /etc/rc.d/rc.mysqld start
root@homsrv:~# 121125 20:39:46 mysqld_safe Logging to '/var/lib/mysql/homsrv.err'.
121125 20:39:47 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysq

 

After MySQL is started, secure it:

root@homsrv:~# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
 SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

Set root password? [Y/n] Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!

By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] Y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] Y
 ... Success!

By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] Y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!

root@homsrv:~#

 

Now it’s secured against remote access. Edit /etc/mysql/my.cnf and comment out the following two line:

#skip-networking

#log-bin=mysql-bin

 

Finally restart MySQL:

root@slack14:~# /etc/rc.d/rc.mysqld restart
121125 20:48:19 mysqld_safe mysqld from pid file /var/run/mysql/mysql.pid ended
root@slack14:~# 121125 20:48:20 mysqld_safe Logging to '/var/lib/mysql/slack14.err'.
121125 20:48:21 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysq

 

A note about the firewall, I have updated the gateway part to allow TCP port 3306 from the DMZ to our Home Server. It’s not needed at this moment, but it is when we add a public web server that will use this MySQL server. Even if this port is open, it is still secure because there are no databases in MySQL that are allowed from the DMZ network.

 

 

Tuning.

Depending on you’re available memory and database workload you may need to tune your MySQL server to improve it’s performance. The default installation is good for a small server. In the directory /etc/mysql you will find several examples. Copy one of them to /etc/mysql/my.cnf and start with that file. For a home server my-small.cnf or my-medium.cnf are good to start with. If you run web server applications like WordPress, Drupal or Joomla, you should make changes to the configuration. Next is an example diff output of /etc/mysql/my.cnf that works well for a small web server:

 

--- my.cnf.orig 2012-02-27 20:25:20.248164745 +0100
+++ my.cnf      2012-02-27 20:27:33.024816227 +0100
@@ -28,14 +28,15 @@
 socket         = /var/run/mysqld/mysqld.sock
 datadir                = /var/lib/mysql
 skip-external-locking
-key_buffer_size = 16M
+key_buffer_size = 96M
 max_allowed_packet = 1M
-table_open_cache = 64
-sort_buffer_size = 512K
+table_open_cache = 256
+sort_buffer_size = 2M
 net_buffer_length = 8K
-read_buffer_size = 256K
-read_rnd_buffer_size = 512K
+read_buffer_size = 1M
+read_rnd_buffer_size = 2M
 myisam_sort_buffer_size = 8M
+query_cache_size = 16M

 # Don't listen on a TCP/IP port at all. This can be a security enhancement,
 # if all processes that need to connect to mysqld run on the same host.
@@ -118,12 +119,12 @@
 #innodb_log_group_home_dir = /var/lib/mysql
 # You can set .._buffer_pool_size up to 50 - 80 %
 # of RAM but beware of setting memory usage too high
-#innodb_buffer_pool_size = 16M
+innodb_buffer_pool_size = 16M
 #innodb_additional_mem_pool_size = 2M
 # Set .._log_file_size to 25 % of buffer pool size
 #innodb_log_file_size = 5M
 #innodb_log_buffer_size = 8M
-#innodb_flush_log_at_trx_commit = 1
+innodb_flush_log_at_trx_commit = 2
 #innodb_lock_wait_timeout = 50

 [mysqldump]

 

Note that innodb_flush_log_at_trx_commit is set to 2, this means that MySQL does not wait until the physical write to disk is complete. The negative impact of this change is that this gives a small risk of data corruption when the system crashes or looses power after a database update and before the data is written to disk.The positive impact is that this setting gives a huge speed increase for applications that use the innodb tables.

 

Backups.

Making a backup of a running MySQL database cannot be done by just making a backup of /var/lib/mysql because cached data is not on the disk. Instead it is best to make use of MySQL tools that are made for that job. Below is an example of a script that can be run by cron on a daily schedule. Then, a real backup program can backup the /root/mysql_backup directory.

[root@homsrv ~]# cat /etc/cron.daily/mysqlbackup
#!/bin/sh
#
# Backup MySQL databases that are used on this server

mkdir -p /root/mysql_backup
cd /root/mysql_backup

mysqlshow  --count -u dspam -ptotallysecret dspam > dspam.stat
mysqlcheck -o -u dspam -ptotallysecret dspam > dspam.log
mysqldump --add-drop-table -u dspam -ptotallysecret dspam > dspam.sql

[root@lx02 ~]#

 

Install phpMyAdmin.

I have created a Slackbuild script to package phpMyAdmin, but just unpacking the source archive in /srv/http/htdocs works well too. Copy /srv/http/htdocs/phpMyAdmin/config.sample.inc.php to config.inc.php and edit that file. Then browse to http://homsrv.wpl.ym/phpMyAdmin/index.php and login as root with the password you have set when you secured the MySQL installation.

 

Download.

See the download page for the script and configuration files.