After I setup a web application server, I would like to use a database for storing my data. MySQL is a free open source software that I am familiar with. For flexibility, I choose to install the binary non-rpm tar.gz version.
1) I download MySQL binary non-rpm tar.gz version for my AMD ET64T machine. The file is mysql-5.0.45-linux-x86_64-glibc23.tar.gz.
2) For a good practice, I verify the downloaded file using md5sum that is already installed in Debian Etch system. So, I issue the command.
>md5sum -b mysql-5.0.45-linux-x86_64-glibc23.tar.gz
Then, I compare the result with what on the MySQL download web site for this particular version.
3) If everything is ok, I unzip the file and move it to its installation directory such as /opt. Well, first of all, I have to become root.
>tar -zxvf mysql-5.0.45-*.tar.gz
>mv mysql-5.0.45*glibc23 /opt/
4) Then, I change my working directory to /opt/mysql-5.0.45*glibc23
5) I open “INSTALL-BINARY” file and follow the installation instructions in this file. However, since I want to put the mysql data directory in the /home directory because of space availability reason, I need to edit MySQL configuration file and put the config file in /etc directory (global configuration). Because I plan to use MySQL database for my web application, I use my-medium.cnf file.
So, I copy my-medium.cnf in the $MYSQL_HOME/support-files directory to /etc directory
>cp my-medium.cnf /etc/my.cnf (updated on Feb 22, 2010)
Then, I edit my.cnf file and put the following text under [mysqld] directive to tell MySQL server where the data directory is.
Look for [mysqld] and add the following text line.
6) Now, I get back to the $MYSQL_HOME directory or /opt/mysql-5.0.45-linux-x86_64-glibc23
7) I begin to follow the instructions in the “INSTALL-BINARY” file as follows.
– Add a new group named “mysql”.
– Add a new user “mysql” to “mysql” group.
>useradd -g mysql mysql
– Make a symbolic link from the $MYSQL_HOME to “mysql” link. So, this will make our life easier to refer to $MYSQL_HOME by using only /opt/mysql rather than using its full name “/opt/mysql-5.0.45-linux-x86_64-glibc23”
>ln -s /opt/mysql-5.0.45-linux-x86_64-glibc23 mysql
– Then, I get back to inside the $MYSQL_HOME
– I change owner of all files and directories (-R) inside $MYSQL_HOME to mysql.
>chown -R mysql .
– I change group owner of all files and directories (-R) inside &MYSQL_HOME to mysql group.
>chgrp -R mysql .
– Since I want to put MySQL data directory in /home directory and I already specify it as “mysql_data” in /etc/my.cnf file, I move the data directory in $MYSQL_HOME to /home directory and change its name to “mysql_data”.
>mv data /home/mysql_data
– Then, I am ready to install database in MySQL server.
For security reasons, everything inside $MYSQL_HOME should be owned by root except the data directory. Therefore, I change owner of all files and directories inside $MYSQL_HOME to root.
>chown -R root .
Since the /home/mysql_data is already owned by ‘mysql’ user and group, I do not need to do anything else.
8) As root, I start MySQL server as follows.
>bin/mysqld_safe –user=mysql &
Press Enter to get back to the command prompt or open a new terminal.
9) I open a new terminal. As a normal user, I enter the following command to activate MySQL client.
10) I issues some commands to test MySQL server and client installations.
11) I quit MySQL client
12) I stop MySQL server. As root.
That’s all for MySQL installation in Debian Etch. Next, I will install connector/j for using MySQL in Java programs.
Any comment or suggestion is very welcomed.
[Update: October 22, 2007] To secure MySQL database, I do the following steps.
1) I assign root’s password
– Start MySQL server
– Start MySQL client
– Assign root password
mysql>SET PASSWORD FOR root@localhost=PASSWORD(‘new_password’);
2) Then, I remove the ‘test’ database. Note that there are existing databases after a fresh MySQL installation, which are ‘information_schema’, ‘mysql’, and ‘test’.
mysql>drop database test;
3) I delete records from the ‘db’ table in the ‘mysql’ database.
mysql>delete from db;
4) I remove all users except root from the ‘user’ table in the ‘mysql’ database.
mysql>delete from user where not (host=”localhost” and user=”root”);
5) I quit MySQL client and shutdown MySQL server.
>bin/mysqladmin -uroot -p<new_password> shutdown.
Note that we need to supply username and password for shutting down MySQL server now.
6) I try to restart MySQL server and client again
>bin/mysql -uroot -p<new_password>
Note that the passwords for shutting down MySQL server and connect to MySQL client are the same one.