forked from mfine30/docs-mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuse.html.md.erb
424 lines (296 loc) · 14.9 KB
/
use.html.md.erb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
---
title: Using MySQL for PCF v2
owner: MySQL
---
This topic provides instructions for developers using the MySQL for Pivotal Cloud Foundry (PCF) v2 service for their
PCF apps. MySQL provides a relational database for apps and devices.
These procedures use the Cloud Foundry Command Line Interface (cf CLI). For more information,
see [Managing Service Instances with the cf CLI](http://docs.pivotal.io/devguide/services/managing-services.html).
You can also use [Apps Manager](http://docs.pivotal.io/pivotalcf/console/dev-console.html) to do the same tasks using a graphical UI.
## <a id="prereqs"></a>Prerequisites
To use MySQL for PCF v2 with your PCF apps, you need:
* A PCF installation with [MySQL for PCF](https://network.pivotal.io/products/p-mysql) installed and listed
in the [Marketplace](http://docs.pivotal.io/devguide/services/#instances)
* A [Space Developer](http://docs.pivotal.io/pivotalcf/concepts/roles.html#roles) or Admin account on the PCF installation
* A local machine with the following installed:
- a browser
- a shell
- the [Cloud Foundry Command-Line Interface](http://docs.pivotal.io/pivotalcf/cf-cli/install-go-cli.html) (cf CLI)
- the Linux [watch](http://www.linfo.org/watch.html) command
* To [log in to](http://docs.pivotal.io/pivotalcf/cf-cli/getting-started.html#login) the org and space containing your app
## <a id='process'></a>The Process for Using MySQL for PCF in Your App
To use MySQL in a PCF app:
1. Check the service availability in the Marketplace,
and see if there is an existing instance of MySQL for PCF in your space.
See [Confirm the MySQL for PCF v2 Service Availability](#marketplace), below.
2. If there is no existing instance or you want to use a different one,
create an instance of the MySQL for PCF service in the same space as the app.
See [Create a Service Instance](#create), below.
3. Push your app into the same space as the MySQL for PCF service instance,
using `cf push`.
For information about `cf push`,
see [Push](http://docs.pivotal.io/pivotalcf/2-0/cf-cli/getting-started.html#push).
4. Bind the app to the MySQL for PCF service instance, to enable the app to use MySQL.
See [Bind a Service Instance to Your App](#bind), below.
5. Call the MySQL for PCF service in your app code, and then re-push your app into the space.
See [Use the MySQL Service in Your App](#call), below.
## <a id='marketplace'></a>Confirm the MySQL for PCF v2 Service Availability
For an app to use the MySQL for PCF v2 service, both of the following must be true:
* The service must be available in the Marketplace for its space.
* An instance of the service must exist in its space.
You can confirm both of these using the cf CLI as follows.
### <a id="check-marketplace"></a> Check Service Availability in the Marketplace
To find out if a MySQL for PCF v2 service is available in the Marketplace, do the following:
1. Enter the following command:
```
cf marketplace
```
1. If the output lists `p.mysql` in the `service` column, MySQL for PCF v2 is available.
If it is not available, ask your operator to install it.
<pre class="terminal">
$ cf marketplace
Getting services from marketplace in org my-org / space my-space as user<span>@</span>example.com...
OK
service plans description
[...]
p.mysql db-small Dedicated instances of MySQL service to provide a relational database
[...]
</pre>
### <a id="check-instance"></a> Check That an Instance Is Running in the Space
To confirm that a MySQL for PCF v2 instance is running in the space, do the following:
1. Use the [cf CLI](http://docs.pivotal.io/pivotalcf/cf-cli/getting-started.html#login) or
[Apps Manager](http://docs.pivotal.io/pivotalcf/customizing/console-login.html) to log in to the org and space that contains the app.
1. Enter the following command:
```
cf services
```
1. Any `p.mysql` listings in the `service` column are service instances of MySQL for PCF v2 in the space.
<br><br>
For example:
<pre class="terminal">
$ cf services
Getting services in org my-org / space my-space as user<span>@</span>example.com...
OK
name service plan bound apps last operation
my-instance p.mysql db-small create succeeded
</pre>
You can [bind](#bind) your app to an existing instance or [create](#create) a new instance to bind to your app.
## <a id='create'></a>Create a Service Instance
Unlike pre-provisioned services, on-demand services are created asynchronously, not immediately.
The `watch` command shows you when your service instance is ready to bind and use.
To create an instance of the MySQL for PCF v2 service, do the following:
1. Run the following command:
```
cf create-service p.mysql PLAN SERVICE-INSTANCE`<br><br>
```
Where:<br>
- `PLAN` is the name of the MySQL for PCF v2 plan you want to use.
- `SERVICE-INSTANCE` is a name you choose to identify the service instance.
This name appears under `service` in output from `cf services`.
1. Enter the following command and wait for the `last operation` for
your instance to show as `create succeeded`.
```
watch cf services
```
For example:
<pre class="terminal">
$ cf create-service p.mysql db-small my-instance<br>
Creating service my-instance in org my-org / space my-space as user<span>@</span>example.com...
OK<br>
$ watch cf services<br>
Getting services in org my-org / space my-space as user<span>@</span>example.com...
OK
name service plan bound apps last operation
my-instance p.mysql db-small create succeeded
</pre>
If you get an error, see [Troubleshooting Instances](./troubleshoot-instances.html).
## <a id="bind"></a>Bind a Service Instance to Your App
For an app to use a service, you must bind the app to a service instance. Do this after you push or re-push the app using `cf push`.
To bind an app to a MySQL for PCF instance run the following command:
```
cf bind-service APP SERVICE-INSTANCE
```
Where:<br>
- `APP` is the app you want to use the MySQL service instance.<br>
- `SERVICE-INSTANCE` is the name you supplied when you ran `cf create-service`.
For example:
<pre class="terminal">
$ cf bind-service my-app my-instance<br>
Binding service mydb to my-app in org my-org / space test as user<span>@</span>example.com...
OK
TIP: Use 'cf push' to ensure your env variable changes take effect
</pre>
## <a id="call"></a>Use the MySQL Service in Your App
To access the MySQL service from your app:
1. Verify that your app code (or the MySQL client library that the app uses) retries in the case of DNS timeouts.
1. Run the following command:
```
cf env APP-NAME
```
Where `APP-NAME` is the name of the app bound to the MySQL for PCF instance.
1. In the output, note the connection strings listed in the `VCAP_SERVICES` > `credentials` object for the app.
1. In your app code, call the MySQL service using the connection strings.<br><br>
See this example [Node.js code](./modify-apps-tls.html#call-service).
## <a id="manage"></a> Manage Service Instances
This section describes tasks you do over the life cycle of your apps and data:
+ Moving your data to a different plan.
+ Removing an app's access to a service it no longer needs.
+ Deleting a service instance that is not used.
### <a id="update"></a>Update a Service Instance to a Larger Plan
As apps and their databases grow, it may be necessary to update the service instance to a larger plan.
This does not require a rebinding of your app. However, while the instance is being migrated to a new service instance,
the database will be unavailable for several minutes.
To update a service instance to a larger plan, run the following command:
```
cf update-service SERVICE-INSTANCE -p PLAN
```
Where `PLAN` is the plan you want to upgrade the service instance to.
For example:
<pre class="terminal">
$ cf update-service my-instance -p db-large
</pre>
### <a id="unbind"></a>Unbind an App from a Service Instance
To stop an app from using a service it no longer needs, run the following command to unbind the app from the service:
```
cf unbind-service APP SERVICE-INSTANCE
```
Where:<br>
- `APP` is the app you want to stop using the MySQL service instance. <br>
- `SERVICE-INSTANCE` is the name you supplied when you ran `cf create-service`.
For example:
<pre class="terminal">
$ cf unbind-service my-app my-instance<br>
Unbinding app my-app from service my-instance in org my-org / space my-space as user<span>@</span>example.com...
OK
</pre>
### <a id="delete"></a>Delete a Service Instance
You cannot delete a service instance that an app is bound to.
To delete a service instance, do the following:
1. Run the following command:
```
cf delete-service SERVICE-INSTANCE
```
Where `SERVICE-INSTANCE` is the name of the service to delete.
<br><br>
For example:
<pre class="terminal">
$ cf delete-service my-instance<br>
Are you sure you want to delete the service my-instance ? y
Deleting service my-service in org my-org / space my-space as user<span>@</span>example.com...
OK
</pre>
1. Enter the following command and wait for a `Service instance not found` error indicating
that the instance no longer exists:
```
watch cf service SERVICE-INSTANCE
```
##<a id='establish-tls'></a> Establish a TLS Connection to a Service Instance
You can use `mysql` to establish a TLS connection to a MySQL for PCF service instance that has TLS enabled. For more information about how to enable TLS for a service instance, see [Using TLS](using-tls.html).
Perform the following steps:
1. Create a new service key for the service instance with TLS enabled. For example:
<pre class="terminal">$ cf create-service-key my-service-instance my-tls-service-key
{
"hostname": "10.1.16.5",
"jdbcUrl": "jdbc:mysql<span>://</span>10.244.16.3:3306/service\_instance\_db?user=6bf07ae455a14064a9073cec8696366c\u0026password=a22aaa2a2a2aaaaa\u0026=true",
"name": "service\_instance\_db",
"password": "a22aaa2a2a2aaaaa",
"port": 3306,
"tls": {
"cert": {
"ca": "-----BEGIN CERTIFICATE-----\...n-----END CERTIFICATE-----\n"
}
},
"uri": "mysql<span>://</span>6bf07ae455a14064a9073cec8696366c:a22aaa2a2a2aaaaa<span>@</span>10.244.16.3:3306/service\_instance\_db?reconnect=true",
"username": "6bf07ae455a14064a9073cec8696366c"
}
</pre>
If the service key does not have a CA certificate under `tls.cert.ca`, the service key may be stale. Create a new one.
1. Copy the contents of the CA certificate under `tls.cert.ca` and paste it into a file. For example:
<pre class="terminal">$ pbpaste > root.pem</pre>
1. Record the values for `username`, `password`, and `hostname`.
1. Use `mysql` to establish a TLS connection to the MySQL instance.
Run the following command:
<pre><code>mysql --host=HOSTNAME \
--user=USERNAME \
--password=PASSWORD \
--ssl-ca=root.pem \
--ssl-verify-server-cert</code></pre>
<br>
Where:
* `HOSTNAME` is the value for `hostname` retrieved above.
* `USERNAME` is the value for `username` retrieved above.
* `PASSWORD` is the value for `password` retrieved above.
<br><br>
For example:
<pre class="terminal">
$ mysql --hostname=10.1.16.5 \
--user=6bf07ae455a14064a9073cec8696366c \
--password=a22aaa2a2a2aaaaa \
--ssl-ca=root.pem \
--ssl-verify-server-cert
</pre>
## <a id="dev-tools"></a>MySQL for PCF Tools
The following tools let developers access their MySQL for PCF databases.
### <a id="mysqlweb"></a>Pivotal MySQLWeb Database Management App
The Pivotal MySQLWeb app provides a web-based UI for managing MySQL for PCF databases.
The free app lets you view and operate on tables, indexes, constraints, and other database structures, and directly execute SQL commands.
![Pivotal MySQLWeb](mysqlweb.png)
You can run the Pivotal MySQLWeb app in two ways:
* Standalone on your own machine
* Deployed to PCF
If you deploy Pivotal MySQLWeb to PCF, you can configure it in the deployment manifest to automatically bind to a specific service instance.
See the Pivotal MySQLWeb [code repo](https://github.com/pivotal-cf/PivotalMySQLWeb) and
[demo video](https://drive.google.com/file/d/0B2wmRcJj8XF-SXloNFNNaGJXbFk/view),
for how to install and run Pivotal MySQLWeb.
### <a id="plugin"></a>cf CLI MySQL Plugin
To connect to your MySQL for PCF databases from a command line, use the cf CLI MySQL plugin.
The plugin lets you:
* Inspect databases for debugging
* Manually adjust database schema or contents in development environments
* Dump and restore databases
To install the cf CLI MySQL plugin, run the following:
<pre class="terminal">
$ cf install-plugin -r "CF-Community" mysql-plugin
</pre>
For more information, see the [cf-mysql-plugin](https://github.com/andreasf/cf-mysql-plugin) repository.
### <a id="desktop"></a> Use Desktop Tools
You can connect your MySQL for PCF databases to a desktop tool, such as MySQL Workbench
or Sequel Pro, if you have the credentials for your MySQL service instance.
If you do not have credentials for your MySQL service instance, follow the procedure in
[Create Read-only Access Credentials](././customize-access.html).
To connect your databases to a desktop tool do the following:
1. To retrieve the credentials from your MySQL service instance service key, run the following
command:
```
cf service-key SERVICE-INSTANCE MYSQL-SERVICE-KEY
```
Where:
* `SERVICE-INSTANCE` is the name of your service instance.
* `KEY-NAME` is the name of your service key.
For example:
<pre class="terminal">
$ cf service-key mydb mykey1<br>
{
"hostname": "99.99.99.9",
"jdbcUrl": "jdbc:mysql://99.99.99.9:3306/cf\_e2d148a8\_1baa\_4961\_b314\_2431f57037e5?user=abcdefghijklm\u0026password=123456789",
"name": "cf\_e2d148a8\_1baa\_4961\_b314\_2431f57037e5",
"password": "123456789",
"port": 3306,
"uri": "mysql://abcdefghijklm:123456789<span>@</span>99.99.99.9:3306/cf\_e2d148a8\_1baa\_4961\_b314\_2431f57037e5?reconnect=true",
"username": "abcdefghijklm"
}
</pre>
1. Record the values for the following:
* `hostname`
* `name`
* `password`
* `port`
* `username`
1. Configure an SSH tunnel using the values for `hostname` and `port` that you recorded in the above step.
For information on configuring an SSH tunnel,
see [Configure Your SSH Tunnel](https://docs.pivotal.io/pivotalcf/2-3/devguide/deploy-apps/ssh-services.html#ssh-tunnel).
1. Configure a connection in your desktop tool using the values for `hostname`, `name`, `password`, `port`, and `username` that you recorded in the above step.
## <a id="example-app"></a>Example App
To help app developers get started with MySQL for PCF, Pivotal provides an example app, which can be downloaded [here][example-app].
Instructions are in the README.
[example-app]:mysql-example-app.tgz