1

I have tried a number of things to connect to my database in cloud 9 but I keep getting similar errors.

This is my PHP code:

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

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)

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

Huseyin Yagli
  • 9,896
  • 6
  • 41
  • 50
Andrew
  • 3,393
  • 4
  • 25
  • 43
  • Seems like you're missing something. Is the username and the database name correct? Are you using `localhost` or `127.0.0.1` as the IP? – chility May 04 '14 at 19:25
  • It said the host name on the cloud 9 doc is : *The same local IP as the application you run on Cloud9* `$IP` and that's what I'm using. I just tried your recommendations and it failed as well. – Andrew May 04 '14 at 19:28
  • did you start the database? did you check the value in $IP, i think you need to replace it – Ronni Skansing May 04 '14 at 19:37
  • 2
    Yes it seems a little confusing make sure you have started the service with `mysql-ctl start` also try `mysqli_connect($IP.':/lib/mysql/socket/mysql.sock', $C9_USER...)` also check https://c9.io/site/blog/2013/05/mysql-for-every-workspace/ also in places it says theres no mysql for free account.. Perhaps you should contact support and say you simply dont understand and others dont too, get them to make an example. It would take 30secs when you know how to connect. complain!!!! these hosted IDE's are 99% of the time broken or in beta, good luck.. – Lawrence Cherone May 04 '14 at 19:39
  • Thanks for the help. I think I will try to get ahold of them. But as of now, nothings working. I tried out your example and it seemed promising - but it said no mysql located here. Also, I made sure the mysql was on using `msql-ctl start` earlier but I don't think their db service is functioning properly. I might give up on c9, do you have any other cloud IDE recommendations? -- I'll invite you to a chat room. – Andrew May 04 '14 at 19:49
  • 1
    Check the comments on c9.io/site/blog/2013/05/mysql-for-every-workspace it seems there is solutions involving getting the **actual IP** of the server your on and the **actual username** the ones supplied are just environment variables. your need to get the **actual** ones if not running the code in the terminal, so **You open terminal(alt+t), and type: echo $IP then echo $C9_USER** – Lawrence Cherone May 04 '14 at 19:52
  • That is correct! You should answer the question with that information and I'll accept it for others to see. I thought it was odd that `var_dump($IP)` was returning `null` ... Thanks a lot Loz! – Andrew May 04 '14 at 19:55
  • np, ill leave it to someone else to formulate an answer ;p – Lawrence Cherone May 04 '14 at 20:19

1 Answers1

7

Credit to Loz Cherone

When using Cloud 9 IDE, the php variables $ID and $C9_USER mentioned in this article are not defined.

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

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

SIDE NOTE: Make sure when running the mysql code that you have the data base turned on. You can turn it on by typing mysql-ctl start in the terminal.

Community
  • 1
  • 1
Andrew
  • 3,393
  • 4
  • 25
  • 43