I've followed the guides at https://learn.microsoft.com/en-us/sql/connect/odbc/linux-mac/installing-the-microsoft-odbc-driver-for-sql-server?view=sql-server-ver15 and https://learn.microsoft.com/en-us/sql/connect/php/installation-tutorial-linux-mac?view=sql-server-ver15 and PHP is finding the drivers / server but now I'm getting an authentication error.
Verified the server is configured for "SQL Server and Windows Authentication mode" and allows remote connections to the server.
In order to get this error, I believe that means the code is at least connecting and attempting to authenticate with the server (changing the server to some nonsense server name/location produces a timeout error) - fairly certain that eliminates any problems that would come before the authentication step.
Still, I'm getting an error as follows...
PDOException: SQLSTATE[28000]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for user 'username'. in /var/www/html/connect.php:10
Stack trace:
#0 /var/www/html/connect.php(10): PDO->__construct('sqlsrv:Server=S...', 'username', 'password')
#1 {main}root#containerIdGoesHere:/var/www/html
<?php
$user = 'username';
$pass = 'password';
try {
$pdo = new PDO("sqlsrv:Server=SomeServer,1433;Database=SomeDb", $user, $pass);
$pdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(Exception $e) {
echo 'See error log';
$elog = fopen('errorlog.txt', 'w') or die("Couldn't open file");
fwrite($elog, $e);
fclose($elog);
}
Note: I've removed any sensitive info from my code... so the username and pw I'm using are not 'username' and 'password' & not related to the issue.
I've got this working on my production environment:
$db = "DatabaseName";
$password = "Password";
$server = "TheSQLServer";
$user = "username";
$link = new PDO(
"sqlsrv:Server=" . $server .
";Database=" . $db,
$user,
$password
);
return $link;
I also had to manually get into the DB and allow the previously created user to have the proper rights into it (DB->Security->Users), you can check that out aswell. Let me know how it goes.
Related
I am trying to log into a local SQL database, but I keep getting an error about 'Login Failed'. This is the first time that I have tried to connect to a MS SQL server... normally I use MySQL. So, I am not even sure if I am doing this correctly.
Here is my connection code:
function dbConnection()
{
$host = '127.0.0.1';
$db = 'my_db';
$user = 'dbuser';
$pass = "my_pw";
$serverName = "tcp:$host,1443";
$connectionOptions = array(
"Database"=>$db,
"Uid"=>$user,
"PWD"=>$pass
);
$connection = sqlsrv_connect($serverName, $connectionOptions);
if (!$connection) print_r(sqlsrv_errors());
return $connection;
}
print_r(dbConnection());
Everytime I try this, I get:
[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Login failed for
user 'dbuser'.
The username and password are correct, and the permission are set correctly. The port is correct, too. I can log into SSMS just fine, but not through PHP.
Any ideas?
Assuming that your mssql server instance is say sqlexpress..
Please use
$serverName = "serverName\\sqlexpress";
or if the port is 1443, then
$serverName = "serverName\\sqlexpress, 1443"
I am trying to connect to (remote) AWS MYSQL database using php and ssl.
I created a database user and could connect with it from php and also on console.
Then I modified the user to use SSL with "require ssl" in the database.
I can stil connect on the console by:
mysql -h host -u username -p --ssl
But in php I get an ERROR 2002.
$con=mysqli_init();
mysqli_ssl_set($con,NULL,NULL,"cacert.pem",NULL,NULL);
$link = mysqli_real_connect($con, "host", "username", "password");
As far as I know the ssl_set parameters doesn't matter with AWS databases.
How to connect to Amazon RDS via SSL?
I think it has to do with the mysqli_ssl_set method, without it a connection is made and access is denied by database and this is the correct behaviour. It might be some configuration error on client side, but I have no idea what I shoud check.
The error is fired by mysqli_real_connect, as mysqli_ssl_set always returns true, just after it tries to connect will it receive an error.
I noticed it did not work for me until I specified the port. The following worked:
$db = mysqli_init();
$db->options(MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, true);
$db->ssl_set(NULL, NULL, 'rds-combined-ca-bundle.pem', NULL, NULL);
$db->connect($host, $user, $pass, $db, 3306);
$servername = "only-ssl-db.ct5b4uz1gops.eu-central-1.rds.amazonaws.com";
$username = "username";
$password = "password";
$dbname = "dbname";
$con = mysqli_init();
if (!$con){
die("mysqli_init failed");
}
mysqli_ssl_set($con,NULL,NULL,'rds-combined-ca-bundle.pem',NULL,'DHE-RSA-AES256-SHA');
if (!mysqli_real_connect($con,$servername, $username, $password, $dbname))
{
die("Connect Error: " . mysqli_connect_error());
}
// Some queries...
echo "Database connected";
printf("Client version: %d\n", mysqli_get_client_version());
mysqli_close($con);
This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 7 years ago.
I do not believe the question to be a duplicate as I believe the issue is related to Shared Hosting environments getting outbound to other cloud mysql servers.
Error message
PDO Exception: SQLSTATE[HY000] [2002] Connection timed out.
I am simply just trying to connect to my instance of AWS RDS and run a query on one of my tables.
SOLUTION: GODADDY BLOCKS ALL PORTS EXCEPT 21 AND 22 (FTP) ON A STANDARD ACCOUNT. THIS IS WHAT I WAS TOLD BY GODADDY TECHNICAL SUPPORT. HENCE, IT WILL BLOCK PORT 1234 AND EVEN 3306 FOR A DATABASE CONNECTION.
UPDATE:
#Drew has validated that this php code works for the connection:
try {
$db = new PDO('mysql:host=uwmparkingbackend.cb7gmuw49vhs.us-east-1.rds.amazonaws.com;port=1234;dbname=uwm_parking;charset=utf8', 'fbgrecojr', 'test1234');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
echo "Connected and Happy<br>";
$sql="select id,thing from thingsCoveted where id in (1,3)";
foreach ($db->query($sql) as $row) {
print $row['id'] . "\t";
print $row['thing'] . "<br>";
}
} catch (PDOException $e) {
echo 'PDO Exception: ' . $e->getMessage();
exit();
}
I am getting this error however on my end:
PDO Exception: SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'uwmparkingbackend.cb7gmuw49vhs.us-east-1.rds.amazonaws.com' (110)
Hence, there is either a config issue on the Godaddy side (where the .php is hosted) or the AWS RDS side (where the database is hosted);
Things I have tried:
1) checked to make sure all PDO options are selected in GoDaddy's php settings
2) added an inbound rule for my AWS RDS security group to allow all connections using my port (1234).
The problem is you've just copied and pasted your connection info into the example given by Amazon inside the $_SERVER array.
// Example
$link = mysqli_connect($_SERVER['RDS_HOSTNAME'], $_SERVER['RDS_USERNAME'], $_SERVER['RDS_PASSWORD'], $_SERVER['RDS_DB_NAME'], $_SERVER['RDS_PORT']);
// What yours should be
mysqli_connect(
'***********.******.us-east-1.rds.amazonaws.com',
'*******',
'********',
'dbname',
'1234'
);
You also need to use mysqli functions since that's how you're connecting, e.g. http://php.net/manual/en/mysqli.query.php
EDIT:
Now you're using PDO you will need to take a different approach to getting your data.
$dbhost = '***********.******.us-east-1.rds.amazonaws.com';
$dbport = 3306;
$dbname = '*****';
$dsn = "mysql:host={$dbhost};port={$dbport};dbname={$dbname}";
$username = '*****';
$password = '*****';
try {
$dbh = new PDO($dsn, $username, $password);
$query = $dbh->query("SELECT num_spaces FROM uwm_parking.lots");
while($row = $query->fetch(PDO::FETCH_OBJ)) {
var_dump( $row );
}
} catch(PDOException $e ){
echo "Error: ".$e;
}
I am trying to connect to a mssql server on my mac through pdo_dblib. I have the freetds.conf updated to the host I want to connect. My phpinfo tells me that I have all the driver hooked up and good to go. Below is the code that I wrote to test if I can connect to the server.
<?php
$servername = "IP";
$port = "port";
$username = "username";
$password = "password";
$myDB = "database";
try {
$conn = new PDO("dblib:dbname=$myDB;host=$servername:$port", "$username", "$password");
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
?>
However I get an error:
Connection failed: SQLSTATE[] (null) (severity 0)
I tried using SQL Developer to connect to the mssql server and it worked. I have been trying to solve this problem for a whole day. What might be the problem? Thanks!
Remove the quotes from the variables in the connection string -
$conn = new PDO("dblib:dbname=$myDB;host=$servername:$port", $username, $password);
I found out the answer!
You have to uncomment TDS version protocol in /usr/local/Cellar/freetds/0.91/etc/freetds.conf.
Found the solution in Why won't my server connect to a remote MSSQL server using PHP mssql_connect?
but why do I have to do this...no idea...wish some one could give some explanation.
this is my code it's working in local host but not in server
$dbtype = 'mysql'; // Normally mysql
$host = 'localhost'; // This is normally set to localhost
$db_username = 'name'; // DB username
$db_password = 'pass'; // DB password
$db_name = 'db_name'; // DB database name
$dbprefix = 'db_'; // Do not change unless you need to!
$cat_table="db_cat";
try
{
$pdo = new PDO('mysql:host='. $host .';port=2082;dbname='.$db_name, $db_username, $db_password);
}
catch (PDOException $e)
{
die($e);
}
?>
ERROR MESSAGE
exception 'PDOException' with message 'SQLSTATE[HY000] [2000] mysqlnd cannot connect to
MySQL 4.1+ using the old insecure authentication. Please use an administration tool to reset your password with the command SET PASSWORD = PASSWORD('your_existing_password').
This will store a new, and more secure, hash value in mysql.user. If this user is used in
other scripts executed by PHP 5.2 or earlier you might need to remove the old-passwords flag
from your my.cnf file' in /home/jeywigki/public_html/hotel/db/config.php:13 Stack trace:
#0 /home/jeywigki/public_html/hotel/db/config.php(13): PDO-
>__construct('mysql:host=loca...', 'table', 'pass') #1 /home/jeywigki/public_html/hotel/index.php(3): require_once('/home/jeywigki/...') #2 {main}
kindly help me.
thanks in advance
I am not sure but it could be the issue.
you can try this:
mysql:host=$host;dbname=$db_name inside in quotes.
try
{
$pdo = new PDO("mysql:host=$host;dbname=$db_name", $db_username, $db_password);
}
catch (PDOException $e)
{
exit('Error Connecting To DataBase');
}
docs