简体   繁体   中英

Openshift.com - unable to connect with mysql

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

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());
?>

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM