The MySQL installation process involves initializing the data directory, including the grant tables in themysql system schema that define MySQL accounts. For details, seeSection 9.1, “Initializing the Data Directory”.
This section describes how to assign a password to the initialroot account created during the MySQL installation procedure, if you have not already done so.
Alternative means for performing the process described in this section:
On Windows, you can perform the process during installation with MySQL Installer (seeSection 5.3, “MySQL Installer for Windows”).
On all platforms, the MySQL distribution includesmysql_secure_installation, a command-line utility that automates much of the process of securing a MySQL installation.
On all platforms, MySQL Workbench is available and offers the ability to manage user accounts (seeMySQL Workbench ).
A password may already be assigned to the initial account under these circumstances:
On Windows, installations performed using MySQL Installer give you the option of assigning a password.
Installation using the macOS installer generates an initial random password, which the installer displays to the user in a dialog box.
Installation using RPM packages generates an initial random password, which is written to the server error log.
Installations using Debian packages give you the option of assigning a password.
For data directory initialization performed manually usingmysqld --initialize,mysqld generates an initial random password, marks it expired, and writes it to the server error log. SeeSection 9.1, “Initializing the Data Directory”.
Themysql.user grant table defines the initial MySQL user account and its access privileges. Installation of MySQL creates only a'root'@'localhost' superuser account that has all privileges and can do anything. If theroot account has an empty password, your MySQL installation is unprotected: Anyone can connect to the MySQL server asrootwithout a password and be granted all privileges.
The'root'@'localhost' account also has a row in themysql.proxies_priv table that enables granting thePROXY privilege for''@'', that is, for all users and all hosts. This enablesroot to set up proxy users, as well as to delegate to other accounts the authority to set up proxy users. SeeProxy Users.
To assign a password for the initial MySQLroot account, use the following procedure. Replaceroot-password in the examples with the password that you want to use.
Start the server if it is not running. For instructions, seeSection 9.2, “Starting the Server”.
The initialroot account may or may not have a password. Choose whichever of the following procedures applies:
If the
rootaccount exists with an initial random password that has been expired, connect to the server asrootusing that password, then choose a new password. This is the case if the data directory was initialized usingmysqld --initialize, either manually or using an installer that does not give you the option of specifying a password during the install operation. Because the password exists, you must use it to connect to the server. But because the password is expired, you cannot use the account for any purpose other than to choose a new password, until you do choose one.If you do not know the initial random password, look in the server error log.
Connect to the server as
rootusing the password:$> mysql -u root -pEnter password:(enter the random root password here)Choose a new password to replace the random password:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root-password';
If the
rootaccount exists but has no password, connect to the server asrootusing no password, then assign a password. This is the case if you initialized the data directory usingmysqld --initialize-insecure.Connect to the server as
rootusing no password:$> mysql -u root --skip-passwordAssign a password:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root-password';
After assigning theroot account a password, you must supply that password whenever you connect to the server using the account. For example, to connect to the server using themysql client, use this command:
$> mysql -u root -pEnter password:(enter root password here)To shut down the server withmysqladmin, use this command:
$> mysqladmin -u root -p shutdownEnter password:(enter root password here) For additional information about setting passwords, seeAssigning Account Passwords. If you forget yourroot password after setting it, seeHow to Reset the Root Password.
To set up additional accounts, seeAdding Accounts, Assigning Privileges, and Dropping Accounts.