PHP mysql_connect vs Ubuntu mysql terminal use - php

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?

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.

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".)

Related

How to configure XAMPP to connect to MySQL remotely

I have a problem to connect my online MySQL database from my local system.
I installed XAMPP on my Windows 7 and created a PHP file with this code:
<?php
$db_path = mysqli_connect('printcity24.com', 'printci1_admin', 'xr10s20191', 'printci1_db', '3306');
if(!$db_path) {
echo mysqli_connect_error();
}else{
echo "Connected successfully";
}
?>
Then i created a database on my website : www.printcity24.com
My web host admin configured my host and opened firewall.
When i use XAMPP command line to connect to my database every thing is ok, i can connect to my database remotely with this code :
# mysql -u printci1_admin -p -h printcity24.com
but when i use php code to connect to my database i get this error :
Warning: mysqli_connect(): MySQL server has gone away in D:\Xampp Server\htdocs\st\index.php on line 2
Warning: mysqli_connect(): (HY000/2006): MySQL server has gone away in D:\Xampp Server\htdocs\st\index.php on line 2
MySQL server has gone away
I upload my php code on to other websites and test for connection and everything is "ok" but on my local xampp can't connect.
In this link said i have to use this command :
setsebool -P httpd_can_network_connect=1
but I don't know where to put this code and how to configure my xampp.

ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost' (10061 "Unknown error")

This may be a duplicate question, however I searched internet for a while, and still couldn't find the solution.
I installed xampp on windows 7. The mysql is running on a random port. However, I can't access mysql from command line. I keep getting this error message:
ERROR 2003 (HY000): Can't connect to MySQL server on 'localhost'
(10061 "Unknown error").
I enabled mysql for windows services as administrator, and opened the port that the mysql is using through windows firewall settings. But the problem is still not fixed, and I still can not see the port from http://canyouseeme.org/.
Open a command line with administrative privileges. Type the command below.
netstat -a -p tcp | findstr ":138"
Give it the port number that your sql is using instead of ":138" in the above example. Check if the output contains 0.0.0.0 as the IP or not. If it contains your local area network IP then use that IP to connect. Perhaps MySQL is not using all IPs to listen for connections.
maybe your mysql service is not running.
my solution:
open the run and type 'services.msc'
find the mysql count that you build(note:not the mysql, is the mysql count that you build yourself),then run this service.
Find your Path that you are install xampp server on and open CMD Write that
"C:\xampp\mysql\bin\mysqld"
click Enter and Close , reopen CMd and write
mysql -u root
video

Moving a SIte from Drupal to Raw PHP ... Error: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

I don't understand this the error,
Error: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
I am not familiar with drupal or any frameworks. Please advice me about this error and if I want to host my raw php site which I created in local WAMP server and connect to the database through, ...
#Database Connection
$dbc = mysqli_connect('localhost','****user', '***pw', '***db') OR die('Error: '.mysqli_connect_error());
what should I do?
Check whether mysql is running with the following command:
mysqladmin -u root -p status
And if not try changing your permission to mysql folder. If you are working locally, you can try:
sudo chmod -R 755 /var/lib/mysql/
Reference Connect to Server

MSSQL VIA FreeTDS, ODBC, and Cpanel Unknown host machine name (severity 2)

I have installed FreeTDS 0.91, ODBC, on a Cpanel server running Centos 6.5x64. Everything appears to be running fine and I can connect to the remote MSSQL 2012 server using:
/usr/local/freetds/bin/tsql -S sqlserver -U test -P mypassword
and succesfully execute queries in the database.
I can also connect through:
isql -v sqlserverdatasource test mypasswordhere
But for some reason /usr/local/freetds/bin/tsql -LH server.ip.here
returns no information or errors which doesn't make much sense when it is proven I can connect with the other methods above.
So now when running a test script from a cpanel account on the machine I get:
Unknown host machine name (severity 2)
Here is the test script:
//Database connection function.
function getConnection() {
try {
//$dbconnect = new PDO("sqlserver:Server=server.ip.here,1433;Database=dbname", "user", "password");
$dbconnect = new PDO("dblib:host=server.ip.here,1433;dbname=dbname", 'user', 'password');
} catch (PDOException $e) {
echo "CONNECTION ERROR.<br>Error message:<br><br>" . $e->getMessage();
die();
}
if (!$dbconnect) {
die('Cant connect to database. Please try again later!');
}
else{
echo "i'm in!";
return $dbconnect;
}
}
The first commented line is the old one using sqlserv which I found did not work at all from what i can tell because of the x64 OS. I have also tried with "" around user and pass as well as no marks at all.
php -m does show PDO and pdo-dblib.
Any ideas where I can look next?
Update: This was fixed. I missed in freetds.conf:
[global]
# TDS protocol version
tds version = 8.0
It was originally set to 4.5 instead of 8.
The fix for me was with three steps:
First, I edited /etc/freetds/freetds.conf and changed the tds version like this:
tds version = 8.0
The second step was not entering port number. The port was already 1433, and not specifying it fixed the exact same issue on my case.
Lastly, to connect properly, I had to restart networking as #user1054844 mentioned as this:
/etc/init.d/networking restart
After all these steps, I was able to connect and work with the SQL Server database.
You actually did not need ODBC at all since your connect script is using pdo_dblib not odbc. You can just install FreeTDS than enable pdo_dblib via the compile time flag in rawopts and rebuild via EasyApache. Of course cPanel specifics for this are a bit different.
I just did this for a friend and decided to document it since it is hard to find accurate clear information for FreeTds and pdo_dblib on cPanel.
Guide is here: FreeTDS And pDO_dblib On cPanel

Execute MySql in PHP from terminal (bash)

I have this problem, i have created a php file which inserts data into a mysqldatabase on Mamp.
I can use the command mysql anywhere and it starts the mysql client.
But when i start my php script i get this error:
Warning: Error establishing mySQL database connection. Correct user/password? Correct hostname? Database server running? in
For testing i have used the default username and password "root". Anyone know what causes this?
I have also tried this command:
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock
For me it is working fine. Which hostname you are using in your php code?
sgeorge-mn:~ sgeorge$ cat con_mysql.php
<?php
$con = mysql_connect("127.0.0.1:3306","root","PASSWORD");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
else
{
echo "Connection Established\n";
}
mysql_close($con);
?>
sgeorge-mn:~ sgeorge$ php con_mysql.php
Connection Established

Categories