If you're using MySQL, there are some easy things you can do to secure your systems and significantly reduce the risk of unauthorised access to your sensitive data.

The most valuable asset for technology-based organisations is usually the customer or product information in their databases. And so, a critical part of database administration in such organisations consists of securing these databases against outside attack and hardware/software failures.

In most cases, hardware and software failures are handled through a data backup regimen. Most databases come with built-in tools to automate the entire process, making this aspect of the job relatively painless and error-free. What's not so simple, however, is the second half of the puzzle: making sure that outside hackers can't get into the system and either steal or damage the information contained therein. And unfortunately, there usually isn't an automated way to solve this problem; rather, it requires you, the administrator, to manually put in place roadblocks and obstacles to trip up would-be hackers and to ensure that your company's data stays secure.

A common reason for not securing databases is that it is "difficult" and "complicated". While this is certainly true, if you're using MySQL, there are some easy things you can do to significantly reduce the risk you face. This tutorial lists six such items, but you can find many more in the MySQL manual and discussion forums.

Step 1: Remove wildcards in the grant tables


The MySQL access control system works through a series of so-called grant tables, which make it possible to define access levels for each user at the database, table or column level. While these tables do allow administrators to set blanket permissions for a user or set of tables using wildcards, doing so is inherently dangerous because a hacker could use a single compromised account to gain access to other parts of the system. For this reason, be precise when assigning users privileges and always ensure that users have only as much access as they need to perform their tasks. In particular, be wary of assigning the SUPER privilege to individual users, as this level allows users to manipulate basic server configuration and access all databases.

Tip: Use the SHOW PRIVILEGES command for each user account to audit your grant tables and see if the use of wildcard permissions is appropriate.

Step 2: Require the use of secure passwords


User accounts are only as secure as the passwords used to protect them. For this reason, the very first thing you should do when you install MySQL is assign a password to the MySQL root account (empty by default). Once you've closed this gaping hole, the next step is to require that every user account must have a password and ensure that passwords do not use easily-recognisable heuristics such as birthdays, user names or dictionary words.

Tip: Use the MySQL --secure-auth option to prevent the use of older, less secure MySQL password formats.

Step 3: Check the permissions of configuration files


Very often, to make server connections faster and more convenient, both individual users and server administrators store their user account passwords in their per-user MySQL options file. However, this password is stored in plain-text within the file and can easily be read. Therefore, it's important to ensure that such per-user configuration files are not viewable by other users of the system, and are stored in non-public locations. Ideally, you'd want the per-user configuration to be stored in the user's home directory with permissions 0600.

Step 4: Encrypt client-server transmissions


An important issue in the MySQL (and any) client-server architecture involves the security of data being transmitted over the network. If client-server transactions take place in plaintext, it is possible for a hacker to "sniff" the data packets being transmitted and thus gain access to sensitive information. You can close this hole either by enabling SSL in your MySQL configuration, or by using a secure shell utility like OpenSSH to create a secure encrypted "tunnel" for your data to pass through. Encrypting your client-server connection in this manner makes it extremely hard for unauthorised users to read the data going back and forth.

Step 5: Disable remote access


If your users don't need to access the server remotely, you can significantly reduce the risk of a network attack by forcing all MySQL connections to take place via the UNIX socket file. This is accomplished by starting the server with the --skip-networking option. Doing this blocks TCP/IP network connections to MySQL and ensures that no user can remotely connect to the system.

Tip: An enhancement to this would be to add the directive bind-address=127.0.0.1 in your MySQL server configuration, to force MySQL to bind to the IP address of the local machine and thus ensure that only users on the same system can connect to MySQL.

Step 6: Actively monitor the MySQL access log


MySQL comes with a number of different log files, which keep track of client connections, queries and server errors. Of these, the most important is the general query log, which logs each client connection and disconnection with a timestamp, and also records each query executed by a client. If you suspect unusual activity, such as that associated with a network break-in attempt, it's a good idea to monitor this log to gain an understanding of the source of the activity.

Protecting your MySQL databases is an ongoing task, and so you shouldn't rest easy once you've done the steps above. Visit the MySQL manual and the MySQL forums for more security tips, and be proactive in monitoring and updating the security of your system. Good luck!

Advertisement

 Do you need help with MySQL? Gain advice from Builder AU forums

  • Comments

1

Jon - 07/08/06

This should have been included:

In your applications (If you have control over how they store data), store the SHA-1 or Whirlpool Hash of sensitive data and not the actual data itself. Learn how to use cryptographic hashes or Message Authentication Codes!

You can't compromise data that you don't have :)

» Report offensive content

2

Ben - 07/08/06

Some great information
Links to 'how-to' perform the above tasks would have been very helpfull.

» Report offensive content

3

Dmitriy Kropivnitskiy - 08/08/06

Well, the last suggestion is sometimes a bit difficult to implement. The general query log is very nice, but the DB server I administer, runs at 500-800 queries a second, so an attempt to log all the queries and all the sessions will only result in hogging the I/O to a halt (I tried). Even trying to get a sample of activity is not trivial, since you cannot turn query log on and off dynamically, it takes a restart, so on a production MySQL server it is more or less out of the question.

» Report offensive content

4

DS - 08/08/06

Something wrong with the docs? It's all in there.

http://dev.mysql.com/doc/refman/5.1/en/index.html

» Report offensive content

5

gokcen - 29/08/06

web applications also compromise database security by allowing injections. attacker may use the input fields to run undesired delete, update queries if must-do filtering is not done.
therefore, it's a good idea to run the scripts with limited permissions. if a script only displays records, why give it delete-update rights.

» Report offensive content

6

Raja - 01/09/06

i need a code for accessing a remote server for mysql in .net , when i am connecting it throughs a socket connection error.how to rectify it

» Report offensive content

7

khalil - 11/10/06

Disable remote access
what you mean that mysql is weak
I want build e-Commerce
please explain to more

» Report offensive content

8

nelson - 04/12/06

could u email to me a java program that links mysql?a program that use an edit,delete,save,find,previous,next and clear?

» Report offensive content

9

Zenlogic.com.au - 02/03/07

It is also wise to consider restricting the hosts a user can connect from, rather than the common * [all] hosts setting. For a web application 'user' this should be mandatory.

» Report offensive content

10

ravi.bachwala - 19/10/07

hi ,

i am using mysql database but i have code of MS sql script for creating procedure in mysql...

i facing the problem while creating the stored procedure or do u have any conveter which can convert MS SQL script into mysql script.

can any body help out

regards,
Ravi

CODE in MS SQL:  
USE [Forums]
GO
/****** Object: StoredProcedure [dbo].[deleteUser] Script Date: 10/19/2007 14:45:42 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

create procedure [dbo].[deleteUser]
(
@userID as int
)
as
delete from f_users where id = @userID
delete from f_blogEntry where userID = @userID
delete from f_blogCategory where userID = @userID
delete from f_avatar where userID = @userID
delete from f_forumModerator where userID = @userID
delete from f_forumSubscription where userID = @userID
delete from f_subscription where userID = @userID
delete from f_userGroupUser where userID = @userID




CODE in MYSQL :

create procedure `forums`.`deleteUser`
(
$userID int
)
BEGIN
delete from f_users where id = @userID
delete from f_blogEntry where userID = @userID
delete from f_blogCategory where userID = @userID
delete from f_avatar where userID = @userID
delete from f_forumModerator where userID = @userID
delete from f_forumSubscription where userID = @userID
delete from f_subscription where userID = @userID
delete from f_userGroupUser where userID = @userID

END;

» Report offensive content

11

Massimiliano - 10/01/08

Can I can crypto data into records of mysql?

» Report offensive content

  • Leave A Comment

(if you need to share some code)

You must read and type the 6 chars within 0..9 and A..F

* mandatory fields.

  • Latest comments

11

Massimiliano - 01/10/08

Can I can crypto data into records of mysql? ... more

10

ravi.bachwala - 19/10/07

hi , i am using mysql database but i have code of MS sql script for creating procedure in mysql... i facing the ... more

9

Zenlogic.com.au - 03/02/07

It is also wise to consider restricting the hosts a user can connect from, rather than the common * [all] hosts ... more

Log in


Sign up | Forgot your password?

  • Blogs

Brendon ChaseHow to manage a team of geniuses
Hiring a team of developers and techies that are smarter than you is inevitable. As a manager how do you cope with this and keep things on track? Read more »

-- posted by Brendon Chase

StaffXO to run XP
When Bill Gates says that everything in the world should be a computer, what he means is that everything in the world should be running Windows. Read more »

-- posted by Staff

StaffDrop in on Builder AU at Open CeBiT 2008
Got a question on open source you need answered? Need a way to help convince your boss that open source is the way to go? Or just curious to learn what all the fuss is about? Then drop in to the Builder AU Open Source Afternoon on Wednesday May 21. Read more »

-- posted by Staff

  • What's On?

Club Builder: Vapour-where? Club Builder: Vapour-where?
Applets are back from the dead, but do we want them? This week's Club Builder also looks at which pieces of software are utter vapour.

Understanding task and data parallelism
The difference between task and data parallelism, and how there is a way around the limits imposed by Amdahl's Law.