How to remotely connect to OpenShift MySQL database using PHP? - php

I've a Tomcat 7 application running on Openshift server, say myapp.
I've also installed MySQL 5.5 cartridge and got the username and password say myUsername and myPassword respectively.
I tried to remotely connect to the database by writing a simple PHP script from my php localhost.
<?php
$link = mysql_connect('127.8.217.2:3306', 'myUsername', 'myPassword');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_select_db('myapp',$link) or die ("could not open db".mysql_error());
but the output shows
Could not connect: Access denied for user 'myUsername'#'localhost' (using password: YES)
I don't know what i am missing.
Now the question part.
1) Is it possible to remotely connect to the OpenShift MySQL database using PHP from either localhost or some other server ? If yes, How ?
If there's any question, shoot it into the comments.
EDIT
I've looked at port-forwarding with the help of this thread. but that's not i want.

Based on your explanation as having a need for a both Java and PHP application needing to connect to same server, your options are either
1) doing port forwarding or connecting through SSH, both explained in this link I offered as a duplicate of this, or
2) Trying something explained here and here to create a shared database using a scalable application - however, people in other similar threads, like this one, have claimed that it is "for Openshift/Redhat internet environments, not for regular customers of Openshift" (source). Don't know if it is so, but at least if you go this way, you should test from another openshift gear, not from local computer.

Related

Unable to Connect to Mysql Through PDO on Certain Networks

I am trying to connect to a MySQL database through PHP's PDO. It works fine on my home network and some other networks (coffee shops, public internet). When I try to connect through my phone's personal hotspot or through hotel wifi, it no longer works.
I am using the same login information across all of these with the same machine setup (XAMPP VM - Mac). This is my connection file:
$DBO = "mysql:host=$DB_Servername;dbname=$DB_Name";
try {
$conn = new PDO($DBO, $DB_Username, $DB_Password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo "Connection Failed: " . $e->getMessage();
}
This is the error I am getting:
Connection Failed: SQLSTATE[HY000] [2002] Connection timed out
I've tried the server name with the website address and the ip address. The strange thing is when I'm using the VS code database extension, I am able to connect to the database fine (no matter the network I am on). I can't figure out why that would be working but the PDO wouldn't be.
I read this answer about adding a space after 'mysql: ', and it returns this new message:
Connection Failed: SQLSTATE[HY000] [1045] Access denied for user 'dailyrts_realDB'#'localhost' (using password: YES)
The user account I am logging in with is assigned to the correct database and the new IP's I connect to are being registered.
dns_get_record returns:
dns_get_record(): A temporary server error occurred.
I wasn't able to find a fix using XAMPP-VM however, the same code works perfectly when using the XAMPP Installer version of the app on MacOS. I looked around for a bit to see if anyone else was having this problem and I didn't find anything that matches this issue specifically. Most were referring to connecting to a local database.
This answer about accessing MySQL through the root user is pretty close to the issue I was having and they recommend following this guide to fix the issue. I don't need to have the VM version installed so I haven't tested this myself however, it seems to have worked for him.
I'm not sure why the VM version only works on some networks and not others when connecting to MySQL databases. I'm sure this has something to do with port blocking on the network's end (mobile hotspot providers are notorious for blocking ports) and because the VM version handles network traffic differently from the regular version.
It looks like issues with the VM version are not isolated to this specific issue on MacOS. In my personal experience (and others), I'd recommend going with the installer version if you are able to. It is more developed and has more online support surrounding it.

Can't access my AWS hosted database using PHP

I am having a huge issue with accessing my AWS database from within the php code for a site i am building. I have tried many different connection methods, and all seem to give an error that says
Warning: mysqli_connect(): (HY000/2003): Can't connect to MySQL server on '(the endpoint of my hosted DB)' (111)
I've looked up error 111, but I can not seem to understand why it applies to me in this scenario. I can access my database from any computer, using the same information I'm using here to try to connect. I'm starting to think it's the host I'm using for my site that's the problem. I'm using the free biz.nf host just for testing, but I'm not sure my site host really should have any effect on the php inside my site. Here's my connect statement
$mysql_host="DB endpoint";
$mysql_user="myusername";
$mysql_pass="mypassword";
$my_db="myDBname";
$con = mysqli_connect($mysql_host,$mysql_user,$mysql_pass);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
Thanks for the help!
You likely need to update your security group and route tables for your subnets to allow the host on biz.nf to access the RDS instance.
Check your RDS dashboard for the given instance, and examine the settings on your security group and subnets.

Database is there but Wordpress cannot establishing a Database Connection

I am having a server running on my machine and I am calling it over the network. The Server is running fine and there have not been any problems with apache or php yet.
My database is not running on the same machine as apache but in the same local network on a server with the domain "sql.local.network.com".
Now i copied a Wordpress site onto my webserver which has been on another server in the same network, so it should work fine, since the database server is still accessable by both these webservers.
The problem now is, that Wordpress just shows the message Error establishing a database connection.
What I did was to access the database with phpmyadmin which I quickly set up on the server. This worked fine, and I was able to access the database.
Also I found a short skript:
<?php
$link = mysql_connect('sql.local.network.com', 'user', 'password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>
This echoed Connected successfully, which means a simple mysql_connect works fine as well.
Also I am able to ping sql.local.network.com over my console and get the response 192.168.0.101.
Everything works, but the Wordpress-page.
Is there a simple solution to fix the Wordpress connection problem I am missing?
Check the Privileges tab in phpmyadmin and look at the host column. If it's set to "localhost", then that dataabase user can only access the database from the same server as the database. To access remotely, change it to either "%" for any server or to the IP of the server you're trying to connect from.

PHP, MySQL, Lost connection to MySQL server during query, TurnKey Ubuntu/Linux,

Hi to all php and mysql experts,
I try to find some help or 'tutorial' or similar question/answer for my problem but it was unsuccessful.
I have install TurnKey Linux, php, mysql, successfuly width ip address: 172.##.##.## and I can connect to this server from another comp.
I was edit my my.cnf file, I was open port 3306 and I can connect via mysql -u root -p -h 172.##.##.## from another comp. I was add user:root that can connect from any host or IP address. Also I can make successful connections width mysqladmin or mysql workbench.
I can, also, make database and some tables on this server like db_test, tab_test etc. and if I use next script on this server its work very good:
<?php
$db_hostname="localhost";
$db_username="root";
$db_password="pxxxxx";
$db_connect=mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_connect) {
die('Could not connect: ' . mysql_error());
}
mysql_close($db_connect);
?>
But if I use next script it does not work and report to me error: Could not connect: Lost connection to MySQL server during query;
<?php
$db_hostname="172.##.##.##";
$db_username="root";
$db_password="pxxxxx";
$db_connect=mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_connect) {
die('Could not connect: ' . mysql_error());
}
mysql_close($db_connect);
?>
Also this script it does not work if I use another php server witch can see or ping this server.
Can somebody help me to resolve this problem! Please. I am desperate.
I have the same error, please go to xampp\phpMyAdmin\libraries\config.default.php
Modify the following code
existing one - $cfg['ExecTimeLimit'] = 300;
modify to - $cfg['ExecTimeLimit'] = 600;
This is getting a bit long for a comment....
It doesn't appear to be a connection error - that's a different error message.
Changing the configuration of PHPMyAdmin is not going to make any difference to other scripts.
It might help if you publish details of the mysql config from PHP. Also is your mysqld started with skip-name-resolve? How long does your example script take to fail?
One thing to watch for is that when the mysql client sees 'localhost' in the conection string, it tries to connect via a filesystem socket - not a network socket - do you get the same error when you supply the host as 172.##.##.## ?

mysql_connect() using http protocol

I am trying to connect to a MySql server (not localhost) from my computer using the code below. It is giving this error:
Warning: mysql_connect() [function.mysql-connect]: [2002] Connection refused (trying to connect via tcp://10.6.3.6:3306) in on line 7
I wonder if we can use the http protocol to connect instead of tcp that is being used by default? I searched quite a bit on how to change the protocol, but most of the answers were describing how to connect to localhost, and not much about how to connect to another server. Please help.
PS: I am able to connect to the server by going to http://10.6.3.6/phpmyadmin/...). So I am sure the server is up.
My Code
<?php
$db_hostname = '10.6.3.6';
$db_database = 'db_user11';
$db_username = 'db_user11';
$db_password = '########';
$db_server = mysql_connect($db_hostname, $db_username, $db_password);
if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());
mysql_select_db($db_database, $db_server)
or die("Unable to select database: " . mysql_error());
?>
You can't. HTTP is not a protocol suitable for this.
You are talking about 2 processes communicating here (MySQL Server and your app) and they either do it via shared memory, pipes or sockets. Those are the 3 ways that processes communicate with each other.
First of all, HTTP is layered on top of TCP/IP.
In order to connect to something via HTTP, it needs to run an HTTP server. MySQL does not run an HTTP server and there's no current/realistic/supported way to tunnel an SQL connection through HTTP. Even if there was, HTTP is not exactly the best protocol for this.
In short: no. You're trying to solve the wrong problem. You need to configure your MySQL server to allow connections from other machines over the network, give the user you're trying to connect with appropriate permissions to connect from other machines and make sure the MySQL server is reachable from other machines.
Just because the phpmyadmin is hosted on that server, does not neccesarily mean that the database server is in the same IP address. Please ask the domain administrator to give you the details of database server.
Maybe just check and make sure the mysql user account you are connecting with has proper permissions set for remote access.
In other words, this may be obvious but make sure the user has valid remote credentials.
On the command-line ( if you can relate to this ) , one can do this in mysql to add a user allowed to connect from remote connexion:
mysql> CREATE USER 'monty'#'%' IDENTIFIED BY 'some_pass';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'monty'#'%'
-> WITH GRANT OPTION;
This would create a user, monty with password some_pass, with FULL privileges on all databases, all tables from anywhere. The % is the key here, and is needed for remote access.
MySQL users created through gui tools often set the host to 'localhost' and this would not allow a remote connexion.
Hope this helps. Good-luck.

Categories