PHP / connection to Sphinx requires username / password - php

I am currently trying to connect to Sphinx via PDO or MySqli connector in order to use SphinxQL, however, although Sphinx has no username/password settings and I should be able to connect without the use of such, I get a user/pass error:
Warning: mysqli_connect(): (HY000/1045): Access denied for user ''#'localhost' (using password: NO)
My connection try is as follows (mysqli in this case:)
$con = mysqli_connect('localhost', '', '', '', '9306');
localhost above works with Mysql, I also tried 127.0.0.1 without success (this gave me a connection refused error).
My sphinx conf looks as follows:
listen = 9306:mysql41
searchd is running, and connection via shell works as it should:
[root#localhost vagrant]# mysql -h0 -P 9306
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1 Server version: 2.2.11-id64-release (95ae9a6)
As you can see above, I use Sphinx v.2.2.11
Just in case, I also re-checked the port assignments:
[root#localhost vagrant]# netstat -tulpn | grep LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1475/master
tcp 0 0 127.0.0.1:9306 0.0.0.0:* LISTEN 21599/searchd
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 4013/mysqld
Any help is appreciated.

Weird things happen sometimes, without a plausible explanation. As mentioned, I am using vagrant (Centos 7.5) on a windows 10 host. The connection would not work no matter what. After putting it to rest for the day, my PC went to "sleep" mode. The next day, after revoking the system everything worked fine. What can I say ¯_(ツ)_/¯ . Problem solved.

I might be late to the party, but I had the same issue and I have the solution :)
Instead of
$con = mysqli_connect('localhost', '', '', '', '9306');
use it this way
$con = mysqli_connect('localhost:9306', '', '', '');
And it will most likely work :)
I had the same issue on Sphinx Search 3.3.1, with mysql 8.0.
The above solved my issue.

Related

localhost: socket: mysqli_connect(): (HY000/2002): No such file or directory

When I attempt to use a socket (not TCP) connection to connect to MySQL 5.7.21 using PHP 7.2.4 mysqli_connect() on Centos 7.4 I get an error:
Warning: mysqli_connect(): (HY000/2002): No such file or directory
php.ini
mysqli.default_socket = /tmp/mysql.sock
my.cnf
[mysqld]
socket=/tmp/mysql.sock
...
[client]
socket=/tmp/mysql.sock
MySQL connection from command line works fine:
/usr/local/mysql57/bin/mysql --socket tmp/mysql.sock --host localhost -u root -p
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.21 MySQL Community Server (GPL)
ls -l /tmp/my*
srwxrwxrwx 1 mysql mysql 0 Apr 19 22:01 /tmp/mysql.sock
-rw------- 1 mysql mysql 5 Apr 19 22:01 /tmp/mysql.sock.lock
ps -edf | grep mysql
mysql 3593 3329 0 22:01 ? 00:00:03 /usr/local/mysql57/bin/mysqld --defaults-file=/usr/local/mysql57/my.cnf --basedir=/usr/local/mysql57 --datadir=/mysql57 --plugin-dir=/usr/local/mysql57/lib/plugin --log-error=myhost.err --pid-file=myhost.pid --socket=/tmp/mysql.sock --port=3306
$dblink = mysqli_connect('localhost', 'root', 'rootpass', 'testdb', 3306, '/tmp/mysql.sock');
if (mysqli_connect_errno($dblink)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
MySQL generic tar.gz was installed. I'm using nginx 1.12.2. If I use 127.0.0.1 or locahost:3306 such that TCP is used it works fine. Why doesn't socket work?
Update #1
I have another Centos 7.4 image with MySQL 5.7.11 which works with socket. However, I don't start it using systemctl. I also noticed that even though it's running there is no /tmp/mysql.sock.lock file, just /tmp/mysql.sock
Other than that it's mostly the same, in fact, /tmp/mysql.sock isn't specified anywhere in php.ini nor my.cnf files.
In my case solution was to change localhost to 127.0.0.1
I was connecting from WSL to Windows Instance of MySQL Server
This could be a simple permissions problem. Seems like only root/current user/sudo has rights to the tmp folder (which is why terminal works and php doesn't).
Also, as a reference to my own Centos 7 mysql setup:
my datadir and socket locations are identical hence why mysql has access to the .sock file
--datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock
You want to connect to mysql via socket in your code
$dblink = mysqli_connect('localhost', 'root', 'rootpass', 'testdb', 3306, '/tmp/mysql.sock');
try to replace this with:
$dblink = mysqli_connect('/tmp/mysql.sock', 'root', 'rootpass', 'testdb', 3306);
there is a solution available here as well https://serverfault.com/questions/673854/how-to-let-php-connect-to-database-by-using-unix-socket-joomla hope this helps.
In my case I used strace /usr/bin/php test.php and found more info:
connect(3, {sa_family=AF_UNIX,
sun_path="/var/run/mysqld/mysqld.sock"}, 29) = -1 ENOENT (No such file
or directory)
This was due to me running inside a jail with no access to the local socket file so I was forced to use TCP protocol by changing localhost to 127.0.0.1 in the php code.

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

php pdo in linux, not able to connect to remote mysql server but in wamp it's working fine [duplicate]

I am facing a weird problem here
we have a server A where the app files are stored
and B server with database
Tried to connect via command prompt from server A to B using the command
mysql -h xx.xx.xx.xx -u root -p password - and it worked
NOw i tried to create a php script in server A to connect to server B
the command is
$this->db=new PDO('mysql:host=xx.xx.xx.xx;dbname=databasename','root','password');
Connection failed: SQLSTATE[HY000] [2003] Can't connect to MySQL
server on 'xx.xx.xx.xx' (13) Fatal error: Uncaught exception
'Exception' with message 'SQLSTATE[HY000] [2003] Can't connect to
MySQL server on 'xx.xx.xx.xx' (13)'
Unable to find a solution on this.
Can any help on this?
thank you
I got it working by running a command in the database server :)
setsebool httpd_can_network_connect_db=1
thanks for the replies yycdev
Try specifying the port in your connection string and ensure the database server is set to allow remote connections and the port is open on your firewall (both of these I suspect are already done as you are able to connect via the terminal but it never hurts to verify and check things).
Change your PDO connection and add the port=3306 or if you're using MAMP use port 8889
$this->db=new PDO('mysql:host=xx.xx.xx.xx;port=3306;dbname=databasename','root','password');
Another thing to check is if – SELinux is blocking network connections. Login as root and run
setsebool -P httpd_can_network_connect=1
I don't know much about that, but try to place the port in back of the script:
$this->db=new PDO('mysql:host=xx.xx.xx.xx;dbname=databasename','root','password',3306);

Can't connect to remote MySQL server (uses default server instead)

I'm trying to connect to an AWS MySQL server from my VPS, but no matter what I try, I just get access denied, my user has privileges to connect from any host.
If I try to connect via PHP:
$db = new mysqli('mysite.cufncdsjslka.us-east-1.rds.amazonaws.com', 'my_user', 'my_pass', 'my_db');
Gives me: Warning: mysqli::mysqli(): (28000/1045): Access denied for user 'my_user'#'XXXX.myhost.com' (using password: YES) in /home/me/shopify/libraries/db.lib.php on line 4
If I try to connect via the MySQL CLI:
mysql -h mysite.cufncdsjslka.us-east-1.rds.amazonaws.com -umy_user -p
Gives me:
ERROR 1045 (28000): Access denied for user 'my_user'#'xxxx.myhost.com' (using password: YES)
This command works flawlessly on my PC, but not on my VPS.
I'm assuming it's some sort of security setting preventing MySQL from using external servers?
My VPS is running Debian squeeze with the LAMP components all from the standard Debian packages except for PHP which I manually updated to 5.4.
I'm posting this as a new question since most similar questions I've found on SO were a permissions issue (I've given my MySQL user priveliges to connect from any host) or a SELinux issue (which AFAIK doesn't apply to Debian)
OK.
I have seen this problem number of times for many different reasons. Since I cannot see your server or your logs so I am guessing from the information that there could be bind address issue.
In my.cnf check
bind-address=YOUR-SERVER-IP
change it to the the ipaddress of your mysql server for example 81.92.33.45
restart mysql after this. And if you are still unable to connect then let me know with the error info from log and I will look again.
Secondly make sure iptables are not causing problems.
iptables -A INPUT -i eth0 -p tcp --destination-port 3306 -j ACCEPT

php can't connect to mysql with error 13 (but command line can)

I have weird situation in newly installed server, and it seems that Google can't help me this time.
I can't connect to (remote) mysql from my php-code. When I try to connect from command line on the same server the connection succseds.
Could not connect: Can't connect to
MySQL server on 'MYSQL.SERVER' (13)
Here is the code and the connect attempt from the command line
[u1#bosko httpdocs]$ cat test.php
<?
$link = mysql_connect('MYSQL.SERVER', 'testusersimon', '123456');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
[u1#bosko httpdocs]$ mysql -h MYSQL.SERVER -utestusersimon --password=123456
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 352108
Server version: 5.0.45-community-nt-log MySQL Community Edition (GPL)
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
This software comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to modify and redistribute it under the GPL v2 license
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> quit
Bye
I tried running the php script both in mod_php mode and in FastCGI,
check that "/etc/php.d/mysql.ini" shows up in the phpinfo() as well as mysql,mysqli and pdo_mysql sections.
but the result was the same, I know its something simple but I just can't .
Please help :)
Edit:
The problem was with SElinux
setsebool -P httpd_can_network_connect_db=1
Was the solution.
setsebool -P httpd_can_network_connect=1
will also be a helpful CLI command to many people visiting this question, as to allow mysql_connet() connections from within HTTP (Apache) requests to a remote MySQL database server, ensure to enable Network Connections from httpd in SElinux usually located in /etc/selinux/config (disabled by default to prevent hackers from attacking other machines using your httpd).
On CentOs 6, you can use the following (without -P)
setsebool httpd_can_network_connect=1
On Fedora 21 with apache 2/httpd version 2.6 using php version 5.6 when connecting to a remote mysql server 5.6 or mariadb version 10. It even seems to be a problem connecting to local server when specifying the server's FQDN instead of localhost in the php code.
This command will fix the permissions problem for the current session:
setsebool httpd_can_network_connect_db on
To make the fix permanent for subsequent reboots you need to do this:
setsebool -P httpd_can_network_connect_db on
Thanks to all on this question for rescuing me from "permission denied" hell. :)

Categories