简体   繁体   English

php连接到云端9中的mysql数据库?

[英]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: 值得一提的事情:

  • You will have to setup a database via a Terminal in Cloud 9. I had no experience prior doing it in a Terminal before, but it's very simple to learn. 您将不得不通过Cloud 9中的终端设置数据库。我以前没有经验在终端之前做过,但它很容易学习。
  • You can not use 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的人。

  1. Type SHOW DATABASES; 输入SHOW DATABASES; and hit Enter. 并按Enter键。 This will show a list of current databases within your project. 这将显示项目中当前数据库的列表。 You can enter this any time you want to see a list of your databases on the current project. 您可以在任何时候想要在当前项目中查看数据库列表时输入此项。
  2. Type in CREATE DATABASE sample_db; 输入CREATE DATABASE sample_db; and hit Enter. 并按Enter键。 You should get a Query OK, 1 Row affected. 你应该得到一个Query OK, 1 Row affected. which means the query was successful. 这意味着查询成功。 You can name the database whatever you like, but for this little walk-through, I named it sample_db . 您可以根据自己的喜好命名数据库,但是对于这个小小的sample_db ,我将其命名为sample_db
  3. Type in USE sample_db; 输入USE sample_db; and hit Enter. 并按Enter键。 This selects sample_db from the list of databases. 这将从数据库列表中选择sample_db
  4. Type in CREATE TABLE users (id INT(11), username VARCHAR(20)); 输入CREATE TABLE users (id INT(11), username VARCHAR(20)); , and hit Enter. ,然后按Enter键。 This creates a table named users with two columns: id and username . 这将创建一个名为users的表,其中包含两列: idusername The number in parentheses represents the character limit the column will store in the database. 括号中的数字表示列将存储在数据库中的字符限制。 In this case for example, username won't hold a string longer than 20 characters in length. 例如,在这种情况下, username不会包含长度超过20个字符的字符串。
  5. Type in INSERT INTO users (id, username) VALUES (1, "graham12"); 输入INSERT INTO users (id, username) VALUES (1, "graham12"); , and hit Enter. ,然后按Enter键。 This will add the id of 1 and a username graham12 in the table. 这将增加的id 1和用户名graham12在表中。 Since the id column is an INT , we do not put quotes around it. 由于id列是INT ,因此我们不会在其周围加上引号。
  6. Type in SELECT * FROM users; 输入SELECT * FROM users; , and hit Enter. ,然后按Enter键。 This will show everything that is in the 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个凭据才能连接:

  1. Host name 主机名
  2. Username 用户名
  3. Password 密码
  4. Database name 数据库名称
  5. Port # 港口 #

Username, password, database name, and port #, are practically already known to you by now. 用户名,密码,数据库名称和端口号,现在几乎已为您所知。 I'll explain: 我会解释一下:

  1. Host name - Type in SHOW VARIABLES WHERE Variable_name = 'hostname'; 主机名 - 键入SHOW VARIABLES WHERE Variable_name = 'hostname'; , and hit Enter. ,然后按Enter键。 You'll get a table that has 2 columns: Variable_name and Value . 您将获得一个包含2列的表: Variable_nameValue 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. 将此数字写下来或将其保存在某处。 This is your host name. 这是您的主机名。 (If you're getting the quick rundown on this walkthrough, just start a new terminal and start up your mysql and select the database you want to use, then type in SHOW VARIABLES WHERE Variable_name = 'hostname'; . Re-read this step from the beginning if you're confused.) (如果您在本演练中获得快速简介,只需启动一个新终端并启动您的mysql并选择您要使用的数据库,然后键入SHOW VARIABLES WHERE Variable_name = 'hostname'; ;.重新阅读此步骤从一开始如果你感到困惑。)
  2. Username - Your username that you use to log in to Cloud 9. 用户名 - 您用于登录Cloud 9的用户名。
  3. Password - There is NO password for your database in Cloud 9. 密码 - Cloud 9中的数据库没有密码。
  4. Database name - This would be sample_db or whatever you named your database; 数据库名称 - 这将是sample_db或您为数据库命名的任何内容;
  5. Port # - is 3306 . ##端口3306 In Cloud 9, all of your projects are wired to 3306 . 在Cloud 9中,您的所有项目都连接到3306 This is a universal constant of Cloud 9. It will not be anything else. 这是Cloud 9的通用常量。它不会是其他任何东西。 Write this as you would an integer, not as a string. 把它写成整数, 而不是字符串。 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这个例子,这就是我的数据:

  • Host name: "graham12-sample_db-1234567" 主机名:“graham12-sample_db-1234567”
  • Username: "graham12" 用户名:“graham12”
  • Password: "" 密码:“”
  • Database name: "sample_db" 数据库名称:“sample_db”
  • Port #: 3306 港口#:3306

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. 请改用PDOmysqli 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.

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