nav-left cat-right
cat-right

MySQL

3.4 MySQL

3.4.1 Install MySQL

I install MySQL from the rpms released on the MySQL website as they are the most up to date. Installing from source has little advantage for MySQL as it is already heavily optimized.

Create a place to put our downloaded RPMs:

  • sudo su -
  • mkdir /usr/src/mysql
  • cd /usr/src/mysql

Now head over to http://dev.mysql.com/downloads/mysql to find the download links for the three packages you'll want. Since we're working with CentOS 4 here, you'll want the Redhat Enterprise Linux 4 RPMs. You have to go through a couple of annoying screens to get to each mirrored download.

Download the Server, Client and Headers and libraries packages. The rpms you should end up with are named MySQL-server-community-[version].rhel4.[architecture].rpm, MySQL-client-community-[etc], and MySQL-devel-community-[etc]. Place them all in /usr/src/mysql.

Now, we need to get the MySQL GPG key to verify these RPMs before we can install them. Go to the MySQL manual at http://dev.mysql.com/doc/refman/4.1/en/checking-gpg-signature.html, and copy the key text on the page in the blue block (they point it out). Paste it into a new file, /usr/src/mysql/mysql_gpg_public_key.asc and save it. Now we'll import the key to our RPM keyring and verify the RPMs:

  • rpm --import mysql_gpg_public_key.asc
  • rpm --checksig MySQL-*

Make sure they all say 'OK' after each check. If so, install them with yum:

  • yum localinstall MySQL-*.rpm

The install creates the mysql user on the system, and places a handy startup script in /etc/init.d/mysql. It also installs MySQL as a service, which means we can start it using service mysql start. Easy as pie.

3.4.2 Configure MySQL

Now run:

  • /usr/bin/mysql_secure_installation

Follow the prompts to set a MySQL root user password and whatever else you'd like to do. Once you have a root password set, you'll want to modify the MySQL config files. There are some example configs found in /usr/share/mysql, called my-large.cnf, my-huge.cnf, etc. Take a look at them. Any half-decent machine will generally fit at least the my-large.cnf file these days. I'm not going to tell exactly you how configure your server, as it's important that you figure out what you need based on what your hardware is like. My general approach is to take the my-huge.cnf file and make slight modifications:

  • sudo su -
  • cp /usr/share/mysql/my-huge.cnf /etc/my.cnf
  • vi /etc/my.cnf

Add or change the following:

thread_cache_size = 16
read_rnd_buffer_size = 1M
#Set to number of CPU's * 2
thread_concurrency = 4

#Slow query logging
log-slow-queries
long_query_time=2

#Kill threads after they've been idle this long
wait_timeout = 300
#Max number of connected clients. Keep it in-line with Apache's MaxClients setting.
#A good rule of thumb is that Total Physical Memory = max_connections * (sort_buffer_size + read_buffer_size) -- key_buffer
max_connections = 160
#This is a per-user connection maximum.
max_user_connections = 160

All done? Start 'er up and get out of your root session.

  • service mysql start
  • exit

Now you can connect to MySQL with mysql -u root -p and start making databases and users. Remember to not use the root user for your web applications!

  • Digg
  • del.icio.us
  • DotNetKicks
  • Slashdot
  • StumbleUpon