How to Instal Mysql in linux
MySQL is freely available for Linux from http://www.mysql.com/. Download the
latest stable release (listed as recommended on the download page); as of this
writing this is MySQL 3.23.54a, which you'll find at http://www.mysql.com/downloads/
mysql-3.23.html. You should grab the Linux (x86, libc6) version under Binary
packages in the Linux downloads section.
With the program downloaded (it was about 9.4MB as of this writing), you
should make sure you're logged in as root before proceeding with the installation,
unless you only want to install MySQL in your own home directory. To begin,
move to /usr/local (unless you want to install MySQL elsewhere for some
reason) and unpack the downloaded file to create the MySQL directory (replace
version with the full version of your MySQL download to match the downloaded
file name on your system):
17
Installing MySQL
shell#cd /usr/local
shell#tar xfz mysql-version.tar.gz
Next, create a symbolic link to the mysql-version directory with the name mysql
to make accessing the directory easier, then enter the directory:
shell#ln -s mysql-version mysql
shell#cd mysql
MySQL is now installed, but before it can do anything useful its database files
need to be installed too. Still in the new mysql directory, type the following
command:
shell#scripts/mysql_install_db
With MySQL installed and ready to store information, all that's left is to get the
server running on your computer. While you can run the server as the root user,
or even as yourself (if, for example, you installed the server in your own home
directory), the best idea is to set up on the system a special user whose sole purpose
is to run the MySQL server. This will remove any possibility of someone
using the MySQL server as a way to break into the rest of your system. To create
a special MySQL user, you'll need to log in as root and type the following commands:
shell#groupadd mysql
shell#useradd -g mysql mysql
By default, MySQL stores all database information in the data subdirectory of
the directory to which it was installed4. We want to make it so that nobody can
access that directory except our new MySQL user. Still assuming you installed
MySQL to the /usr/local/mysql directory, you can use these commands:
shell#cd /usr/local/mysql
shell#chown -R mysql data
shell#chgrp -R mysql .
shell#chmod -R go-rwx data
Now everything's set for you to launch the MySQL server for the first time. From
the MySQL directory, type the following command:
shell#bin/safe_mysqld --user=mysql &
4Until recently, it used the var subdirectory.
18
Installation
If you see the message mysql daemon ended, then the MySQL server was prevented
from starting. The error message should have been written to a file called
hostname.err (where hostname is your machine's host name) in MySQL's data
directory. You'll usually find that this happens because another MySQL server
is already running on your computer.
If the MySQL server was launched without complaint, the server will run (just
like your Web or FTP server) until your computer is shut down. To test that the
server is running properly, type the following command:
shell#bin/mysqladmin -u root status
A little blurb with some statistics about the MySQL server should be displayed.
If you receive an error message, something has gone wrong. Again, check the
hostname.err file to see if the MySQL server output an error message while
starting up. If you retrace your steps to make sure you followed the process described
above, and this doesn't solve the problem, a post to the SitePoint Forumsx
will help you pin it down in no time.
If you want your MySQL server to run automatically whenever the system is
running (just like your Web server probably does), you'll have to set it up to do
so. In the support-files subdirectory of the MySQL directory, you'll find a
script called mysql.server that can be added to your system start-up routines
to do this.
First of all, assuming you've set up a special MySQL user to run the MySQL
server, you'll need to tell the MySQL server to start as that user by default. To
do this, create in your system's /etc directory a file called my.cnf that contains
these two lines:
[mysqld]
user=mysql
Now, when you run safe_mysqld or mysql.server to start the MySQL server,
it will launch as user mysql automatically. You can test this by stopping MySQL,
and then running mysql.server with the start argument:
shell#bin/mysqladmin -u root shutdown
shell#chmod u+x support-files/mysql.server
shell#support-files/mysql.server start
xhttp://www.sitepointforums.com/
19
Installing MySQL
Dealing with “@HOSTNAME@: command not found”
In recent versions of MySQL as of this writing, mysql.server may spit out an error
message along the lines of “@HOSTNAME@: command not found”. This error is the result
of a bug in the binary distribution of MySQL for Linux, and can be easily remedied. Simply
open mysql.server in your favourite text editor and find the single occurrence of the
string @HOSTNAME@ in the file. Replace it with /bin/hostname, to point to the program on
your server that will output the machine's host name. Save that change, shutdown MySQL
again, and try starting it using mysql.server start. This time, it should work.
Request the server's status using mysqladmin as before to make sure it's running
correctly.
All that's left to do is to set up your system to run mysql.server automatically
at start-up (to launch the server) and at shutdown (to terminate the server). This
is a highly operating system-dependant task. If you're not sure of how to do it,
you'd be best to ask someone who knows. The following commands, however,
will do the trick for most versions of Linux:
shell#cp /usr/local/mysql/support-files/mysql.server /etc/init.d/
shell#cd /etc/init.d
shell#chmod 755 mysql.server
shell#cd /etc/rc2.d
shell#ln -s ../init.d/mysql.server S99mysql
shell#cd /etc/rc3.d
shell#ln -s ../init.d/mysql.server S99mysql
shell#cd /etc/rc5.d
shell#ln -s ../init.d/mysql.server S99mysql
shell#cd /etc/rc0.d
shell#ln -s ../init.d/mysql.server K01mysql
That's it! To test that this works, reboot your system and request the status of
the server as before.
One final thing you might like to do for convenience's sake is to place the MySQL
client programs, which you'll use to administer your MySQL server later on, in
the system path. To this end, you can place symbolic links to mysql, mysqladmin,
and mysqldump in your /usr/local/bin directory:
shell#ln -s /usr/local/mysql/bin/mysql /usr/local/bin/mysql
shell#ln -s /usr/local/mysql/bin/mysqladmin
/usr/local/bin/mysqladmin
shell#ln -s /usr/local/mysql/bin/mysqldump
/usr/local/bin/mysqldump
latest stable release (listed as recommended on the download page); as of this
writing this is MySQL 3.23.54a, which you'll find at http://www.mysql.com/downloads/
mysql-3.23.html. You should grab the Linux (x86, libc6) version under Binary
packages in the Linux downloads section.
With the program downloaded (it was about 9.4MB as of this writing), you
should make sure you're logged in as root before proceeding with the installation,
unless you only want to install MySQL in your own home directory. To begin,
move to /usr/local (unless you want to install MySQL elsewhere for some
reason) and unpack the downloaded file to create the MySQL directory (replace
version with the full version of your MySQL download to match the downloaded
file name on your system):
17
Installing MySQL
shell#cd /usr/local
shell#tar xfz mysql-version.tar.gz
Next, create a symbolic link to the mysql-version directory with the name mysql
to make accessing the directory easier, then enter the directory:
shell#ln -s mysql-version mysql
shell#cd mysql
MySQL is now installed, but before it can do anything useful its database files
need to be installed too. Still in the new mysql directory, type the following
command:
shell#scripts/mysql_install_db
With MySQL installed and ready to store information, all that's left is to get the
server running on your computer. While you can run the server as the root user,
or even as yourself (if, for example, you installed the server in your own home
directory), the best idea is to set up on the system a special user whose sole purpose
is to run the MySQL server. This will remove any possibility of someone
using the MySQL server as a way to break into the rest of your system. To create
a special MySQL user, you'll need to log in as root and type the following commands:
shell#groupadd mysql
shell#useradd -g mysql mysql
By default, MySQL stores all database information in the data subdirectory of
the directory to which it was installed4. We want to make it so that nobody can
access that directory except our new MySQL user. Still assuming you installed
MySQL to the /usr/local/mysql directory, you can use these commands:
shell#cd /usr/local/mysql
shell#chown -R mysql data
shell#chgrp -R mysql .
shell#chmod -R go-rwx data
Now everything's set for you to launch the MySQL server for the first time. From
the MySQL directory, type the following command:
shell#bin/safe_mysqld --user=mysql &
4Until recently, it used the var subdirectory.
18
Installation
If you see the message mysql daemon ended, then the MySQL server was prevented
from starting. The error message should have been written to a file called
hostname.err (where hostname is your machine's host name) in MySQL's data
directory. You'll usually find that this happens because another MySQL server
is already running on your computer.
If the MySQL server was launched without complaint, the server will run (just
like your Web or FTP server) until your computer is shut down. To test that the
server is running properly, type the following command:
shell#bin/mysqladmin -u root status
A little blurb with some statistics about the MySQL server should be displayed.
If you receive an error message, something has gone wrong. Again, check the
hostname.err file to see if the MySQL server output an error message while
starting up. If you retrace your steps to make sure you followed the process described
above, and this doesn't solve the problem, a post to the SitePoint Forumsx
will help you pin it down in no time.
If you want your MySQL server to run automatically whenever the system is
running (just like your Web server probably does), you'll have to set it up to do
so. In the support-files subdirectory of the MySQL directory, you'll find a
script called mysql.server that can be added to your system start-up routines
to do this.
First of all, assuming you've set up a special MySQL user to run the MySQL
server, you'll need to tell the MySQL server to start as that user by default. To
do this, create in your system's /etc directory a file called my.cnf that contains
these two lines:
[mysqld]
user=mysql
Now, when you run safe_mysqld or mysql.server to start the MySQL server,
it will launch as user mysql automatically. You can test this by stopping MySQL,
and then running mysql.server with the start argument:
shell#bin/mysqladmin -u root shutdown
shell#chmod u+x support-files/mysql.server
shell#support-files/mysql.server start
xhttp://www.sitepointforums.com/
19
Installing MySQL
Dealing with “@HOSTNAME@: command not found”
In recent versions of MySQL as of this writing, mysql.server may spit out an error
message along the lines of “@HOSTNAME@: command not found”. This error is the result
of a bug in the binary distribution of MySQL for Linux, and can be easily remedied. Simply
open mysql.server in your favourite text editor and find the single occurrence of the
string @HOSTNAME@ in the file. Replace it with /bin/hostname, to point to the program on
your server that will output the machine's host name. Save that change, shutdown MySQL
again, and try starting it using mysql.server start. This time, it should work.
Request the server's status using mysqladmin as before to make sure it's running
correctly.
All that's left to do is to set up your system to run mysql.server automatically
at start-up (to launch the server) and at shutdown (to terminate the server). This
is a highly operating system-dependant task. If you're not sure of how to do it,
you'd be best to ask someone who knows. The following commands, however,
will do the trick for most versions of Linux:
shell#cp /usr/local/mysql/support-files/mysql.server /etc/init.d/
shell#cd /etc/init.d
shell#chmod 755 mysql.server
shell#cd /etc/rc2.d
shell#ln -s ../init.d/mysql.server S99mysql
shell#cd /etc/rc3.d
shell#ln -s ../init.d/mysql.server S99mysql
shell#cd /etc/rc5.d
shell#ln -s ../init.d/mysql.server S99mysql
shell#cd /etc/rc0.d
shell#ln -s ../init.d/mysql.server K01mysql
That's it! To test that this works, reboot your system and request the status of
the server as before.
One final thing you might like to do for convenience's sake is to place the MySQL
client programs, which you'll use to administer your MySQL server later on, in
the system path. To this end, you can place symbolic links to mysql, mysqladmin,
and mysqldump in your /usr/local/bin directory:
shell#ln -s /usr/local/mysql/bin/mysql /usr/local/bin/mysql
shell#ln -s /usr/local/mysql/bin/mysqladmin
/usr/local/bin/mysqladmin
shell#ln -s /usr/local/mysql/bin/mysqldump
/usr/local/bin/mysqldump
Labels: Server