Cài đặt MySQL 5.x.x trên Linux

Introduction

These instructions describe the installation of MySQL binaries on Linux. They were written while installing on Red Hat 6.1, but should work for any recent version of Linux. These binaries are optimised for i686 computers, if you are using anything else then you should probably install from source.

Before We Begin

  1. These instructions assume that you are working as root.
        su root
    

Getting MySQL

  1. You can determine the latest stable version of MySQL at the downloads page. For this installation I'm using MySQL 3.23.28.
  2. Download the binary distribution for PC Linux. Save it into your /usr/local directory.
        cd /usr/local
        ftp ftp.mysql.com       (Name: anonymous) (Password: [enter your email address here])
        cd MySQL-3.23
        bin
        get mysql-3.23.28-gamma-pc-linux-gnu-i686.tar.gz
    

Installing MySQL

  1. Unpack the MySQL download.
        tar xzf mysql-3.23.28-gamma-pc-linux-gnu-i686.tar.gz
    
  2. Make life easier by adding a symbolic link to the complex directory name. This also makes it easier in the future if you want to upgrade your MySQL installation.
        ln -s mysql-3.23.28-gamma-pc-linux-gnu-i686 mysql
    

Configuring MySQL

  1. Now we must initialise the standard MySQL databases. This also sets up the initial user privileges for your MySQL server.
        cd mysql
        scripts/mysql_install_db
    

Running the Server Using an Unprivileged User Account

  1. Before we start the server, we want to make an account for the MySQL server. This should really be a seperate account from other users and not on the root. Why? Because this could compromise security. Also, in general, you want to limit the power of any process unless it needs root access, which MySQL does not. First off, we shall add the user.
       /usr/sbin/useradd mysql
    
  2. If the system being used is Red Hat, the next command can be skipped. All other systems have to seperatly define a group for its users, where as Red Hat automatically does this in the last command.
       /usr/sbin/groupadd mysql
    
  3. Now we modify the ownership of the data directory and any subdirectories and files so that the mysql user owns them.
      cd /usr/local/mysql/data
    

    Once the file is found, we now modify the ownership.

     chown -R mysql:mysql . (Make sure the final [space][period], is present, as this specifies which file to modify.)
    
  4. Modify the permissions to the directory and subdirectories and files so that only the mysqladm has access
     chmod -R go-rwx . (Once again, remember the final [space][period].)
    

Starting the Server By Hand

  1. You can start the server with a single command. Give it a moment to start and then press Enter a couple of times to get your command prompt back (the & starts the server in another process).
        cd /usr/local/mysql
        bin/safe_mysqld &
    
  2. It should be obvious whether the MySQL server started properly, but you can use the admin program to check.
       bin/mysqladmin ping
    
    mysqld is alive
    

Securing the Server and Adding Users

  1. The MySQL security and privilege system is extremely powerful and thus quite complicated. My setup below is for a very simple set of users. The MySQL manual explains the details very well. Basically you can configure privileges based on the user name, the user's location (host), the database they are using, and the tables and columns they wish to use.
  2. The initial user setup and security for MySQL is designed for ease of use. That is, completely insecure. The first thing we need to do is give the root user a password. Here I'm setting the root password to be mypass:
        bin/mysqladmin -u root password mypass
    
  3. You can now access the database by giving a password on the command line or entering it when prompted. Notice that there is no space between the -p and the password when connecting.
        bin/mysql -uroot -pmypass
    

    To get prompted, just leave off the password. This is more secure since other users may be able to see your command like that above:

        bin/mysql -uroot -p
    
  4. I generally create one database for each web site. For example, e-gineer has a database set up as follows:
    shell> bin/mysql -uroot -pmypass
    mysql> create database egineer;
    

    Now I create a database user that will be used by PHP to access this database. I use the same name as the database (egineer) for convenience. Notice that this user only has privileges to select, insert, update and delete. They also only have permission to connect from the local machine. Since the password will be in PHP code it's best to keep the privileges for this user as minimal as possible.

    mysql> GRANT SELECT,INSERT,UPDATE,DELETE
               ON egineer.*
               TO egineer@localhost
               IDENTIFIED BY 'mypass';
    
  5. Now the parameters for your PHP code are:
        Host:     localhost
        User:     egineer
        Password: mypass
    

Adding the MySQL commands to Your Path

  1. This section is completely optional. It just sets up your system so the MySQL commands can be run from anywhere like this:
        mysql
    

    instead of having to qualify the full path to the binary file:

        /usr/local/mysql/bin/mysql
    

    Which do you prefer?

  2. If most of the users on the machine use mysql then you might like to make the commands available in the default path. You can do this by editing:
        /etc/profile
    
  3. Simply add the MySQL bin path to the PATH command in this file. I just added a line like this:
        PATH=$PATH:/usr/local/mysql/bin
    

    immediately below the existing command to set the PATH.

  4. If you don't want to make the mysql commands available to everyone, but want to give yourself the convenience add the PATH command to your own .profile. For me, this file is:
        ~/.bash_profile
    

Making MySQL Start Automatically on Boot

It's convenient to have MySQL start automatically with your machine. It's also wise if you have an unattended web server that might be rebooted for reasons beyond your control. These instructions explain how to set it up on Red Hat Linux.