How to Start, Stop and Restart MySQL

What do you do if your database server is down? Well, you can certainly reboot the server. That might solve the problem, but I rather restart the database first before doing something as drastic as rebooting a server. There are instances were rebooting doesn’t always solve the problem if there’s an issue with the database. Now, most open-source blogs and web applications today use MySQL as their database of choice. So, here are instructions on how to start, stop and restart MySQL on Ubuntu and Debian-based systems.

Stop MySQL database

$ sudo /etc/init.d/mysql stop

Start MySQL database

$ sudo /etc/init.d/mysql start

Restart MySQL database

$ sudo /etc/init.d/mysql restart

MySQL mysql_connect new_link

I want to share a database connection access problem I had last week, while working with a custom PHP script inside WordPress. I had created this WordPress Page Template containing some PHP code that needs access to the database. The problem was that the database connection for my custom script overwrote the WordPress database connection that was previously establish, causing certain parts of WordPress to not display properly.

It took me a while to figure out that it was the newer database connection of my custom PHP script that was causing the previously established WordPress database connection to disappear. Hence, certain parts of the WordPress page were not displayed. Little did I know, that the fix was quite simple. So, here’s a sample of my mysql_connect code. Prior to this line, I’ve already set the variables.

Mysql_connect

$db=mysql_connect($host,$username,$password);

The Fix

Simply add a fourth parameter called new_link and set it to TRUE.

$db=mysql_connect($host,$username,$password,TRUE);

What this does is basically telling mysql_connect to establish a new connection, while keeping the older mysql_connect connection around, in case we need to access it at a later time. It’s amazing how one little switch in a command can make a huge difference to this seemingly simple code. Anyways, adding a fourth parameter and setting it to TRUE was the solution.

Backup MySQL via Bash Script

If you run your own LAMP server, you might want to check out this Bash script which perform backups of the MySQL database. The neat little script can be set to run independently or as part of a cron job. All you have to do is provide the backup directory, username, password, hostname and port number.

The backup script is quite simple. It checks if the backup directory exists and creates one if it’s missing. It will then list all databases and performs a MySQL dump of each database ignoring the mysql, information_schema, and test databases. The file is formatted in gzip and then stored in the backup directory.

The backup script can be set to run every day or once a week using cron. The backup will depend on your backup requirements. I recommend testing the backup and restore process to see if it works.

The Perfect Server Based On Ubuntu 10.04

There is no such thing as a perfect server, but this particular one is as close as you can get to being perfect. This tutorial will walk you through how to install the Ubuntu 10.04 LTS Server with all the services available typically that you get from ISPs and hosters.

It contains the installation of Apache web server (SSL-capable), Postfix mail server with SMTP-AUTH and TLS, BIND DNS server, Proftpd FTP server, MySQL server, Courier POP3/IMAP, Quota, Firewall, etc. It also installs the free web hosting control panel called ISPConfig2. Here’s the link to the tutorial.