In this guide, you will learn how to install MySQL in Ubuntu Linux, and then how to set MySQL
root user password, and finally how to access MySQL server as
All steps given below are tested on a freshly installed Ubuntu 22.04 LTS server edition.
Before installing MySQL, make sure your Ubuntu system is updated with latest packages:
$ sudo apt update
$ sudo apt upgrade
Once all updates are installed, reboot your system:
$ sudo reboot
1. Install MySQL in Ubuntu Linux
MySQL is available in the default repositories of Ubuntu Linux. To install MySQL in Ubuntu Linux, run:
$ sudo apt install mysql-server
This command installs all necessary components to setup a MySQL database server in Ubuntu.
To check the MySQL version, run:
$ mysql --version
Or use capital
$ mysql -V
mysql Ver 8.0.29-0ubuntu0.22.04.2 for Linux on x86_64 ((Ubuntu))
1.1. Start, restart and stop MySQL service
In Ubuntu, MySQL service is automatically started once the MySQL server is installed. To verify if the MySQL service is started or not, run:
$ sudo systemctl status mysql
* mysql.service - MySQL Community Server Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2022-05-09 07:47:00 UTC; 15min ago Process: 1217 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS) Main PID: 1225 (mysqld) Status: "Server is operational" Tasks: 37 (limit: 37964) Memory: 359.7M CPU: 1.939s CGroup: /system.slice/mysql.service `-1225 /usr/sbin/mysqld May 09 07:47:00 ubuntu22ct systemd: Starting MySQL Community Server... May 09 07:47:00 ubuntu22ct systemd: Started MySQL Community Server.
As you can see, MySQL service is loaded and running! If it is not started for any reason, you can start MySQL service using command:
$ sudo systemctl start mysql
To enable MySQL to start automatically on every reboot, run:
$ sudo systemctl enable mysql
To restart MySQL service, run:
$ sudo systemctl restart mysql
2. Set MySQL root user password
By default, MySQL root password is blank, which is not recommended for production environments. You must set a strong and unique database administrative user i.e. root user password.
MySQL ships with a script named
mysql_secure_installation to remove insecure default settings and secure the database server.
mysql_secure_installation script will do the following:
VALIDATE PASSWORD COMPONENT,
VALIDATE PASSWORD COMPONENTis enabled, set your preferred
- Remove anonymous users,
rootuser login remotely,
- Remove test database and remove the privileges on test database,
- Finally reload privilege tables.
Now let us run this script:
$ sudo mysql_secure_installation
When you run this command for the first time after MySQL installation, you will be connected to MySQL with a blank password. Then, you will be prompted if you want to setup
VALIDATE PASSWORD PLUGIN.
VALIDATE PASSWORD PLUGIN is used to test passwords and improve security. It checks the password strength and allows the users to set only those passwords which are secure enough.
Setting up VALIDATE PASSWORD COMPONENT is optional. It is safe to leave it disabled. However, I recommend you to use strong and unique passwords when creating user roles. If you don't want to enable
VALIDATE PASSWORD PLUGIN, simply press any key to skip the password validation part and continue the rest of the steps.
I want to use this
VALIDATE PASSWORD PLUGIN, so I entered y for yes:
Securing the MySQL server deployment. Connecting to MySQL using a blank password. VALIDATE PASSWORD COMPONENT can be used to test passwords and improve security. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD component? Press y|Y for Yes, any other key for No: y
If you choose yes, you will prompted to choose a password policy level. There are three password validation policies are available. They are LOW, MEDIUM and STRONG.
Each password policy contains a set of password rules that you should use when creating new database roles. For example if you choose MEDIUM, the password should contain at least 8 characters long including a number, a lowercase and uppercase letters, and a special character. Choose any password policy that suits you.
I go with LOW level password policy, so I choose 0 (zero):
There are three levels of password validation policy: LOW Length >= 8 MEDIUM Length >= 8, numeric, mixed case, and special characters STRONG Length >= 8, numeric, mixed case, special characters and dictionary file Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG:
Next enter the MySQL root user password twice. After entering the password, the password strength score will be displayed. In my case, I got 100. If the password strength is OK for you, press
y to continue with the given password. Else, type another strong password and then press
Y to continue.
Please set the password for root here. New password: Re-enter new password: Estimated strength of the password: 100 Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
Please don't confuse this with your system's
root account. It is an administrative account for our database server.
y and hit ENTER to accept the defaults for all subsequent questions. This will remove anonymous user, disallow root login remotely and remove the test databases.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y Success. Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y Success. By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y - Dropping test database... Success. - Removing privileges on test database... Success. Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y Success. All done!
We have set MySQL root user password.
Did you forget your MySQL root password? No problem! You can easily reset root password in MySQL as described in the following links:
3. Login to MySQL
To login to MySQL server as
root user, simply run:
$ sudo mysql
Enter your user's password to access the MySQL prompt:
[sudo] password for ostechnix: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.29-0ubuntu0.22.04.2 (Ubuntu) Copyright (c) 2000, 2022, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
Start creating the user roles and databases for your projects. We will cover it in a separate guide soon.
To view the
help section, type
\h and press
To exit from MySQL prompt and return back to your Terminal, run:
4. Change authentication method for MySQL root user
Did you notice that I logged into the MySQL server without providing the MySQL root user's password. I simply entered the password of my system user account, but not the MySQL root user's password. This is because MySQL root user uses
auth_socket plugin to authenticate with MySQL server in Ubuntu systems running MySQL 5.7 and later versions.
auth_socket plugin authenticates clients that connect to the MySQL server from the local host through the Unix socket file. So you can't authenticate to the MySQL server as
root user by providing a password.
If you want to use password authentication for MySQL root user, you need to reconfigure the MySQL server to use a different authentication method as described in the following article.
🔗 Change Authentication Method For MySQL Root User In Ubuntu
In this guide, we discussed how to install MySQL in Ubuntu Linux operating system. We also learned how to set MySQL root user password and how to access or connect to the MySQL server as
using the “expect” tool uou can automate the mysql configuration with a script. like here: