Website Language

Saturday, January 4, 2020

Mysql connection problems and setting root password

Recently i had to make a fresh install to my Linux machine. While installing software and packages i need regularly i had some problems with mysql server. I had errors while trying to connect as root user. Here i will note the steps for solving the problem and resetting mysql root password step by step.
(Please enter/copy the commands one by one. Do not copy all commands at once and paste to terminal. Terminal shortcut is Ctrl Alt T)

If you had edited mysql configuration and would like to start from the beginning without confusion, you may prefer to purge your mysql server installation and install again using these commands:

sudo apt purge mysql-server
sudo apt-get install mysql-server

First i opened a terminal window and entered the following commands. I used the first two commands because i had an error about permission problem for /var/run/mysqld directory while starting mysql server.

sudo mkdir -p /var/run/mysqld
sudo chown mysql:mysql -R /var/run/mysqld
sudo /etc/init.d/mysql stop
sudo mysqld_safe --skip-grant-tables &
(press enter)
mysql -uroot

You should be able to access mysql query terminal now. Enter the following mysql commands:

use mysql;
update user set authentication_string=PASSWORD("yourrootpassword") where User='root';
update user set plugin="mysql_native_password" where User='root';
flush privileges;
quit;

When you turn to terminal enter these commands for testing your connection.

sudo /etc/init.d/mysql stop
sudo /etc/init.d/mysql start # reset mysql
mysql -u root -p

When your connection is ok you can also test using your mysql admin software like phpmyadmin, adminer etc. to be sure that you can connect on web too. If you get errors about missing plugins etc. make sure that you have installed mysql module for php and restart apache server.

Contact Form

Name

Email *

Message *