SiemensSEN
asked on
Cannot connect to mysql
Hello .
I cannot connect to MYSQL from PHP.
The ID/pass is correct since I can connect from the shell.
user | host |
+---------+-----------+
| wikiadm | % |
|| root | localhost |
| wikiadm | localhost |
+---------+-----------+
PHP CODE
I cannot connect to MYSQL from PHP.
The ID/pass is correct since I can connect from the shell.
GRANT OUTPUT
+----------------------------------------------+
| Grants for wikiadm@% |
+----------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'wikiadm'@'%' |
+----------------------------------------------+
MYSQL USER TABLEuser | host |
+---------+-----------+
| wikiadm | % |
|| root | localhost |
| wikiadm | localhost |
+---------+-----------+
PHP CODE
<?php
// Show all information, defaults to INFO_ALL
$username = "wikiadm";
$password = "password";
$hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
ASKER
Thanks,
The error is
Could not connect: Permission denied
The error is
Could not connect: Permission denied
ASKER
I notice that I can connect if I run the program from the local shell. However, I get permission denied from the browser..
What operating system are you running, and if Linux, what is the output of the following command?
/usr/sbin/getenforce
/usr/sbin/getenforce
ASKER
I am using linux but this command does not exist
If you're on Windows 7 or above, try using '127.0.0.1' as the server name. Some versions of MySQL don't work with IPV6 which is the default 'localhost' in Windows 7 and above. Alternately use the IP address of the machine itself.
Is the web server on the same machine as the MySQL server? It has to be to use 'localhost'.
ASKER
Yes, Both the webserver (apache) and the mysql is on the same server
What do you see if you type the following?
telnet localhost 3306
telnet localhost 3306
ASKER
I run the program from the Linux shell and it connect to the DB. However, it does not work if I run it from the browser via webserver
ASKER
telnet localhost 3306
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused
-----
I will look at the port rules again..
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused
-----
I will look at the port rules again..
I run the program from the Linux shell and it connect to the DB. However, it does not work if I run it from the browser via webserver
Yes, I'm trying to determine if your MySQL server is listening on port 3306 on localhost. When you use the mysql cli, it usually uses the socket file (normally /tmp/mysql.sock) but Apache/PHP connects via tcp, port 3306.
So I would still like to see the output of each of these two commands:
telnet localhost 3306
netstat -na | grep :3306
Yes, I'm trying to determine if your MySQL server is listening on port 3306 on localhost. When you use the mysql cli, it usually uses the socket file (normally /tmp/mysql.sock) but Apache/PHP connects via tcp, port 3306.
So I would still like to see the output of each of these two commands:
telnet localhost 3306
netstat -na | grep :3306
ASKER
telnet localhost 3306
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused
-------------------------- ---------- ---------- ---------- ---------
netstat -na | grep :3306
tcp 0 0 172.20.32.86:3306 0.0.0.0:* LISTEN
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused
--------------------------
netstat -na | grep :3306
tcp 0 0 172.20.32.86:3306 0.0.0.0:* LISTEN
You need to set bind-address to 0.0.0.0 in /etc/my.cnf and restart MySQL and then it will work.
Right now it's only listening on your external IP address.
Right now it's only listening on your external IP address.
The output of netstat should look something like this after you change it:
[xterm@foo ~]$ netstat -na | grep :3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN
ASKER
I understand .. but
Here my issue.
I have a WIKi that connect to mySQL on server A that connect to DB and I can access it via a URL.
On SERVER B it does not work. It have same wiki and mysql
telnet localhost 3306
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused
usbrws02:(root) [/export/apps] $ netstat -na | grep :3306
tcp 0 0 172.20.32.86:3306 0.0.0.0:* LISTEN
Why would SERVER A works and not SERVER B
Here my issue.
I have a WIKi that connect to mySQL on server A that connect to DB and I can access it via a URL.
here is the output for SERVER A
netstat -na | grep :3306
tcp 0 0 172.25.0.14:3306 0.0.0.0:* LISTEN
------------------------------
telnet localhost 3306
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused
Trying ::1...
On SERVER B it does not work. It have same wiki and mysql
telnet localhost 3306
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused
usbrws02:(root) [/export/apps] $ netstat -na | grep :3306
tcp 0 0 172.20.32.86:3306 0.0.0.0:* LISTEN
Why would SERVER A works and not SERVER B
Probably on server A you configured it to connect to 172.25.0.14 instead of localhost, because your code from server B won't work there either.
You can change $hostname on server B to be 172.20.32.86 instead of localhost, and it will probably work, but then you will need to put that IP in the mysql user table and do a reload/flush privileges.
You can change $hostname on server B to be 172.20.32.86 instead of localhost, and it will probably work, but then you will need to put that IP in the mysql user table and do a reload/flush privileges.
ASKER
change it to bind to 0.0.0.0 but it did not work
netstat -na | grep :3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN
the error is..
Can't contact the database server: Permission denied (localhost))
netstat -na | grep :3306
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN
the error is..
Can't contact the database server: Permission denied (localhost))
ASKER
This is grant command for the id wikiadm
Grants for wikiadm@localhost |
+------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ------+
| GRANT ALL PRIVILEGES ON *.* TO 'wikiadm'@'localhost' IDENTIFIED BY PASSWORD '*1B36526200EB58A9AF17101D FBAEBD2DAE 877C1E' |
Grants for wikiadm@localhost |
+-------------------------
| GRANT ALL PRIVILEGES ON *.* TO 'wikiadm'@'localhost' IDENTIFIED BY PASSWORD '*1B36526200EB58A9AF17101D
Have you run "flush privileges" since you did the GRANT command?
BTW, now the problem of not being able to connect to localhost is fixed, now you have a permissions issue. Are you sure you're able to connect with the CLI?
BTW, now the problem of not being able to connect to localhost is fixed, now you have a permissions issue. Are you sure you're able to connect with the CLI?
Please show the following SQL commands in the CLI in DB mysql:
SELECT * FROM db;
SELECT * FROM user;
SELECT * FROM db;
SELECT * FROM user;
ASKER
Yes, I issue the flush command
mysql> flush PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
GRANT output
mysql> show grants for wikiadm@localhost
-> ;
+------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ------+
| Grants for wikiadm@localhost |
+------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ------+
| GRANT ALL PRIVILEGES ON *.* TO 'wikiadm'@'localhost' IDENTIFIED BY PASSWORD '*1B36526200EB58A9AF17101D FBAEBD2DAE 877C1E' |
+------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ------+
1 row in set (0.00 sec)
mysql> flush PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
GRANT output
mysql> show grants for wikiadm@localhost
-> ;
+-------------------------
| Grants for wikiadm@localhost |
+-------------------------
| GRANT ALL PRIVILEGES ON *.* TO 'wikiadm'@'localhost' IDENTIFIED BY PASSWORD '*1B36526200EB58A9AF17101D
+-------------------------
1 row in set (0.00 sec)
OUTPUT from DB (the wikiadm does not exist..)
mysql> SELECT host, db, user FROM db;
+-----------+-------+-------+
| host | db | user |
+-----------+-------+-------+
| localhost | oemdb | oemdb |
+-----------+-------+-------+
1 row in set (0.00 sec)
-----------------------mysql> select host,user from user;
+-----------+---------+
| host | user |
+-----------+---------+
| 127.0.0.1 | root |
| ::1 | root |
| localhost | oemdb |
| localhost | root |
| localhost | wikiadm |
+-----------+---------+
5 rows in set (0.01 sec)
ASKER
I can connect from the linux shell. However, i get access denied when I access it from http
This really isn't this hard. If you are connecting to the MySQL server from the machine that it is on, you can use 'localhost'. If you are connecting from another machine, you have to use the IP address of the machine that MySQL is running on. A remote connection can not be made with 'localhost' because it means exactly what it sounds like. On the "local host" and no where else. Same with IPV6 ::1 and IPV4 127.0.0.1, those are strictly 'localhost', not remote host.
http://dev.mysql.com/doc/refman/5.5/en/connecting.html
http://dev.mysql.com/doc/refman/5.5/en/connecting.html
I was just working on my Ubuntu Linux box and I couldn't connect to it remotely. So I brought up the settings in WebMin and it was set to only respond to '127.0.0.1'. I changed it to 'anyhost' and the problem was solved. Now I have to do it with my other machine. I have my MS SQL servers set up to accept remote connections also. Makes it easier to test code that way.
ASKER
Maybe I an not clear..
<?php
The code above works when I run it from the linux shell [[php connect.php]]
However, if i open a browser from a remote machine and type [[http://<hostIP>/connect.php then I get the access error
<?php
// Show all information, defaults to INFO_ALL
$username = "wikiadm";
$password = "password";
$hostname = "localhost";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
The code above works when I run it from the linux shell [[php connect.php]]
However, if i open a browser from a remote machine and type [[http://<hostIP>/connect.php then I get the access error
Is the web server where PHP is running on the same machine where you are connecting from the shell? Then it should work. Or is the web server where PHP is being run on a different machine? It that is the case, then 'localhost' shouldn't work.
What happens when you type from the linux shell?:
mysql -u wikiadm -p password oemdb
(where "password" is whatever you set the password to for the wikiadm user)
mysql -u wikiadm -p password oemdb
(where "password" is whatever you set the password to for the wikiadm user)
ASKER
The code works from the Linux shell
If I change the ip from localhost to the server ip (172.20.nn.nn) then the code works from the shell and browser
So, localhost or 127.0.0.1 does not work when I try to access the script via http . However it works from the shell
If I change the ip from localhost to the server ip (172.20.nn.nn) then the code works from the shell and browser
So, localhost or 127.0.0.1 does not work when I try to access the script via http . However it works from the shell
try changing the $hostname
technicallly should be service in the format
hostname:port/database/
default port is 3306 for localhost,
yours may be different
localhost:3306/oemdb/
see if that fixes it
that was the problem with me at first
technicallly should be service in the format
hostname:port/database/
default port is 3306 for localhost,
yours may be different
localhost:3306/oemdb/
see if that fixes it
that was the problem with me at first
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window