After installing MySQL on Unix, you must initialize the grant tables, start the server, and make sure that the server works satisfactorily. You may also wish to arrange for the server to be started and stopped automatically when your system starts and stops. You should also assign passwords to the accounts in the grant tables.
On Unix, the grant tables are set up by the mysql_install_db program. For some installation methods, this program is run for you automatically if an existing database cannot be found.
For other platforms and installation types, including generic binary and source installs, you will need to run mysql_install_db yourself.
The following procedure describes how to initialize the grant tables (if that has not previously been done) and start the server. It also suggests some commands that you can use to test whether the server is accessible and working properly. For information about starting and stopping the server automatically, see Section 220.127.116.11, “Starting and Stopping MySQL Automatically”.
After you complete the procedure and have the server running, you should assign passwords to the accounts created by mysql_install_db and perhaps tighten access to test databases. For instructions, see Section 2.10.3, “Securing the Initial MySQL Accounts”.
In the examples shown here, the server runs under the user ID of
mysql login account. This assumes that such
an account exists. Either create the account if it does not exist,
or substitute the name of a different existing login account that
you plan to use for running the server.
Change location into the top-level directory of your MySQL
installation, represented here by
BASEDIR is the installation
directory for your MySQL instance. It is likely to be
/usr/local. The following steps assume
that you have changed location to this directory.
You will find several files and subdirectories in the
BASEDIR directory. The most
important for installation purposes are the
bin directory contains client
programs and the server. You should add the full path name
of this directory to your
environment variable so that your shell finds the MySQL
programs properly. See
Section 2.13, “Environment Variables”.
For some distribution types, mysqld is
scripts directory contains the
mysql_install_db script used to
containing the grant tables that store the server access
For some distribution types,
mysql_install_db is in the
If necessary, ensure that the distribution contents are
mysql. If you unpacked the
mysql, no further action is
required. If you unpacked the distribution as
root, its contents will be owned by
root. Change its ownership to
mysql by executing the following commands
root in the installation directory:
chown -R mysql .shell>
chgrp -R mysql .
The first command changes the owner attribute of the files to
mysql user. The second changes the
group attribute to the
If necessary, run the mysql_install_db program to set up the initial MySQL grant tables containing the privileges that determine how users are permitted to connect to the server. You will need to do this if you used a distribution type for which the installation procedure does not run the program for you.
Typically, mysql_install_db needs to be run only the first time you install MySQL, so you can skip this step if you are upgrading an existing installation, However, mysql_install_db does not overwrite any existing privilege tables, so it should be safe to run in any circumstances.
The exact location of mysql_install_db will
depends on the layout for your given installation. To
initialize the grant tables, use one of the following
commands, depending on whether
mysql_install_db is located in the
It might be necessary to specify other options such as
mysql_install_db does not identify the
correct locations for the installation directory or data
directory. For example:
bin/mysql_install_db --user=mysql \
The mysql_install_db script creates the
server's data directory with
mysql as the
owner. Under the data directory, it creates directories for
mysql database that holds the grant
tables and the
test database that you can
use to test MySQL. The script also creates privilege table
root and anonymous-user
accounts. The accounts have no passwords initially.
Section 2.10.3, “Securing the Initial MySQL Accounts”, describes the initial
privileges. Briefly, these privileges permit the MySQL
root user to do anything, and permit
anybody to create or use databases with a name of
test or starting with
It is important to make sure that the database directories and
files are owned by the
mysql login account
so that the server has read and write access to them when you
run it later. To ensure this, the
--user option should
be used as shown if you run
root. Otherwise, you should execute the
script while logged in as
mysql, in which
case you can omit the
--user option from
mysql_install_db creates several tables in
mysql database, including
others. See Section 5.5, “The MySQL Access Privilege System”, for a complete
listing and description of these tables.
If you do not want to have the
database, you can remove it after starting the server, using
the instructions in Section 2.10.3, “Securing the Initial MySQL Accounts”.
If you have trouble with mysql_install_db at this point, see Section 18.104.22.168, “Problems Running mysql_install_db”.
For MySQL versions older than 3.22.10, mysql_install_db left the server running after creating the grant tables. This is no longer true; you need to restart the server after performing the remaining steps in this procedure.
Most of the MySQL installation can be owned by
root if you like. The exception is that the
data directory must be owned by
accomplish this, run the following commands as
root in the installation directory. For
some distribution types, the data directory might be named
var rather than
data; adjust the second command
chown -R root .shell>
chown -R mysql data
If the plugin directory (the directory named by the
plugin_dir system variable)
is writable by the server, it may be possible for a user to
write executable code to a file in the directory using
SELECT ... INTO
DUMPFILE. This can be prevented by making
plugin_dir read only to the
server or by setting
--secure-file-priv to a
can be made safely.
If you installed MySQL using a source distribution, you may
want to optionally copy one of the provided configuration
files from the
/etc directory. There are
different sample configuration files for different use cases,
server types, and CPU and RAM configurations. If you want to
use one of these standard files, you should copy it to
/etc/mysql/my.cnf and edit and check the
configuration before starting your MySQL server for the first
If you do not copy one of the standard configuration files, the MySQL server will be started with the default settings.
If you want MySQL to start automatically when you boot your
machine, you can copy
support-files/mysql.server to the
location where your system has its startup files. More
information can be found in the
mysql.server script itself, and in
Section 22.214.171.124, “Starting and Stopping MySQL Automatically”.
Start the MySQL server:
bin/mysqld_safe --user=mysql &
For versions of MySQL older than 4.0, substitute bin/safe_mysqld for bin/mysqld_safe.
It is important that the MySQL server be run using an
root) login account. To
ensure this, the
--user option should be used
as shown if you run mysqld_safe as
root. Otherwise, you should execute the
script while logged in as
mysql, in which
case you can omit the
--user option from the
Further instructions for running MySQL as an unprivileged user are given in Section 5.4.6, “How to Run MySQL as a Normal User”.
If the command fails immediately and prints
ended, you can find some information in the
file in the data directory.
If you neglected to create the grant tables by running mysql_install_db before proceeding to this step, the following message appears in the error log file when you start the server:
mysqld: Can't find file: 'host.frm'
If you have other problems starting the server, see Section 126.96.36.199, “Starting and Troubleshooting the MySQL Server”. For more information about mysqld_safe, see Section 4.3.2, “mysqld_safe — MySQL Server Startup Script”.
Use mysqladmin to verify that the server is running. The following commands provide simple tests to check whether the server is up and responding to connections:
The output from mysqladmin version varies slightly depending on your platform and version of MySQL, but should be similar to that shown here:
bin/mysqladmin versionmysqladmin Ver 14.7 Distrib 4.1.19, for linux on i586 ... Server version 4.1.19-max Protocol version 10 Connection Localhost via Unix socket TCP port 3306 UNIX socket var/lib/mysql/mysql.sock Uptime: 5 days 19 hours 19 min 0 sec Threads: 1 Questions: 163 Slow queries: 0 Opens: 11 Flush tables:1 Open tables: 0 Queries per second avg: 0.007 Threads: 1 Questions: 9 Slow queries: 0
bin/mysqladmin -u root shutdown
bin/mysqld_safe --user=mysql --log &
Run some simple tests to verify that you can retrieve information from the server. The output should be similar to what is shown here:
bin/mysqlshow+-----------+ | Databases | +-----------+ | mysql | | test | +-----------+ shell>
bin/mysqlshow mysqlDatabase: mysql +--------------+ | Tables | +--------------+ | columns_priv | | db | | func | | host | | tables_priv | | user | +--------------+ shell>
bin/mysql -e "SELECT Host,Db,User FROM db" mysql+------+--------+------+ | host | db | user | +------+--------+------+ | % | test | | | % | test_% | | +------+--------+------+
There is a benchmark suite in the
sql-bench directory (under the MySQL
installation directory) that you can use to compare how MySQL
performs on different platforms. The benchmark suite is
written in Perl. It requires the Perl DBI module that provides
a database-independent interface to the various databases, and
some other additional Perl modules:
DBI DBD::mysql Data::Dumper Data::ShowTable
sql-bench/Results directory contains
the results from many runs against different databases and
platforms. To run all tests, execute these commands:
If you do not have the
directory, you probably installed MySQL using RPM files other
than the source RPM. (The source RPM includes the
sql-bench benchmark directory.) In this
case, you must first install the benchmark suite before you
can use it. Beginning with MySQL 3.22, there are separate
benchmark RPM files named
that contain benchmark code and data.
If you have a source distribution, there are also tests in its
tests subdirectory that you can run. For
example, to run
execute this command from the top-level directory of your
mysql -vvf test < ./tests/auto_increment.tst
The expected result of the test can be found in the
At this point, you should have the server running. However, none of the initial MySQL accounts have a password, and the server permits permissive access to test databases. To tighten security, follow the instructions in Section 2.10.3, “Securing the Initial MySQL Accounts”.
As of MySQL 4.1.3, the installation procedure creates time zone
tables in the
mysql database but does not
populate them. To do so, use the instructions in
Section 9.7, “MySQL Server Time Zone Support”.
You can set up new accounts using the
bin/mysql_setpermission script if you install
modules. See Section 4.6.15, “mysql_setpermission — Interactively Set Permissions in Grant
Tables”. For Perl
module installation instructions, see
Section 2.14, “Perl Installation Notes”.
If you would like to use mysqlaccess and have
the MySQL distribution in some nonstandard location, you must
change the location where mysqlaccess expects
to find the mysql client. Edit the
bin/mysqlaccess script at approximately line
18. Search for a line that looks like this:
$MYSQL = '/usr/local/bin/mysql'; # path to mysql executable
On Windows, you can also perform the process described in this section using the Configuration Wizard (see Section 188.8.131.52, “The Security Options Dialog”). On other platforms, the MySQL distribution includes mysql_secure_installation, a command-line utility that automates much of the process of securing a MySQL installation.