PDO Sphinx connection user rights (SELINUX issue) - php

I have an issue that is most probably a user rights issue.
I have a php script that connects using PDO to a sphinx db.
$sp = new PDO('mysql:host=127.0.0.1;port=9306;dname=', '', '');
When I run it from terminal using root account it works fine. But,
when I attempt to run it through browser as user apache I get the following error:
[Thu Mar 20 11:22:51 2014] [error] [client 98.12.26.274] PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2003] Can't connect to MySQL server on '127.0.0.1' (13)' in /var/www/html/surveys/test.php:3\nStack trace:\n#0 /var/www/html/surveys/test.php(3): PDO->__construct('mysql:host=127....', '', '')\n#1 {main}\n thrown in /var/www/html/surveys/test.php on line 3
There has to be something that is missing the propoer user rights but I have no clue what files I need to give rights to for this to work.
UPDATE:
I realized that selinux was blocking the port. How do I enable that port for apache using selinux?

Just use a socket instead of the IP address? See the manual for an example. This circumvents the network stack altogether.
Put something like
listen=/tmp/mysql_sphinx.sock
into the sphinx.cnf and put the same socket into the DNS of PDO like
$db = new PDO('mysql:dbname=testdb;unix_socket=/tmp/mysql_sphinx.sock');
This frees you from the need to make MySQL accessible over the network too. This is better taking security into account.

Related

Mysql PDO access denied for remote connection but Mysql CLI works

I am trying to connect to a remote Mysql server from my web server. I followed the following steps:
On the remote server:
1. “CREATE USER ’newremoteuser’#‘web-server_ip' IDENTIFIED BY 'remote_user_password’;”
2. “GRANT ALL PRIVILEGES ON *.* TO 'newremoteuser'#'web_server_ip’;”
3. “FLUSH PRIVILEGES;”
On the web server when I try to access using Mysql CLI, the connection is successful.
"mysql -u newremoteuser -h remote_server_ip -p"
However when I try to connect to remote server using PDO or Mysqli in PHP, I get the error
"FastCGI sent in stderr: "PHP message: PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[28000] [1045] Access denied for user 'newremoteuser'#'my-domain-name.com' (using password: YES)'
Below is the PDO code
$REMOTEPDO = new PDO("mysql:host=$remote_server_ip;port=3306;dbname=$remote_db", $newremoteuser, $remote_user_password);
$REMOTEPDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
I think it is denying access to 'newremoteuser'#'my-domain-name.com' as the remote user I added was 'newremoteuser'#'web-server_ip'. However, I can not find a solution to this. Can anyone please point me in the right direction?
Note: 'my-domain-name.com' is my website name hosted on my 'web_server_ip’ server.
Remote server is Ubuntu 18 and Web server is Ubuntu 14.
As you've pointed out, changing the host for that user might fix your problem since the connection denial seems to be because of a host mismatch. Try updating the host for your existing user from the IP address to the domain name:
UPDATE mysql.user SET Host='my-domain-name.com' WHERE User='username';
FLUSH PRIVILEGES;
If it still doesn't work, it might be worth updating the host to '%' instead and seeing if it then lets you connect.
I just went through this. I could connect with command line but not PDO. My webserver client wouldn't connect until I enabled httpd_can_network_connect_db on the client server's SELinux and rebooted.
sudo setsebool -P httpd_can_network_connect_db 1
I forgot the reboot and chased my tail a bit longer than necessary.

Ratchet and WSS not working properly

After a research i'm gonna try to explain the whole process i've been trough to try to solve the problem.
First, the idea was to make a WebSocket connections with the users of my app with the server to basically share some information, all this process works perfectly if we are not under SSL.
The moment my server was secured, i got two messages, first, i couldn't do a connection to a non secure WebSocket Service under SSL, obvious reasons. Then i tried the secure connection and also failed with a 400 hand shake failure.
Then i started my research about the problem, i saw that ratchet documentation didn't tell me anything new about this matter, at least not that could find. After that, i found this.
php ratchet websocket SSL connect?
I asked my provider to install and configure this Apache modules (Apache version 2.4.25, just in case that gives any valuable information). The configuration worked since now i can make the connection with the ws://www.website.com /ws/ and works fine. But now when i try wss connection, i get a timed out response:
/* Apache error
[Mon Feb 06 16:49:02.966991 2017] [cgi:error] [pid 8969] (70014)End of file found: [client ipadress:port] AH01225: Error reading request entity data
[Mon Feb 06 16:51:42.530383 2017] [cgi:error] [pid 9837] (70014)End of file found: [client ipadress:port] AH01225: Error reading request entity data
*/
And the last thing, about the Stunnel configuration, im not sure what ports should i use on those variables.
https://www.stunnel.org/config_unix.html
[websockets]
accept = 8443
connect = 8888 //i guess here the port where my WS is running?
For now, with all this information and the research i did, i'm not able to find any other clue that could help me solve this.
Thanks in advance.
Please check the answer here, where I share how to configure Apache server to access wss:// and ws:// at the same time.
PHP websocket on SSL with proxy_wsTunnel - Apache
After hours of strugling with Radchet i've got it working on a new Windows 2016 machine with IIS 10.0.
My Ratchat server is started from the commandline, and is running on localhost:8585
Using Windows 2016 with IIS10.0, installed the Websocket feature (but not used in IIS).
Created a new website, with HTTPS binding on port 443.
This site is a dummy site, no html / web files.
Create a Application Proxy rewrite rule, see web.config:
The client can connect with the wss://yoursertcomain.com:443 and IIS will pass the requests to/from Ratchet.
Our application is now over WSS up-and-running.
PS: Using the loopback IP-adress 127.0.0.1 instead of 'localhost', otherwise connecting takes 1 second, now it's serveral ms.

Not able to access mysql server using workbench on local network connection

When I connect to the network using the local network connection I am unable to establish a connection to my server. It returns the following error:
Warning: mysqli::mysqli(): (HY000/2002): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in C:\xxxx\htdocs\xxxx.xxxx\class\class.mysql.php on line 11
Connect Error (2002) A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
Warning: mysqli::close(): Couldn't fetch mysql in C:\xxxx\htdocs\xxxx.xxxx\class\class.mysql.php on line 59
Now when I tether through my phone I have no problems connecting to the server.
My knowledge of networking is extremely ignorant so do forgive me if this is obvious, but what could some of the issues be stopping my connection to the server?
I have figured out the answer to the problem I was experiencing. I am documenting below, just in case it helps anyone else.
=========================
I recently came across a problem when working through the 4th edition of Kevin Yank's "Build Your Own Data-Driven Website."
There appears to be an incompatibility between certain software packages and operating systems. In particular, this affects PHP 5.3 when trying to connect to MySQL while running on Windows Vista with Apache. It is not really a bug, but a discrepancy between how far along the different elements are in the transition from IPv4 to IPv6.
Basically, when PHP tries to connect to the database, it times out because it does not recognize localhost, only 127.0.0.1, and produces the following error message:
=========================
Warning: mysqli_connect() [function.mysqli-connect]: [2002] A connection attempt failed because the connected party did not (trying to connect via tcp://localhost:3306) in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\phpmysql-4\chapter4\connect\index.php on line 2
Warning: mysqli_connect() [function.mysqli-connect]: (HY000/2002): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\phpmysql-4\chapter4\connect\index.php on line 2
Fatal error: Maximum execution time of 30 seconds exceeded in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\phpmysql-4\chapter4\connect\index.php on line 2
=========================
There are two possible solutions to this problem:
=========================
Replace "localhost" with "127.0.0.1" in all PHP files that you wish to have connect to a MySQL database
Locate the "hosts" file on your computer. It is typically at a location such as this:
C:\Windows\System32\drivers\etc\hosts
Open it up, and comment out the line that prevents the localhost from "mapping" correctly; in otherwords, change
::1 localhost
to
::1 localhost
This solution was, in fact, presented in the SitePoint forums previously, though without a full explanation of the details.
http://www.sitepoint.com/forums/showthread.php?t=637612
=========================
The issue is described in detail on the PHP forum at the following link:
http://bugs.php.net/bug.php?id=45150
Please note that -- from what I understand -- Windows XP does not have this problem, as it is configured only to IPv4. Vista runs into a problem because it is designed to handle both, as is Windows 7.
Also, I should mention that using PHP 5.2 with the aforementioned book is not recommended because there is a bug in that version -- a veritable one -- that causes Apache to crash when it attempts to open the "deletejoke" file.

MySQL access denied in PHP, CLI will connect

At first I tried just connecting with PDO in PHP. However, I get access denied messages. I can use the exact same commands to connect to the command line interface, but php gives this error from PDO:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [1045] Access denied for user 'avt_root'#'localhost' (using password: YES)' in C:\inetpub\wwwroot\index.php:3 Stack trace: #0 C:\inetpub\wwwroot\index.php(3): PDO->__construct('mysql:host=loca...', 'avt_root', '[**]') #1 {main} thrown in C:\inetpub\wwwroot\index.php on line 3
$db = new PDO("mysql:host=localhost;dbname=AVT_TIME", "avt_root", "[**]");
So next I tried just connecting with mysql_connect to troubleshoot maybe having the PDO parameters incorrect:
$mysql = mysql_connect("localhost", "avt", "[**]");
However, even this gives me pretty much the same error:
Warning: mysql_connect(): Access denied for user 'avt'#'localhost' (using password: YES) in C:\inetpub\wwwroot\index.php on line 2
Server Configuration:
IIS7.0 with PHP running under fast_cgi and MySQL installed with correct extensions chosen in php.ini file.
Any and all help is appreciated, minus any comments regarding correct username and password, I have checked and triple checked for both the avt_root and avt accounts. The password for both is actually the same, and both can log in via the CLI over remote desktop.
localhost can be a little wonky in mysql-land. The standard mysql interface library, which mysql_*() functions use, internally redefine localhost to be a local unix-domain socket connection. This is purey for efficiency, as unix sockets do not have the overhead that TCP sockets do.
PDO, which is probably using mysqlnd, will not have that problem. localhost will mean 127.0.0.1 and it'll be trying to use a TCP socket.
Make sure that your avt account is set up as avt#127.0.0.1 to allow TCP connections.
I eventually gave up on this and completely reinstalled MySQL and am just going to use the root account, as we have high enough security that I don't have to worry too much about someone messing with an intranet only site.

mysql_connect() is not getting to the correct server

I have a system with a globally open mysql connection to the local server. In one of my files I am opening an additional connection to a remote machine.
Wieldly instead of trying to connect to that machine, I get an access denied message from my ISP (it seems to be trying to connect to the database on that machine).
I am trying to connect using:
$cust_conn = mysql_connect($host,'root','##password##');
I have tried subdomain.domain.com:3306, subdomain.domain.com and ip:3306 as the value for $host.
The wierd this is the response i get:
Warning: mysql_connect(): Access denied for user 'root'#'my.isp.com' (using password: YES) in /var/www/html/report/module/sql_view.php on line 19 Error: Could not connect to database:
Any ideas why this would happen? It seems like for some reason my script is attempting to connect to my ISPs server, instead of the one passed in $host.
The host given in the error message is the host it's trying to connect from, not to.
that hostname in the error (my.isp.com) is your client's host...remote root access is often disabled, or perhaps the pass/host combo is wrong
to add the creds:
http://dev.mysql.com/doc/refman/5.1/en/adding-users.html

Categories