2

I am trying to establish a connection with mysql-5.1, but it shows errors like "could not connect" or when I change the hostname "Could not connect: No such file or directory".

My file reads like this

<?php
$link = mysql_connect('localhost', 'username', 'password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
printf("MySQL server version: %s\n", mysql_get_server_info());
?>

Tried changing mysql_connect to mysqli_connect. No difference.

1) The Mysql is running(as per the interface).

2) tried various rhc commands to restart the app, restart|reload the cartridge etc. - no errors while doing so."eg.Mysql-5.1 restart...done".

3) tried pinging the $OPENSHIFT_MYSQL_DB_HOST and it worked.

4) tried telneting the $OPENSHIFT_MYSQL_DB_HOST and it says connection established and some garbled text.

5) ps aux | grep "mysql" 4213 21721 0.0 0.0 103252 828 pts/0 S+ 10:56 0:00 grep mysql

6)mysql> status

/usr/bin/mysql Ver 14.14 Distrib 5.1.73, for redhat-linux-gnu (x86_64) using readline 5.1

Connection id: 4 Current database: Current user: xxxxxxxxxxxxx@xx.xx.xx.xx SSL: Not in use Current pager: stdout Using outfile: '' Using delimiter: ; Server version: 5.1.73 Source distribution Protocol version: 10 Connection: xxxxxxxx-xxxxxxxxxxxxxxx.rhcloud.com via TCP/IP Server characterset: latin1 Db characterset: latin1 Client characterset: latin1 Conn. characterset: latin1 TCP port: 50986 Uptime: 18 min 40 sec

Threads: 1 Questions: 4 Slow queries: 0 Opens: 17 Flush tables: 1 Open tables: 4 Queries per second avg: 0.3

I changed the code to

<?php
$mysqli = new mysqli('localhost', 'username', 'password', 'dbname');

/*
 * This is the "official" OO way to do it,
 * BUT $connect_error was broken until PHP 5.2.9 and 5.3.0.
 */
if ($mysqli->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') '
            . $mysqli->connect_error);
}

/*
 * Use this instead of $connect_error if you need to ensure
 * compatibility with PHP versions prior to 5.2.9 and 5.3.0.
 */
if (mysqli_connect_error()) {
    die('Connect Error (' . mysqli_connect_errno() . ') '
            . mysqli_connect_error());
}

echo 'Success... ' . $mysqli->host_info . "\n";

$mysqli->close();
?>

again the same error.

Can somebody help me out? Even phpmyadmin code says "could not connect to mysql".

thanks Ganesh Kumar

taro
  • 5,772
  • 2
  • 30
  • 34
  • Please dont use mysql_ extension..go for mysqli or PDO instead...maybe you username and password on the php script are wrong...which user and password did you use to connect to the mysql console...use those same on your php script – Hackerman Apr 11 '14 at 15:14
  • username and pass were the same as given while creating the cartridge. let me just check with mysqli_ – user3524025 Apr 11 '14 at 15:19
  • Should I use localhost or "534xxxxxxxxxx-xxxxxxxx.rhcloud.com" as specified in the $OPENSHIFT_MYSQL_DB_HOST? – user3524025 Apr 11 '14 at 15:25
  • tried both. Here, tested with 534xxxxxxxxx-xxxxx.rhcloud.com" to get this error "Connect Error (2002) php_network_getaddresses: getaddrinfo failed: Name or service not known". And tried with localhost to get "Connect Error (2002) No such file or directory". Any idea? – user3524025 Apr 11 '14 at 15:32
  • You should use connection parameters available as environment variables on your openshift box. OPENSHIFT_MYSQL_DB_HOST, OPENSHIFT_MYSQL_DB_USERNAME, OPENSHIFT_MYSQL_DB_PASSWORD, OPENSHIFT_APP_NAME (as database name). – taro Apr 12 '14 at 13:40
  • It worked after adding the port number.Thanks :-) – user3524025 Apr 12 '14 at 13:40
  • Possible duplicate of [PHP MySQL 5.5 db connection in Openshift](http://stackoverflow.com/questions/22045035/php-mysql-5-5-db-connection-in-openshift) – CodeMonkey May 24 '16 at 01:18

2 Answers2

1

Have you tried the following:

<?php
// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database */
define('DB_NAME', $_ENV['OPENSHIFT_APP_NAME']);

/** MySQL database username */
define('DB_USER', $_ENV['OPENSHIFT_MYSQL_DB_USERNAME']);

/** MySQL database password */
define('DB_PASSWORD', $_ENV['OPENSHIFT_MYSQL_DB_PASSWORD']);

/** MySQL hostname */
define('DB_HOST', $_ENV['OPENSHIFT_MYSQL_DB_HOST'] . ':' . $_ENV['OPENSHIFT_MYSQL_DB_PORT']);

$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
printf("MySQL server version: %s\n", mysql_get_server_info());
?>
MartinB
  • 231
  • 1
  • 5
0

I had the same problem and was able to resolve it by following code.

define('DB_HOST', getenv('OPENSHIFT_MYSQL_DB_HOST'));
define('DB_PORT', getenv('OPENSHIFT_MYSQL_DB_PORT'));
define('DB_USER', getenv('OPENSHIFT_MYSQL_DB_USERNAME'));
define('DB_PASS', getenv('OPENSHIFT_MYSQL_DB_PASSWORD'));
define('DB_NAME', getenv('OPENSHIFT_GEAR_NAME'));

$dbhost = constant("DB_HOST"); // Host name 
$dbport = constant("DB_PORT"); // Host port
$dbusername = constant("DB_USER"); // Mysql username 
$dbpassword = constant("DB_PASS"); // Mysql password 
$db_name = constant("DB_NAME"); // Database name 
CodeMonkey
  • 2,828
  • 1
  • 23
  • 32