Documentation Home
MySQL 5.7 Reference Manual
Related Documentation Download this Manual
PDF (US Ltr) - 38.0Mb
PDF (A4) - 38.0Mb
PDF (RPM) - 37.3Mb
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

MySQL 5.7 Reference Manual  /  ...  /  Initializing the Data Directory Manually Using mysql_install_db

2.10.1.2 Initializing the Data Directory Manually Using mysql_install_db

This section describes how to initialize the data directory using mysql_install_db.

Note

The procedure described here is used on Unix and Unix-like systems prior to MySQL 5.7.6. (For Windows, MySQL distributions include a data directory with prebuilt tables in the mysql database.) As of MySQL 5.7.6, mysql_install_db is deprecated. To initialize the data directory, use the procedure described at Section 2.10.1.1, “Initializing the Data Directory Manually Using mysqld”.

The following instructions assume that your current location is the MySQL installation directory, represented here by BASEDIR:

shell> cd BASEDIR

To initialize the data directory, invoke mysql_install_db. This program is located under the base directory in either bin or scripts, depending on your version of MySQL. If it is in scripts, adjust the following commands appropriately.

shell> bin/mysql_install_db --user=mysql

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, run mysql_install_db as root and include the --user option as shown. Otherwise, execute the program while logged in as mysql, in which case you can omit the --user option from the command.

The mysql_install_db command creates the server's data directory. Under the data directory, it creates directories for the mysql database that holds the grant tables and (prior to MySQL 5.7.4) a test database that you can use to test MySQL. The program also creates privilege table entries for the initial account or accounts. For a complete listing and description of the grant tables, see Section 6.2, “The MySQL Access Privilege System”.

It might be necessary to specify other options such as --basedir or --datadir if mysql_install_db does not identify the correct locations for the installation directory or data directory. For example:

shell> bin/mysql_install_db --user=mysql \
         --basedir=/opt/mysql/mysql \
         --datadir=/opt/mysql/mysql/data

If mysql_install_db generates a random password for the root account, start the server and assign a new password:

  1. Start the server (use the first command if your installation includes mysqld_safe, the second it if includes systemd support):

    shell> bin/mysqld_safe --user=mysql &
    shell> systemctl start mysqld

    Substitute the appropriate service name if it differs from mysqld; for example, mysql on SLES systems.

  2. Look in the $HOME/.mysql_secret file to find the random password that mysql_install_db wrote there. Then connect to the server as root using that password:

    shell> mysql -u root -h 127.0.0.1 -p
    Enter password: (enter the random password here)
  3. After connecting, assign a new root password:

    mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('new_password');

    After resetting the password, remove the .mysql_secret file; otherwise, if you run mysql_secure_installation, that command may see the file and expire the root password again as part of ensuring secure deployment.

If mysql_install_db did not generate a random password, you should still assign one. For instructions, see Section 2.10.4, “Securing the Initial MySQL Accounts”. That section also describes how to remove the test database, if mysql_install_db created one and you do not want it.

If you have trouble with mysql_install_db at this point, see Section 2.10.1.3, “Problems Running mysql_install_db”.


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