Question

I have just set up an Ubuntu web server (I'm new to Linux) and after many issues, I have finally got round to setting my website up. However, I cannot connect to the local database using either the IP address or 'localhost'. PHPMyAdmin works absolutely fine, but I can't connect to mysql even when I use the same access details as PHPMyAdmin.

I am connecting with the following PHP code (which worked fine on my laptop and on my other server):

mysqli_connect($server, $username, $password, $database);

I have created a brand new account and granted full permissions to it, but with no luck.

If I use the IP address to connect, I get the following error:

Not connected : Can't connect to MySQL server on '(ip address)' (111)

If I use 'localhost', I get the following error:

Not connected : Access denied for user 'newuser'@'localhost' (using password: YES)

I am 100% sure the password is correct and, seeing as PHPMyAdmin works fine, I am sure the MySQL service is running.

Any help would be appreciated!

EDIT : SELECT * FROM mysql.User;

localhost root *049124FA19B22B4F6343D5C6FB5E31F710CA3AF7 Y Y Y Y Y Y Y Y Y Y Y Y‌​ Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y 0 0 0 0  
ns3367936 root *049124FA19B22B4F6343D5C6FB5E31F710CA3AF7 Y Y Y Y Y Y Y Y Y Y Y Y‌​ Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y 0 0 0 0
127.0.0.1 root *049124FA19B22B4F6343D5C6FB5E31F710CA3AF7 Y Y Y Y Y Y Y Y Y Y Y Y‌​ Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y 0 0 0 0 
::1 root *049124FA19B22B4F6343D5C6FB5E31F710CA3AF7 Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y‌​ Y Y Y Y Y Y Y Y Y Y Y Y Y Y 0 0 0 0
localhost N N N N N N N N N N N N N N N N N N N N N N N N N N N N N 0 0 0 ‌​0 NULL 
ns3367936.ip-37-187-88.eu N N N N N N N N N N N N N N N N N N N N N N N N N N ‌​N N N 0 0 0 0 NULL 
localhost debian-sys-maint *7866534019D21D9BA648FD769CFAAD256D2F7438 Y Y Y Y Y Y‌​ Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y N 0 0 0 0 NULL 
localhost phpmyadmin *049124FA19B22B4F6343D5C6FB5E31F710CA3AF7 N N N N N N N N N‌​ N N N N N N N N N N N N N N N N N N N N 0 0 0 0 NULL     
localhost newuser *049124FA19B22B4F6343D5C6FB5E31F710CA3AF7 Y Y Y Y Y Y Y Y Y Y ‌​N Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y 0 0 0 0 NULL 
Was it helpful?

Solution

You get the error while trying to login from 127.0.0.1 because you have not created an account for 'newuser' that is allowed access MySQL from an ip address of 127.0.0.1.

See your 'root' accounts for example.

There are 4 accounts for 'root' i.e. root@localhost, root@127.0.0.1, root@::1 and root@ns3367936.

A MySQL account requires the Username and a host. You only have 'newuser@localhost` and you need newuser@127.0.0.1 as well, and maybe root@::1 also if you have IPV6 running

Of course you coudl also edit the existing 'newuser' account and wilcard the host so that that user can login from any host.

Host in this case I always find is a misleading name, in fact it means the location that the user is trying to login from. So if you wanted to allow the 'newuser' account to login from a remote location like home, you could also add a newuser@11.22.33.44 account, assuming your home IP address was 11.22.33.44

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top