Trouble installing MySQL on Ubuntu 20.04 — asks for root password

  • Thread starter Thread starter Wrichik Basu
  • Start date Start date
  • Tags Tags
    Mysql Root Ubuntu
Click For Summary

Discussion Overview

The discussion revolves around issues encountered while installing MySQL on Ubuntu 20.04, specifically related to root user access and password management. Participants share their experiences, troubleshooting steps, and advice on securing the MySQL installation.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Experimental/applied

Main Points Raised

  • One participant reports being able to log into MySQL as root without a password after installation, which they found unsafe, leading them to change the authorization plugin.
  • After changing the authorization, the same participant could no longer log in as root, receiving an access denied error despite entering what they believed was the correct password.
  • Another participant suggests purging MySQL to remove all configuration files and databases before reinstalling, claiming this should allow root access without a password.
  • A later reply confirms that following the suggested steps resolved the initial participant's issue, allowing them to install and set up the server successfully.
  • Concerns are raised about the safety of logging in as root without a password, with some participants arguing that it is acceptable if only trusted users have access to the server.
  • One participant mentions encountering an access denied error when trying to connect via MySQL Workbench, despite having set a password during installation.
  • Another participant explains that using 'sudo' allows root access to MySQL, but emphasizes the importance of understanding Linux root privileges and suggests creating additional users for regular access.
  • Participants discuss the need to configure MySQL Workbench to use a non-root user for better security practices.

Areas of Agreement / Disagreement

Participants express differing views on the safety of logging in as root without a password. While some argue it is acceptable under certain conditions, others emphasize the need for stronger security measures. The discussion remains unresolved regarding the best practices for root access and user management in MySQL.

Contextual Notes

Participants mention various commands and configurations related to MySQL installation and user management, but there are no consensus conclusions on the best approach to securing the MySQL root user.

Wrichik Basu
Science Advisor
Insights Author
Gold Member
Messages
2,186
Reaction score
2,694
I wanted to install MySQL on my laptop running Ubuntu 20.04, and was following this website for instructions. I executed the following commands:
Code:
~$ sudo apt update
~$ sudo apt install mysql-server
~$ sudo mysql_secure_installation
After installation, I found that I could log into root user without using any password. This felt a little unsafe, so following this answer on askubuntu.com, I changed the authorization plugin such that I am requested a password every time I type mysql -u root -p.

Since then, I cannot log into the root user any more. I am sure that I am entering the correct password, but it just doesn't let me in, and shows this error message:
Code:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
I thought a re-installation would help. So I uninstalled it using the following commands:
Code:
~$ sudo apt remove mysql-server
~$ sudo apt autoremove
Then I again installed it, but when I try to execute sudo mysql_secure_installation, I am asked for a password. Weird, there should be no password set because this is a fresh installation. I tried entering the previous password, and got this error:
Bash:
~$ sudo mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root:
Error: Access denied for user 'root'@'localhost' (using password: YES)
I tried entering no password, and got the same message as above, with the "YES" changed to "NO".

So, I cannot proceed with the installation.

Tried restarting the system so that temporary files are deleted, but no avail.

All help is appreciated.
 
Computer science news on Phys.org
If mysql is still installed:
Code:
sudo apt purge mysql-server
This will not only remove the mysql-server package, but also all it's config files etc.
If you get a prompt asking to remove databases, click yes.
Then you can install mysql again and it should allow you to login as root without password.If mysql is not installed, things become a little more tricky:
First make sure nothing is installed concerning mysql:
Code:
sudo apt list --installed | grep mysql
if anything pops up, simply remove it and it's config files, e.g.
Code:
sudo apt purge mysql-common
Then after everything is uninstalled, do the following:
Code:
sudo rm /var/lib/mysql
Afterwards you can reinstall mysql and, again, you should be able to log in
 
Last edited:
  • Like
  • Love
Likes   Reactions: jack action, Greg Bernhardt, sysprog and 2 others
Thanks a lot, @optimisticsoda, and also, welcome to Physics Forums! I followed the instructions, and they worked like a charm. Could finally install and set up the server.

One question: as before, I can login as the root user by just typing sudo mysql -u root. Is this safe enough and can I leave it at this?
 
Thanks :)

Whether you can leave it at that or need to change depends on the situation. If others can login on the server, you should definitely create a password for the root user.
Otherwise you can leave it as is.

Oh and just sudo mysql works too
 
  • Like
Likes   Reactions: sysprog and Wrichik Basu
I have just installed the MySQL workbench, and it came with a default connection to the root user with port 3306. When I click on it, I get this error:

1610485480306.png


The port is correct; I checked it:
SQL:
mysql> SHOW GLOBAL VARIABLES LIKE 'PORT';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3306  |
+---------------+-------+
1 row in set (0.00 sec)
Basically, this is the command where the error is being thrown:
Code:
~$ mysql -u root -p
Enter password:
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
Note that I have already set a password during installation.

This is exactly the place where I screwed up the first time, so I want to proceed with caution.
 
Wrichik Basu said:
One question: as before, I can login as the root user by just typing sudo mysql -u root. Is this safe enough and can I leave it at this?
Yes this is safe enough, the sudo there shows that only a (Linux) user with root privileges can do this, and if you have Linux root privileges then you can do anything on that server anyway so there is no point adding more 'security'.

Wrichik Basu said:
Code:
~$ mysql -u root -p
Enter password:
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
Note that I have already set a password during installation.

This is exactly the place where I screwed up the first time, so I want to proceed with caution.
This shows that without the sudo you can't attatch to MySQL as the root user, proving what I said above. Because you are not running mysql_workbench with Linux root privileges that means it can't attach to MySQL. Understanding root privileges and sudo is fundamental to administering any Linux at the console and along with file access permissions is the biggest thing you need to get your head around when moving from Windows.

You should only use the MySQL root user to create other users. To do this use the following (you can see from the prompt that I am using MariaDB which for these purposes is functionally equivalent to MySQL).
Code:
$ sudo mysql -u root
MariaDB [(none)]> CREATE USER 'admin'@'localhost' IDENTIFIED BY 'password';
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost';
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> exit
Bye
$
You then need to configure MySQL Workbench to use the admin user with the password; I can't help you with this, I don't use MySQL Workbench (I use phpMyAdmin instead).
 
Last edited:
  • Like
Likes   Reactions: sysprog and Wrichik Basu
pbuk said:
Yes this is safe enough, the sudo there shows that only a (Linux) user with root privileges can do this, and if you have Linux root privileges then you can do anything on that server anyway so there is no point adding more 'security'.
Understood.
pbuk said:
This shows that without the sudo you can't attatch to MySQL as the root user, proving what I said above. Because you are not running mysql_workbench with Linux root privileges that means it can't attach to MySQL.
Actually I tried executing sudo mysql-workbench, but even then it could not connect to the root user.
pbuk said:
You should only use the MySQL root user to create other users. To do this use the following (you can see from the prompt that I am using MariaDB which for these purposes is functionally equivalent to MySQL).
Code:
$ sudo mysql -u root
MariaDB [(none)]> CREATE USER 'admin'@'localhost' IDENTIFIED BY 'password';
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'admin'@'localhost';
MariaDB [(none)]> FLUSH PRIVILEGES;
MariaDB [(none)]> exit
Bye
$
You then need to configure MySQL Workbench to use the admin user with the password; I can't help you with this, I don't use MySQL Workbench (I use phpMyAdmin instead).
This is good advice; I created the second user and granted all privileges. Now I can connect to it via MySQL workbench, and I can execute queries normally. Thanks!
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 24 ·
Replies
24
Views
11K
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
6
Views
6K
Replies
16
Views
3K
  • · Replies 12 ·
Replies
12
Views
11K