Question

MySQL installation failing - root password required

I installed mysql-server via apt install mysql-server. It seemed to go fine. However, when I enter sudo mysql to set up a root password, I get:

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

Which is interesting, because none of the documentation I’ve seen says that I need a password. I tried sudo mysql -u root -p and hit Enter at the password prompt, but I got the same result.

Next, I decided to try sudo mysql_secure_installation. No luck - it asked me for the root password, which I had not yet been able to create. Hitting Enter got me:

Error: Access denied for user 'root'@'localhost' (using password: NO)

I.e., the same message with a different prefix.

I dug a little bit and found the suggestion to stop the service and start it with mysqld_safe. That failed in a different way:

[1] 17293
~: 2022-06-20T00:07:01.652212Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2022-06-20T00:07:01.654424Z mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists.

[1]  + done       sudo mysqld_safe –skip-grant-tables –skip-networking

And indeed, /var/run/mysqld don’t exists. I created it and tried again:

[1] 17776
~: 2022-06-20T00:13:33.123633Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2022-06-20T00:13:33.154910Z mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
2022-06-20T00:13:35.950220Z mysqld_safe mysqld from pid file /var/lib/mysql/ocean.pid ended

[1]  + done       sudo mysqld_safe –skip-grant-tables –skip-networking

It took roughly a second to start and then end. /var/log/mysql/error.log had no new messages. /var/lib/mysql/ocean.pid was nowhere to be found.

Finally, I tried a suggestion that had me updating the contents of /etc/mysql/my.cnf. All of the commands - mysql, mysql_secure_installation, mysqld_safe - gave the same results, so I reset the my.cnf file to its former state.

To summarize, thus far I simply can’t use MySQL, because it’s asking for a root password that it won’t let me create. Any thoughts out there about what might be going on?


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi there,

I’ve just tested that out on a fresh new Ubuntu 22.10 Droplet with a fresh new installation of MySQL 8.0 and it seems to be working out of the box.

Can you confirm the OS that you are using and the exact version?

cat /etc/os-release

And also can you verify the MySQL version as well:

mysql -V

Regarding the mysql_secure_installation script, as of July 2022, an error will occur when you run the mysql_secure_installation script without some further configuration.

The reason is that this script will attempt to set a password for the installation’s root MySQL account but, by default on Ubuntu installations, this account is not configured to connect using a password:

https://www.digitalocean.com/community/tutorials/how-to-install-mysql-on-ubuntu-22-04

Best,

Bobby