Our school let us sue a small database that I am trying to access through the school network.
Connecting using
mysql -D ddddd -h hhhh -u uuuu
works fine (Note that contrary to multiple questions on this site, hhhh is not localhost). However, using
new PDO('mysql:host=hhhh;dbname=dddd, uuuu, pppp)
does not, and brings up an error:
error: 'Can't connect to local MySQL server through socket '/tmp/mysql.sock''
How do I locate this socket? (NB: In case it is relevant, the schools computers run on FreeBSD and I only have a very limited permissions).
the error:
error: 'Can't connect to local MySQL server through socket '/tmp/mysql.sock''
means that the PDO is trying to connect to MySQL via local socket (linux socket is file).
But my guess is that you want to connect via hostname/ip. Your code Has to be wrong. From the example it looks like you are using php. Try double check everything.
Here is an example from php.net:
<?php
/* Connect to an ODBC database using driver invocation */
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>
Use correct IP address to connect.
If u r using localhost then by default socket will be used on Linux so
Use 127.0.0.1 as host instead of localhost.
Related
I am running mysql on a linux server. I have the linux server IP and mysql runs on localhost on that linux server. Now how do I access mysql running on localhost server remotely?
I can do this by ssh into linux server then running mysql, but how would I do it with in a php script? For example...
<?php
$servername = "localhost";
$username = "username";
$password = "password";
try {
$conn = new PDO("mysql:host=$servername;dbname=myDB", $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();
}
?>
(this script only connects to mysql directly which is not possible remotely)
In my script I would need to access linux server then mysql server unless there is a way to access mysql directly (remotely). How is this achievable?
You need change bind address in my.cnf and restart your mysql service.
Official documentation:
https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_bind_address
If the address is 0.0.0.0, the server accepts TCP/IP connections on all server host IPv4 interfaces.
By default all connections restricted localhost.
In order to access your mysql instance from your server; you need to install workbench (or some other mySql explorer) on your development machine.
Create a connection to mysql instance using server ip address as host name and provide username and password to open connection. If every thing goes well you will see your databases in explorer.
Im having real trouble getting this to work. I have got the mssql-connect drivers set up and working on the Linux server, it now understands that function and does not return any errors.
I have had our server management team configure the windows server to allow the connection from the linux server. They have tested this and confirm it is all working, however I just cant seem to connect to it with PHP.
I have tried various connection strings, but it won't connect, here is an example
<?php
// Server in the this format: <computer>\<instance name> or
// <server>,<port> when using a non default port number
$server = '214.133.182.71,1443';
// Connect to MSSQL
$link = mssql_connect($server, '****', '******');
if (!$link) {
die('Something went wrong while connecting to MSSQL');
}
?>
I always get:
Warning: mssql_connect() [function.mssql-connect]: Unable to connect to server: 214.133.182.71,1443 in /home/v3rec/public_html/test.php on line 10
Something went wrong while connecting to MSSQL
The username and password of that for the MS SQL database. Ive tried it with and without the port name. I am running SQL 2012, im not sure what the instance name would be? Am I missing something? Why is PHP unable to connect?
May I suggest you use PDO and do this:
$dbhost = "myhost";
$dbport = 10060;
$dbname = "tempdb";
$dbuser = "dbuser";
$dbpass = "password";
try {
$dbh = new PDO ("dlib:host=$dbhost:$dbport;dbname=$dbname","$dbuser","$dbpass");
} catch (PDOException $e) {
echo "Failed to get DB handle: " . $e->getMessage() . "\n";
exit;
}
Im trying to acess an EC2 mysql server from another EC2 instance using PHP.
$con = mysqli_connect('elastic_ip_host','user','pass','database');
if(!$con){
echo mysqli_connect_error(); echo mysql_error();
}
//Also trying PDO
try
{
$dbcon = new PDO('mysql:host=elastic_ip_host;dbname=database','user','pass');
$dbcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo 'Connected!';
}
catch(PDOException $e)
{
echo 'ERROR! '.$e->getMessage();
die();
}
Can't connect to MySQL server on 'elastic_ip_host' (13)ERROR! SQLSTATE[HY000] [2003] Can't connect to MySQL server on 'elastic_ip_host' (13)
Using MySQLWorkbench app in my computer, Im connecting pretty fine using Elastic IP (number). Just PHP is not connecting. Checking phpinfo I see it has pdo and mysqli all ok.
Do you have any idea?
(Also Im using percona and I run that command first install that ask if I will not be able to connect remotly with root. But I created a new user as %. Thats what I am using in MySqlWorkbench program from my computer to access the database fine)
Running:
setsebool -P httpd_can_network_connect=1
As root in ssh fixed my problem :)
I've mysql database and php/apache on two different servers: let's say hostphp.domain.com and hostmysql.domain.com.
On the mysql server I've set a user "my_user" with permissions to connect to "my_database" db from the specific host "hostphp.domain.com".
When I connect to it using mysql_connect it does right. But when I do it via php PDO I get this error:
SQLSTATE[42000] [1044] Access denied for user 'my_user'#'%' to database 'my_database'
I've done some tests and I found the problem is ...#'%', mysql is refusing that connection because "my_user" does not have permission to connect from any host.
Also I've tried to connect using mysql_connect with a wrong password to see the error and I get this:
Could not connect: Access denied for user 'my_user'#'hostphp.domain.com' (using password: YES).
The difference is in ..#'%' and ...#'hostphp.domain.com'.
So that's my question, why php pdo do not declare hostname when connecting to a remote host? (or is doing that wrong).
Thanks and sorry for my english.
Edit.
Some code example, this does not work:
try {
$pdo = new PDO(
'mysql:host=hostmysql.domain.com;port=3306;dbname=my_database',
'my_user',
'my_pass'
);
} catch (PDOException $e) {
die($e->getMessage());
}
but this works ok:
$conn = mysql_connect('hostmysql.domain.com', 'my_user', 'my_pass');
if (!$conn) {
die('Could not connect: ' . mysql_error());
}
You have successfully connected to the host using mysql_connect, but you got no errors because you did not attempt to select the database.
Your user probably doesn't have access to your database.
Try running mysql_select_db("my_database"); after connected to the host and you should get the same error.
This is the standard code when using windows authentication:
<?php
try {
$conn = new PDO("sqlsrv:Server=geoffrey-pc\SQLEXPRESS;Database=books",
NULL, NULL);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo $e;
die("Error connecting to SQL Server");
}
echo "Connected to SQL Server\n";
?>
The above works for connecting to the local server (SQL Server 2008 Express Edition), but not for connecting to a server on the network (SQL Server Standard Edition). The error message is:
exception 'PDOException' with message 'SQLSTATE[28000]:
[Microsoft][SQL Server Native Client 10.0][SQL Server]Login failed for
user 'myDomain\GEOFFREY-PC$'.' in C:\wamp\www\PhpProject1\index.php:10
Stack trace: #0 C:\wamp\www\PhpProject1\index.php(10):
PDO->__construct('sqlsrv:Server=n...', NULL, NULL) #1 {main}
The connection string for the network server is the same as for the local server, except that the server name is similar to abc0120 and does not end with \SQLEXPRESS or anything else, and the database name is different. The database name I use with the network server does exist.
I am using Apache 2.2.11. The SQLServer 2005 MSDN page for How to: Connect Using Windows Authentication reads:
The credentials under which the Web server's process (or thread) is running must map to a valid SQL Server login in order to establish a connection.
Maybe that is the problem.
I can connect to the network server using SQL Server Management Studio also using windows authentication.
From the PHP manual, you have this example :
<?php
/* Connect to an ODBC database using driver invocation */
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';
try {
$dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
In your example, you omit the $user and $password arguments. I would try and specify a username and password instead of passing null; the user running your PHP script might not be the same as the logged user... omitting to provide a username and password, PHP tries to provide the user's credentials who's running the script (with perhaps even no password at all).