Seems like this is a pretty common error, but I can't work it out. I'm running PHP 5.3.1 towards an
external MySQL server 5.5.8 - both installed with Phpmyadmin and running Windows Server 2k8R2.
When I try the following:
$connection = new mysqli("myhost.com:3306", "myUser", "myPwd", "myDB");
$result = $connection->prepare("SELECT * FROM tt_staff
WHERE crew_type = ?
AND inaktiv_vakt = 0
ORDER BY ansvarlig_vakt DESC, crew_type, navn_vakt");
$result->bind_param("s", $value);
$result->execute();
$result->bind_result($value, $name);
I get the "
Warning: mysqli::mysqli() [mysqli.mysqli]: (HY000/2005): Unknown MySQL server host (...)"
I made sure that the Mysqli is enabled in the php.ini on the DB-server, but that's also all I've done as I've been told this should work out-of-the-box. Since this is the first time I'm working with prepared statements there very well might be a simple typo error in there some place.
The problem is that MySQLi does not accept the port as part of the hostname but as a separate parameter. So I think this is how you should open your connection:
$connection = new mysqli("myhost.com", "myUser", "myPwd", "myDB", "3306");
Or perhaps, do not give the port at all because 3306 is the default one.
Check the Privileges for your user ('myUser') on the Database. Make sure the privileges allow connections thru '%' (or just your IP), not just 'localhost' (127.0.0.1). Then make sure to "reload priviliges'.
BlockquoteNote: phpMyAdmin gets the users' privileges directly from MySQL's privilege tables. The content of these tables may differ from the privileges the server uses, if they have been changed manually. In this case, you should reload the privileges before you continue.
mysqli cannot find the database server, you need to check that domain and port number are correct
Related
I am trying to connect to a SQL server database that is running on a windows server. I am running this PHP code on a linux server (centos 7).
I am using the following pdo connection string to connect to the database.
$db = new PDO ("dblib:192.168.2.1;dbname=TestDB","username",'pass');
When i run the code i get the following exception. 'PDOException' with message 'SQLSTATE[HY000] Unable to connect: Adaptive Server is unavailable or does not exist (severity 9)'
I have tried to test the connection using tsql and i am able to connect to the database without any error. The following code gave me a list of all the tables in TestDB. It wouldnt work if i didng type use TestDB first.
tsql -S 192.168.2.1 -U username -P 'pass' -L TestDB
use TestDB
GO
select * FROM sys.Tables
GO
My freetds.conf file contains the following
[Server1]
host = 192.168.2.1
port = 1433
tds version = 8.0
I cannot figure out how i am able to connect using tsql, but cannot do the same when connecting with php.
The dblib driver is definitely installed.
print_r(PDO::getAvailableDrivers());
Array ( [0] => dblib [1] => mysql [2] => sqlite )
Answer
Found the cause of the problem. Turned out to be SELinux. The following commands fixed the issue.
setsebool -P httpd_can_network_connect 1
setsebool -P httpd_can_network_connect_db 1
You have the datasource name, you should make use of it:
$db = new PDO ("dblib:host=Server1;dbname=TestDB","username",'pass');
You are running linux right? I recommend giving odbc a shot.
Three things to check for you.
First try your connection using your defined port.
Instead of:
$db = new PDO ("dblib:192.168.2.1;dbname=TestDB","username",'pass');
try using this:
$db = new PDO("dblib:host=192.168.2.1:1433;dbname=TestDB","username",'pass');
Second, you should be sure if your SQL Server is configured to hear on port 1433. You can check this using the SQL Server Configuration Manager.
The third (if you run it on windows) thing you can check is one thing I find in the PHP docs. Inside a comment, another one mentioned the same error. Here is the answer which seems to work:
For PDO MSSQL connection issues, ensure that you have the updated version of ntwdblib.dll (currently 8.00.194 as of this post). Overwrite the existing (old) file or place it in the Windows system32 folder. The version that ships with PHP 5.2.X does not work. This is a well known issue apparently, but I had a really hard time finding information on this issue until I was able to lock in a file name. I hope that this helps someone.
Another possible issue could be SELinux if it's enabled. I've gotten some errors which are something familiar with this on my Ruby on Rails installation. You can give it a try by disabling SELinux and try it again.
When connecting using PDO, if you leave out the host= it uses the Unix domain sockets which could be causing your problem. Putting in the host= will connect to the database via TCP/IP.
So try change your line of code to:
$db = new PDO ("dblib:host=192.168.2.1;dbname=TestDB","username",'pass');
Or with the port aswell:
$db = new PDO ("dblib:host=192.168.2.1:1433;dbname=TestDB","username",'pass');
For me, the problem was using the IP address.
I changed it to "MyServerName/SQL2017DEV" and it connected
I am trying to connect to Oracle database which is not on my PC. I have installed Xampp, PHP and Oracle instant Client. I have added a windows environmental path to C:\instantclient_11_1. The Oci8 is enabled too when I check from phpinfo(). I have added the extension extension_dir = C:\php-5.4.0\ext to the php.ini and also enabled extension=php_oci8_11g.dll. Then when I try to connect to the database using the code below:
<?php
$conn = oci_connect('username', 'password');
$query = 'select table_name from user_tables';
?>
It says Warning: oci_connect(): ORA-12560: TNS:protocol adapter error. Could anyone help?
The oci_connect call has an optional third parameter for the connection_string. As you aren't specifying TWO_TASK or LOCAL in your environment, as mentioned in the documentation, you have to provide that connection string so that PHP knows how to find and connect to your database.
The general pattern for easy connect syntax is:
$conn = oci_connect('username', 'password', '//hostname:port/service_name');
Where hostname is the name or IP address of the server the database is on - which will not be localhost since you said it is not on your PC; port is the listener port, which defaults to 1521 but could be something else (you can do lsnrctl status on the server to check); and service_name is the database service name, which may or may not be the same as the SID (you can do lsnrctl services to see the valid values on your server).
If you have an SQL Developer connection to the same database, the hostname and port will be shown in the connection settings. It may also show the service name, or may show the SID depending on how it was configured. The service name and SID might be the same. (If you can't run lsnrctl but have sufficient privileges you can try select value from v$parameter where name = 'service_names', but it's likely you won't be able to see that view).
I've done a bit of research on this today, but nothing seems to address this issue. I recently upgraded to PHP 5.3.3 from 5.1.6, as well as upgraded MySQL to 5.5 from 5.0. Afterwards, the following code generates an error saying "Can't connect to mysql database":
$connection = mysql_pconnect($dbhost, $dbusername, $dbpassword);
if (!$connection) {
//Can't connect
die('Could not connect: ' . mysql_error());
return;
}
And get the following error:
Warning: mysql_pconnect(): Can't connect to MySQL server on '199.59.157.103' (13) in /var/www/html/ws/Cust/customerWS_1_1.php on line 19 Could not connect: Can't connect to MySQL server on '199.59.157.103' (13)
I am able to connect to the remote host via the command line, and have tried everything from resetting the password to shutting down IP Tables. I'm kind of at a loss - so any help would be appreciated.
We had a similar problem with the same error message "Can't connect to mysql database" and maybe the following points will be also usefull:
check mysql user privileges (in our case and with external connections we had the old server IP address (allow access) in the table) GRANT ALL PRIVILEGES ON tablename.* TO 'username'#'145.1.1.2';
-> reload privileges after changes (FLUSH PRIVILEGES;)
disable the SELinux parameter. In new Plesk versions (>10?) this would be set enabled automatically...
--> in /etc/selinux/config change the line that says:
SELINUX=enforcing to SELINUX=disabled
See further details: http://googolflex.com/?p=482
There is a PHP 5.3 mysql driver restriction related to old authentication scheme hash stored in the database by previous mysql server. This could cause a problem with stored passwords so the databases will be unaccessible with old passwords.
To solve the problem you should recreate the mysql users with the same login and same password.
When you say "I am able to connect to the remote host via the command line" do you mean, you are connecting to the MySQL server REMOTELY? Or that you are SSHing to your MySQL server and connecting to it from the MySQL server's command line? If the latter, it's possible that when you upgraded to MySQL 5.5 you accidentally stopped MySQL from binding / listening on a public IP. Check your my.cnf.
Otherwise: is your PHP code being run on the same server as your MySQL server? It's possible that your permissions could be off. MySQL permissions are very particular about connecting to the MySQL server via it's IP address if the client only has access to connect from 'localhost', and vice-versa.
First you should make sure that the connect parameters $dbhost, $dbusername and $dbpassword are correct. Assuming these are correct, you should check your mysql ini settings. I would assume that the bind-address is set to 127.0.0.1 or localhost, but not to a public IP address.
Also note that it's a security risk if you can connect to mysql from a public IP address. You may want to look into a solution that does not require such connection.
How to log with a diferent user in a MYSQL remote database?
Here's what I've done:
Logged as root in the MYSQL:
'create user 'user'#'%' IDENTIFIED BY 'password';
'grant select on *.* to 'user'#'%';
Then I setted a PHP script which connection is this one:
$con = mysql_pconnect("xxx.xx.xxx.xxx","user","password");
$selected = mysql_select_db("database",$con);
Aaaand it isn't working:
I'm using LAMP on a cloud server, by the way;
Warning: mysql_pconnect() [function.mysql-pconnect]: Can't connect to MySQL server on 'xxx.xx.xxx.xx' (10061) in D:\path\index.php on line 21
What am I doing wrong?
EDIT: Not a firewell issue;
You might want to check the MySQL documentation on this specific problem. If I had to guess, I would say that your MySQL server may be bound only to the local (127.0.0.1) address. To troubleshoot you should probably try connecting to the server using the command line MySQL client in order to get a better idea of why exactly the connection isn't being made.
Seems to me like a firewall issue. You should check if the machine hosting the MySQL server allows connection on the port 3306 from external IPs as well.
Check if the server you have to connect to has the firewall open on the port you are trying to connect... default port is 3306
you can use
mysqladmin -h localhost
to see check what is the port mysql is using
Ok, If you can answer this question, you deserve the nobel peace prize. Anyways, here's the situation.
I'm using Slicehost dot net, and I have 2 slices (two different IPs). One slice is my mail server and the other is my normal website. I'm running Ubuntu (8.04 or 8.10, something like that, it shouldn't matter). What I'm trying to do is access the MySQL database on the Mail server from the other slice. I'm trying to do that with PHP. I really appreciate any help.
mysql_connect()
$resource = mysql_connect('other-server-address.com', 'username', 'password');
The first parameter is the mysql server address.
Server Param
The MySQL server. It can also include
a port number. e.g. "hostname:port" or
a path to a local socket e.g.
":/path/to/socket" for the localhost.
If the PHP directive
mysql.default_host is undefined
(default), then the default value is
'localhost:3306'. In SQL safe mode,
this parameter is ignored and value
'localhost:3306' is always used.
Unless I'm misunderstanding... this setup is pretty common. Any trouble you're having might be related to the following:
Firewall settings
Grant access to the mysql user to connect from the other host
my.ini settings not allowing outside connections
Some other related SO questions:
Connecting to MySQL from other machines
How do I enable external access to MySQL Server?
php access to remote database
How to make mysql accept connections externally
Remote mysql connection
Assuming your mail server is at IP 192.168.1.20 and web server is 192.168.1.30
First of all you need to allow the web server to access the mysql database on your Mail server .
On 192.168.1.20 you run the mysql command and grant access on the database needed to your web server
mysql> grant all on mydb.* to 'someuser'#'192.168.1.30' identified by 'secretpass;
Your PHP code connects to that database with something like:
$conn = mysql_connect('192.168.1.20', 'someuser', 'secretpass');
mysql_connect() returns a link identifier if the connection is successful. Also you have to do is keep the references to both links.
When you want to use which ever link, simply include the link as an argument.
$link1 = mysql_connect($host1, $username1, $password1);
$link2 = mysql_connect($host2, $username2, $password2);
$r = mysql_query(QUERY, $link1);
Simple as that.