Skip to content
This repository was archived by the owner on Apr 6, 2021. It is now read-only.

Commit 9081ee0

Browse files
committed
updated mysql page
1 parent f051f18 commit 9081ee0

File tree

1 file changed

+85
-5
lines changed

1 file changed

+85
-5
lines changed

src/persistence/setting_up_mysql.md

Lines changed: 85 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ $ mysql-ctl stop
2626
$ mysql-ctl cli
2727
```
2828

29-
You can then connect to the database with following parameters:
29+
After having started (and with that created) the database, you can connect to it using the following parameters:
3030
<div markdown="1">
3131
<table class="table table-striped table-bordered">
3232
<thead>
@@ -40,7 +40,7 @@ You can then connect to the database with following parameters:
4040
<tr>
4141
<td>`Hostname`</td>
4242
<td>`$IP`</td>
43-
<td>The same local IP as the application you run on Cloud9</td>
43+
<td>The environment variable 'IP' (type in a terminal: echo $IP)</td>
4444
</tr>
4545
<tr>
4646
<td>`Port`</td>
@@ -50,12 +50,12 @@ You can then connect to the database with following parameters:
5050
<tr>
5151
<td>`User`</td>
5252
<td>`$C9_USER`</td>
53-
<td>Your Cloud9 user name</td>
53+
<td>Your Cloud9 user name (again an environment variable)</td>
5454
</tr>
5555
<tr>
5656
<td>`Password`</td>
57-
<td>-</td>
58-
<td>No password since you can only access the DB from within the workspace</td>
57+
<td>`<empty>`</td>
58+
<td>No password (empty string); access is restricted to the the workspace</td>
5959
</tr>
6060
<tr>
6161
<td>`Database`</td>
@@ -80,6 +80,86 @@ You are now in the MySQL environment and can start the import:
8080
To verify that everything got imported run:
8181

8282
mysql> show tables;
83+
84+
Some useful CLI commands (please note the semicolon at the end of most of them):
85+
<div markdown="1">
86+
<table class="table table-striped table-bordered">
87+
<thead>
88+
<tr>
89+
<th>Command</td>
90+
<th>Description</td>
91+
</tr>
92+
</thead>
93+
<tbody>
94+
<tr>
95+
<td>`help`</td>
96+
<td>`list all mysql commands`</td>
97+
</tr>
98+
<tr>
99+
<td>`show databases;`</td>
100+
<td>`list the available databases`</td>
101+
</tr>
102+
<tr>
103+
<td>`use c9`</td>
104+
<td>`select/use database c9`</td>
105+
</tr>
106+
<tr>
107+
<td>`show tables;`</td>
108+
<td>`list the tables of the current database`</td>
109+
</tr>
110+
<tr>
111+
<td>`exit`</td>
112+
<td>`close the mysql command line tool`</td>
113+
</tr>
114+
<tr>
115+
<td>`select * from mysql.user;`</td>
116+
<td>`show all records/columns from system table user`</td>
117+
</tr>
118+
</tbody>
119+
</table>
120+
</div>
121+
122+
## Connecting from PHP
123+
124+
So now you know how to create a database, start the db server, access it via a
125+
command line tool.. It is time for the real deal: accessing it from your code.
126+
127+
In this example we will connect from PHP:
128+
129+
1. Create a new file, call it `connect.php`
130+
131+
2. Copy/paste the following code in there:
132+
```bash
133+
<?php
134+
// A simple PHP script demonstrating how to connect to MySQL.
135+
// Press the 'Run' button on the top to start the web server,
136+
// then click the URL that is emitted to the Output tab of the console.
137+
138+
$servername = getenv('IP');
139+
$username = getenv('C9_USER');
140+
$password = "";
141+
$database = "c9";
142+
$dbport = 3306;
143+
144+
// Create connection
145+
$db = new mysqli($servername, $username, $password, $database, $dbport);
146+
147+
// Check connection
148+
if ($db->connect_error) {
149+
die("Connection failed: " . $db->connect_error);
150+
}
151+
echo "Connected successfully (".$db->host_info.")";
152+
?>
153+
```
154+
155+
3. Run the code by a right-click on the file tab 'connect.php', select 'run this file'
156+
157+
4. The output pane shows 'Starting Apache httpd...'
158+
159+
5. Click the link that is displayed after that
160+
161+
6. A preview pane will open, showing 'Connected successfully'.
162+
83163
84164
Note:
85165
MySQL socket file can be found in ~/lib/mysql/socket/mysql.sock

0 commit comments

Comments
 (0)