dongle7637 2018-08-04 04:56
浏览 77

PHP无法在基于Ubuntu的自定义S2I映像中连接到MySQL

Playing with Openshift origin 3.9 on my custom servers. So far it has been a pleasant experience. I've been building a custom s2i image based on Ubuntu for my LEMP stack.

I'm not able to connect to the MySQL database. I always get an error saying:

Failed to connect to MySQL: (2002) No such file or directory

Here's my PHP code:

$mysql_database = getenv("MYSQL_DATABASE");
$mysql_server_name =getenv("MYSQL_HOST");
$mysql_username = getenv("MYSQL_USER");
$mysql_password = getenv("MYSQL_PASSWORD");
$mysql_port = getenv("MYSQL_PORT");
$mysqli = new mysqli($mysql_server_name, $mysql_username, $mysql_password, $mysql_database, $mysql_port);
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
    die();
}

Some observations:

I'm able to connect from MySQL CLI client from inside the pod.

enter image description here

The same app/code works fine with the official openshift PHP s2i image.

Am I missing anything in my s2i?

  • 写回答

1条回答 默认 最新

  • duanha3539 2018-08-04 04:58
    关注

    Check your phpinfo() and search for MySQLi extension. If you can't find, that means you do not have the extension installed yet and it needs to install.

    try installing from terminal

    (Ubuntu/Mint) $ sudo apt-get install php(version)-mysql
    

    replace (version) with any of your php version number. For example, php7.0 / php7.1, etc.

    To check phpinfo(), create a phpinfo.php file which you can access from url with the content

    <?php
    phpinfo()
    

    If you have everything and still unable to connect? Try checking your mysql.sock file path.

    Open the php.ini file and find this line:

    mysql.default_socket

    And make it (know where your mysql.sock located)

    mysql.default_socket = /path/to/mysql.sock

    评论

报告相同问题?