It’s very easy to install MySQL on the Ubuntu server by using the below commands
sudo apt update
sudo apt install mysql-server
sudo mysql_secure_installation
For details follow the below steps
1st Step
First, of all run the command
sudo apt update
when the command executes successfully then run the MySQL server command
sudo apt install mysql-server
press y for the installation properly.
2nd Step
Configure Mysql
Run command
sudo mysql_secure_installation
This will come with many prompts for asking in case you understand the point put the value y or n which stands for yes or no.
Otherwise, you will enter and the question will skips.
Mysql install with root username and no password
so run command
sudo MySQL
this will come like this
mysql>
after that use the below steps for making the password for the root user
mysql> SELECT user,authentication_string,plugin,host FROM MySQL.user;
The output will come like this
+——————+——————————————-+———————–+———–+
| user | authentication_string | plugin | host |
+——————+——————————————-+———————–+———–+
| root | | auth_socket | localhost |
| mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| debian-sys-maint | *CC744277A401A7D25BE1CA89AFF17BF607F876FF | mysql_native_password | localhost |
+——————+——————————————-+———————–+———–+
4 rows in set (0.00 sec)
If the root user shows auth_socket means the root user has no authentication so for adding a password for the root user run the below command
mysql> ALTER USER ‘root’@’localhost’ IDENTIFIED WITH mysql_native_password BY ‘password’;
‘password’ — put the password which you want to set;
when the command runs successfully
Flush all the privileges by using the below command
mysql> FLUSH PRIVILEGES;
After the when you run the user command
mysql>SELECT user,authentication_string,plugin,host FROM mysql.user;
you will get the output
+——————+——————————————-+———————–+———–+
| user | authentication_string | plugin | host |
+——————+——————————————-+———————–+———–+
| root | *3636DACC8616D997782ADD0839F92C1571D6D78F | mysql_native_password | localhost |
| mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| debian-sys-maint | *CC744277A401A7D25BE1CA89AFF17BF607F876FF | mysql_native_password | localhost |
+——————+——————————————-+———————–+———–+
4 rows in set (0.00 sec)
there is no auth_socket because now the root user has a password.
Now you can exit from MySQL using the exit command
mysql> exit
Again try to login directly using the MySQL command
sudo mysql
you will get the error prompt for you to have authentication.
Now use the below command
mysql -u root -p
enter the command you will ask for a password put the password you log in.
mysql>
Mysql Install successfully and password for root user add.
If you want to create a new user for MySQL use below steps
Login in MySQL using the above command
mysql -u root -p
enter the password then you log in.
mysql>
Now use the below command to create a new user
mysql>CREATE USER ‘max’@’localhost’ IDENTIFIED BY ‘password’;
You can use username as you want in place of ‘max’ and password in place of ‘password’
After run successfully this command grant access to this user by using the below command
mysql>GRANT ALL PRIVILEGES ON . TO ‘max’@’localhost’ WITH GRANT OPTION;
After the query ok you will exit and login with this new user.
mysql> exit
Using the below command you can check the new user access
mysql -u max -p
and put the password after this above command.
you will log in successfully
mysql>
If you still face any issue please feel free to put your command in the comment section as we get the comment we will help you to resolve your issue.
Have a great day.