Cloud 9 IDE无法连接到数据库 [英] Cloud 9 IDE can't connect to database
问题描述
我已经尝试了很多方法来连接到cloud 9
中的数据库,但是我仍然遇到类似的错误.
I have tried a number of things to connect to my database in cloud 9
but I keep getting similar errors.
这是我的PHP代码:
<?php
// Create connection
$con=mysqli_connect($IP, "$C9_USER", "", "c9");
//(host,username,password,dbname)<- guide for me
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
我从w3schools获得了基本代码,并将其与解释了Cloud 9的mysql数据库如何工作的文档一起使用: https://docs.c9.io/setting_up_mysql.html
I took the basic code from w3schools and I used it with a document explaining how cloud 9's mysql database works: https://docs.c9.io/setting_up_mysql.html
但是如果没有出现以下错误,我似乎无法连接:
But I can't seem to connect without getting the follow error:
Failed to connect to MySQL: Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
所以我想知道是否有其他方法可以将套接字更改为cloud 9建议的地址:注意: MySQL socket file can be found in ~/lib/mysql/socket/mysql.sock
So I'm wondering if there's a different way to change the socket to the address that cloud 9 recommended: Note: MySQL socket file can be found in ~/lib/mysql/socket/mysql.sock
推荐答案
使用Cloud 9 IDE时,本文未定义.
When using Cloud 9 IDE, the php variables $ID
and $C9_USER
mentioned in this article are not defined.
为了检索这些变量以在代码中使用,必须使用cloud 9 ide
终端,方法是按 ALT + T 并输入:
echo $ID
echo $C9_USER
In order to retrieve these variables for use in your code, you must use the cloud 9 ide
terminal by pressing ALT + T and entering:
echo $ID
echo $C9_USER
然后采用这些值并将其放在php代码中的变量中,如下所示:
Then take those values and place them in a variable in your php code like so:
<?php
// Create connection
$IP = "value from terminal";
$C9_USER = "value from terminal";
$con=mysqli_connect($IP, $C9_USER, "", "c9");
//mysqli_connect(host,username,password,dbname); << guideline
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
侧注意: :确保在运行mysql代码时已打开数据库.您可以通过在终端中输入mysql-ctl start
来打开它.
这篇关于Cloud 9 IDE无法连接到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!