0

I'm using PHP in order to connect to MySQL with the following way:

$link = mysql_connect('...host...', '...username...', '...password...'); 
if (!$link) { 
    die('Could not connect: ' . mysql_error()); 
} 
echo 'Connected successfully'; 

and it connects just fine. But when trying from my terminal this

mysql -h ...host... -u ....username... -p ...password...

I give my password and i take as a result this:

ERROR 2003 (HY000): Can't connect to MySQL server on '...host...' (111)

Any ideas how this can be solved?

Nick Fr.
  • 1
  • 1
  • Do you have apache installed? What does sudo status mysql output? – Paul Jul 18 '11 at 03:36
  • When you say "my terminal", do you mean a terminal on your own system or on the server (through SSH)? – Francois Deschenes Jul 18 '11 at 03:37
  • @PaulPRO I use a hosting provider @Francois a terminal on my own system ... – Nick Fr. Jul 18 '11 at 03:38
  • 1
    It's quite possible that the host isn't available publicly. The hostname may point to an internal IP address or the MySQL server may only accept connections from the local network. – Francois Deschenes Jul 18 '11 at 03:40
  • I had a similar problem. I was specifying the ip address of the server when I should have been writing `localhost` for the ip address. Note that I wasn't trying to connect remotely. – TryHarder Oct 29 '12 at 03:20

2 Answers2

0

Try sudo mysql -h ....etc.

I'm running linuxMint and had to use either su or sudo. I think by default you have to be root to enter the terminal. But with Ubuntu root is inaccessible, the only feature I don't like about Ubuntu.

Alan B. Dee
  • 5,490
  • 4
  • 34
  • 29
0

The php code is running on the web server, your local command is running on your machine.

MySQL accounts are not just usernames - they are username + hostname combos. So both have to match for MySQL to find the account.

Since your hostname is different from the one the web server uses MySQL does not consider it a valid user. (It's possible to set the hostname to % which means "Any".)

Ariel
  • 25,995
  • 5
  • 59
  • 69