[英]php connect to mysql db in cloud 9?
I usually connect php to mysql with localhost in my PC.. now i'm trying to put my project in cloud https://c9.io ,but i can't connect to mysql. 我通常在我的电脑上将php连接到mysql与localhost ..现在我想把我的项目放在云https://c9.io ,但我无法连接到mysql。 i already have mysql database in cloud and put my project in same place...
我已经在云端拥有mysql数据库并将我的项目放在同一个地方......
mysql_connect("/lib/mysql/socket/mysql.sock","myUser","") or die(mysql_error());
i use script above to connect but i get Unknown MySQL server host '/lib/mysql/socket/mysql.sock' (1)
我使用上面的脚本连接,但我得到
Unknown MySQL server host '/lib/mysql/socket/mysql.sock' (1)
what shoul i do ? 我该怎么办?
Okay, so none of the above answers had worked for me, but fortunately I was able to setup a database and get it up and running my own way and I can now make queries and run them successfully, so I will share my method with you in hopes that anyone else scouring the internet can stumble across this and not have to go through the same head scratching that I did. 好的,所以上面的答案都没有对我有用,但幸运的是我能够设置一个数据库并按照我自己的方式运行它,我现在可以查询并成功运行它们,所以我将与你分享我的方法希望任何在网上搜索的人都可以偶然发现这一点,而不必像我那样经历同样的困难。
If you want the quick rundown, just scroll to Step 3 and read on from there. 如果你想快速破解,只需滚动到第3步并从那里继续阅读。 If you're a complete beginner, keep reading as I'll walk you through it in detail.
如果你是一个完整的初学者,请继续阅读,因为我将详细介绍它。
Couple things to mention: 值得一提的事情:
mysql
functions, you have to use mysqli
, since mysql
functions are deprecated and Cloud 9 will not run them. mysql
函数,你必须使用mysqli
,因为不推荐使用mysql
函数,Cloud 9也不会运行它们。 Step 1 : Setup MySQL on Cloud 9 (in Terminal) 第1步 :在Cloud 9上设置MySQL(在终端中)
In your project, open up a New Terminal (click the plus-sign tab above the text editor space, select "New Terminal"). 在您的项目中,打开一个新终端(单击文本编辑器空间上方的加号选项卡,选择“新终端”)。 In the terminal, type
mysql-ctl start
and hit Enter. 在终端中,键入
mysql-ctl start
并按Enter键。 MySQL will start up in the back, but you won't get any response back in the terminal. MySQL将在后面启动,但你不会在终端中得到任何回复。
Next, type mysql-ctl cli
and hit Enter. 接下来,键入
mysql-ctl cli
并按Enter键。 You should see some text that starts off as Welcome to the MySQL monitor...
. 您应该看到一些文本以
Welcome to the MySQL monitor...
Congrats, you've setup MySQL on your Cloud 9 project. 恭喜,您已经在Cloud 9项目上设置了MySQL。
Step 2 : Create a test database (in Terminal) 第2步 :创建测试数据库(在终端中)
You can actually go ahead and create your official database if you like, but for this sake I'll just make a database that holds a table that holds an ID and a username. 如果你愿意,你可以继续创建你的官方数据库,但为了这个缘故,我将创建一个包含一个包含ID和用户名的表的数据库。 So here's the steps to setting up a database and a table.
所以这是设置数据库和表的步骤。 If you've used MySQL and databases before, then this should be cake, but I'll explain it in detail for those who might not fully understand MySQL .
如果您以前使用过MySQL和数据库,那么这应该是蛋糕,但我会详细解释那些可能不完全了解MySQL的人。
SHOW DATABASES;
SHOW DATABASES;
and hit Enter. CREATE DATABASE sample_db;
CREATE DATABASE sample_db;
and hit Enter. Query OK, 1 Row affected.
Query OK, 1 Row affected.
which means the query was successful. sample_db
. sample_db
,我将其命名为sample_db
。 USE sample_db;
USE sample_db;
and hit Enter. sample_db
from the list of databases. sample_db
。 CREATE TABLE users (id INT(11), username VARCHAR(20));
CREATE TABLE users (id INT(11), username VARCHAR(20));
, and hit Enter. users
with two columns: id
and username
. users
的表,其中包含两列: id
和username
。 The number in parentheses represents the character limit the column will store in the database. username
won't hold a string longer than 20 characters in length. username
不会包含长度超过20个字符的字符串。 INSERT INTO users (id, username) VALUES (1, "graham12");
INSERT INTO users (id, username) VALUES (1, "graham12");
, and hit Enter. 1
and a username graham12
in the table. 1
和用户名graham12
在表中。 Since the id
column is an INT
, we do not put quotes around it. id
列是INT
,因此我们不会在其周围加上引号。 SELECT * FROM users;
SELECT * FROM users;
, and hit Enter. users
table. users
表中的所有内容。 The only entry in there should be what we inserted from the last step we just did. Step 3 : Get the credentials you'll need to connect to the database from PHP. 第3步 :获取从PHP连接到数据库所需的凭据。 (in Terminal)
(在终端)
Now we have some data in our table that we can test our mysqli
connection with. 现在我们在表中有一些数据可以测试我们的
mysqli
连接。 But first, we have to get the credentials we will need to connect to the database in PHP. 但首先,我们必须获得在PHP中连接到数据库所需的凭据。 In Cloud 9, we will need 5 credentials to connect:
在Cloud 9中,我们需要5个凭据才能连接:
Username, password, database name, and port #, are practically already known to you by now. 用户名,密码,数据库名称和端口号,现在几乎已为您所知。 I'll explain:
我会解释一下:
SHOW VARIABLES WHERE Variable_name = 'hostname';
SHOW VARIABLES WHERE Variable_name = 'hostname';
, and hit Enter. Variable_name
and Value
. Variable_name
和Value
。 In the Value
column you should see something like yourUsername-yourProjectName-XXXXXXX
, where the X
's are a 7 digit number. Value
列中,您应该看到类似yourUsername-yourProjectName-XXXXXXX
,其中X
是7位数字。 Write this number down or save it some where. SHOW VARIABLES WHERE Variable_name = 'hostname';
. Re-read this step from the beginning if you're confused.) SHOW VARIABLES WHERE Variable_name = 'hostname';
;.重新阅读此步骤从一开始如果你感到困惑。) sample_db
or whatever you named your database; sample_db
或您为数据库命名的任何内容; 3306
. 3306
。 In Cloud 9, all of your projects are wired to 3306
. 3306
。 This is a universal constant of Cloud 9. It will not be anything else. mysqli_connect()
will interpret the port # as a long
data type. mysqli_connect()
会将端口#解释为long
数据类型。 Last Step : Connect to the database with PHP! 最后一步 :使用PHP连接数据库! (using PHP)
(使用PHP)
Open up a PHP file and name it whatever you like. 打开一个PHP文件,并根据自己的喜好命名。
I'll pretend that my host name is graham12-sample_db-1234567
for this example and that this is what my data looks like: 我假装我的主机名是
graham12-sample_db-1234567
这个例子,这就是我的数据:
So in PHP , insert your credentials accordingly: 所以在PHP中 ,相应地插入您的凭据:
<?php
//Connect to the database
$host = "grahamsutt12-sample_db-1234567"; //See Step 3 about how to get host name
$user = "grahamsutt12"; //Your Cloud 9 username
$pass = ""; //Remember, there is NO password!
$db = "sample_db"; //Your database name you want to connect to
$port = 3306; //The port #. It is always 3306
$connection = mysqli_connect($host, $user, $pass, $db, $port)or die(mysql_error());
//And now to perform a simple query to make sure it's working
$query = "SELECT * FROM users";
$result = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($result)) {
echo "The ID is: " . $row['id'] . " and the Username is: " . $row['username'];
}
?>
If you get a result and no error then you have successfully setup a database and formed a connection to it with PHP in Cloud 9. You should now be able to make all the queries you can normally make. 如果您得到结果并且没有错误,那么您已成功设置数据库并使用Cloud 9中的PHP形成了与它的连接。您现在应该可以进行通常可以进行的所有查询。
Note: I demonstrated the last part without using parameterized queries for the sake of being simple. 注意:为了简单起见,我演示了最后一部分而不使用参数化查询。 You should always use parameterized queries when working with real web applications.
在使用真实Web应用程序时,应始终使用参数化查询。 You can get more info on that here: MySQLi Prepared Statements .
您可以在此处获得更多信息: MySQLi准备好的声明 。
For starters, the mysql_* functions are deprecated so you shouldn't be using them. 对于初学者, 不推荐使用 mysql_ *函数,因此您不应该使用它们。 Look at PDO or mysqli instead.
请改用PDO或mysqli 。 Next, you'll want to try this per the example docs :
接下来,您将根据示例文档尝试此操作:
$link = mysql_connect('localhost:/lib/mysql/socket/mysql.sock', 'myUser', '') or die(mysql_error());
To find the ip running you project, create a test file with the code below, run it and put the result as host. 要找到运行项目的ip,请使用下面的代码创建一个测试文件,运行它并将结果作为主机。
<?php
$ip = getenv("REMOTE_ADDR") ;
Echo "Your IP is " . $ip;
?>
You are using Cloud9 so it's a little different to use. 您使用的是Cloud9,因此使用起来有点不同。 To connect to MySQL you have to first create the MySQL server in C9.
要连接到MySQL,您必须首先在C9中创建MySQL服务器。 Type this in C9's command line:
在C9的命令行中键入:
mysql-ctl start
C9 will create your mysql server. C9将创建您的mysql服务器。
MySQL 5.1 database added. Please make note of these credentials:
Root User: <username>
Database Name: c9
Next to find your IP address type: 接下来找到您的IP地址类型:
echo $IP
Now use this code with your username, the ip address, no password and the 'c9' database to access MySQL: 现在使用此代码与您的用户名,IP地址,无密码和'c9'数据库访问MySQL:
mysql_connect("<$IP>","<username>","") or die(mysql_error());
mysql_select_db("c9")
Hope this helps 希望这可以帮助
The documentation show how start, stop, and run the mysql environment. 文档说明了如何启动,停止和运行mysql环境。
Start the MySQL shell mysql-ctl start
then in yor file.php: 启动MySQL shell
mysql-ctl start
然后在yor file.php中:
$ip = getenv("REMOTE_ADDR");
$port = "3306";
$user = "YorUsername";
$DB = "c9";
$conn = mysql_connect('$ip', '$user', '', '$db', '$port')or die(mysql_error());
mysql_select_db('$db','$conn')or die(mysql_error());
mysql_query("select * from YourTableName",'$conn')or die(mysql_error());
The line getenv("REMOTE_ADDR")
return the same local IP as the application you run on Cloud9. 行
getenv("REMOTE_ADDR")
返回与您在Cloud9上运行的应用程序相同的本地IP。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.