Documentation Home
MySQL 5.7 Reference Manual
Related Documentation Download this Manual
PDF (US Ltr) - 38.0Mb
PDF (A4) - 38.1Mb
PDF (RPM) - 37.4Mb
HTML Download (TGZ) - 10.2Mb
HTML Download (Zip) - 10.2Mb
HTML Download (RPM) - 8.9Mb
Man Pages (TGZ) - 216.6Kb
Man Pages (Zip) - 329.5Kb
Info (Gzip) - 3.5Mb
Info (Zip) - 3.5Mb
Excerpts from this Manual

2.10.1 Initializing the Data Directory

After installing MySQL, you must initialize the data directory, including the tables in the mysql system database. For some MySQL installation methods, data directory initialization may be done automatically, as described in Section 2.10, “Postinstallation Setup and Testing”. For other installation methods, including installation from generic binary and source distributions, you must initialize the data directory yourself.

This section describes how to initialize the data directory on Unix and Unix-like systems. (For Windows, see Section 2.3.7, “Windows Postinstallation Procedures”.) For some suggested commands that you can use to test whether the server is accessible and working properly, see Section 2.10.3, “Testing the Server”.

In the examples shown here, the server runs 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, or substitute the name of a different existing login account that you plan to use for running the server. For information about creating the account, see Creating a mysql System User and Group, in Section 2.2, “Installing MySQL on Unix/Linux Using Generic Binaries”.

  1. Change location into the top-level directory of your MySQL installation, represented here by BASEDIR:

    shell> cd BASEDIR

    BASEDIR is likely to be something like /usr/local/mysql or /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 is the bin subdirectory, which contains the server as well as client and utility programs.

  2. Create a directory that provides a location to use as the value of the secure_file_priv system variable that limits import/export operations to a specific directory. See Section 5.1.5, “Server System Variables”.

    shell> mkdir mysql-files
    shell> chmod 750 mysql-files
  3. 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 mysql by executing the following commands as root in the installation directory. The first command changes the owner attribute of the files to the mysql user. The second changes the group attribute to the mysql group.

    shell> chown -R mysql .
    shell> chgrp -R mysql .
  4. If necessary, initialize the data directory, including the mysql database 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. If you are upgrading an existing installation, you should run mysql_upgrade instead (see Section 4.4.7, “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.

    As of MySQL 5.7.6, use the server to initialize the data directory:

    shell> bin/mysqld --initialize --user=mysql

    Before MySQL 5.7.6, use mysql_install_db:

    shell> bin/mysql_install_db --user=mysql

    For more information, see Section, “Initializing the Data Directory Manually Using mysqld”, or Section, “Initializing the Data Directory Manually Using mysql_install_db”, depending on which command you use.


    Initialization of the data directory might fail because some required software libraries are missing from your system. For example:

    shell> bin/mysqld --initialize --user=mysql
    bin/mysqld: error while loading shared libraries: cannot
    open shared object file: No such file or directory

    When this happens, you have to install the missing libraries manually or with your system's package manager before retrying the data directory initialization.

  5. If you want the server to be able to deploy with automatic support for secure connections, use the mysql_ssl_rsa_setup utility to create default SSL and RSA files:

    shell> mysql_ssl_rsa_setup

    For more information, see Section 4.4.5, “mysql_ssl_rsa_setup — Create SSL/RSA Files”.

  6. 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 root if you like. The exception is that the data directory and the mysql-files directory must be owned by mysql. To 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 accordingly.

    shell> chown -R root .
    shell> chown -R mysql data mysql-files

    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 the plugin directory read only to the server or by setting the secure_file_priv system variable at server startup to a directory where SELECT writes can be performed safely. (For example, set it to the mysql-files directory created earlier.)

  7. To specify options that the MySQL server should use at startup, put them in a /etc/my.cnf or /etc/mysql/my.cnf file. You can use such a file, for example, to set the secure_file_priv system variable. See Section 5.1.2, “Server Configuration Defaults”. If you do not do this, the server starts with its default settings.

  8. If you want MySQL to start automatically when you boot your machine, see Section 2.10.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 Section 10.6, “MySQL Server Time Zone Support”.

User Comments
Sign Up Login You must be logged in to post a comment.