After MySQL is installed, the data directory must be initialized,
including the tables in the
For some MySQL installation methods, data directory initialization is automatic, as described in Chapter 3, Postinstallation Setup and Testing.
For other installation methods, you must initialize the data directory manually. These include installation from generic binary and source distributions on Unix and Unix-like systems, and installation from a ZIP Archive package on Windows.
This section describes how to initialize the data directory manually for MySQL installation methods for which data directory initialization is not automatic. For some suggested commands that enable testing whether the server is accessible and working properly, see Section 3.3, “Testing the Server”.
In the examples shown here, the server is intended to run under
the user ID of the
mysql login account. This
assumes that such an account exists. Either create the account if
it does not exist (see
Create a mysql User and Group), or
substitute the name of a different existing login account that you
plan to use for running the server.
Change location to the top-level directory of your MySQL installation, represented here by
BASEDIRis likely to be something like
/usr/local. The following steps assume that you have changed location to this directory.
You will find several files and subdirectories in the
BASEDIRdirectory. The most important for installation purposes are the
scriptssubdirectories, which contain the server as well as client and utility programs.
If necessary, ensure that the distribution contents are accessible to
mysql. If you installed the distribution as
mysql, no further action is required. If you installed the distribution as
root, its contents will be owned by
root. Change its ownership to
mysqlby executing the following commands as
rootin the installation directory. The first command changes the owner attribute of the files to the
mysqluser. The second changes the group attribute to the
chown -R mysql . chgrp -R mysql .
If necessary, initialize the data directory, including the
mysqldatabase containing the initial MySQL grant tables that determine how users are permitted to connect to the server.
Typically, data directory initialization need be done only the first time you install MySQL. For upgrades to an existing installation, you should run mysql_upgrade instead (see mysql_upgrade — Check and Upgrade MySQL Tables). However, the command that initializes the data directory does not overwrite any existing privilege tables, so it should be safe to run in any circumstances. Use the server to initialize the data directory; for example:
It is important to make sure that the database directories and files are owned by the
mysqllogin account so that the server has read and write access to them when you run it later. To ensure this if you run mysql_install_db as
root, include the
--useroption as shown. Otherwise, you should execute the program while logged in as
mysql, in which case you can omit the
--useroption from the command.
The mysql_install_db command creates the server's data directory. Under the data directory, it creates directories for the
mysqldatabase that holds the grant tables and the
testdatabase that you can use to test MySQL. The program also creates privilege table entries for the initial account or accounts.
test_. For a complete listing and description of the grant tables, see Chapter 4, Access Control and Account Management.
It might be necessary to specify other options such as
--datadirif mysql_install_db does not identify the correct locations for the installation directory or data directory. For example (enter the command on a single line):
scripts/mysql_install_db --user=mysql --basedir=/opt/mysql/mysql --datadir=/opt/mysql/mysql/data
If you do not want to have the
testdatabase, you can remove it after starting the server, using the instructions in Section 3.4, “Securing the Initial MySQL Accounts”.
If you have trouble with mysql_install_db at this point, see Section 3.1.1, “Problems Running mysql_install_db”.
After initializing the data directory, you can establish the final installation ownership settings. To leave the installation owned by
mysql, no action is required here. Otherwise, most of the MySQL installation can be owned by
rootif you like. The exception is that the data directory must be owned by
mysql. To accomplish this, run the following commands as
rootin the installation directory. For some distribution types, the data directory might be named
data; adjust the second command accordingly.
chown -R root . chown -R mysql data
If the plugin directory (the directory named by the
plugin_dirsystem 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 the plugin directory read only to the server or by setting the
secure_file_privsystem variable at server startup to a directory where
SELECTwrites can be performed safely.
In the absence of any option files, the server starts with its default settings. (See Server Configuration Defaults.) If you installed MySQL using a source distribution, you may want to optionally copy one of the provided configuration files from the
support-filesdirectory into your
/etcdirectory. There are different sample configuration files for different use cases, server types, and CPU and RAM configurations. To use one of these standard files, copy it to
/etc/mysql/my.cnfand edit and check the configuration before starting your MySQL server for the first time.
You can also create
my.cnfyourself and place into it the options the server should use at startup. (See Using Option Files.) For example, you can use an option file to set the
If you do not copy one of the standard configuration files or create your own, the MySQL server starts with its default settings.
To arrange for MySQL to start automatically at system boot time, see Section 3.5, “Starting and Stopping MySQL Automatically”.
Data directory initialization creates time zone tables in the
mysql database but does not populate them. To
do so, use the instructions in
MySQL Server Time Zone Support.