项目作者: andreasf

项目描述 :
Cloud Foundry CLI MySQL Plugin
高级语言: Go
项目地址: git://github.com/andreasf/cf-mysql-plugin.git
创建时间: 2016-11-15T07:05:05Z
项目社区:https://github.com/andreasf/cf-mysql-plugin

开源协议:Apache License 2.0

下载


Cloud Foundry CLI MySQL Plugin

build-and-test
License

cf-mysql-plugin makes it easy to connect the mysql command line client to any MySQL-compatible database used by
Cloud Foundry apps. Use it to

  • inspect databases for debugging purposes
  • manually adjust schema or contents in development environments
  • dump and restore databases

Contents

Usage

  1. $ cf mysql -h
  2. NAME:
  3. mysql - Connect to a MySQL database service
  4. USAGE:
  5. Open a mysql client to a database:
  6. cf mysql <service-name> [mysql args...]
  7. $ cf mysqldump -h
  8. NAME:
  9. mysqldump - Dump a MySQL database
  10. USAGE:
  11. Dumping all tables in a database:
  12. cf mysqldump <service-name> [mysqldump args...]
  13. Dumping specific tables in a database:
  14. cf mysqldump <service-name> [tables...] [mysqldump args...]

Connecting to a database

Passing the name of a database service will open a MySQL client:

  1. $ cf mysql my-db
  2. Reading table information for completion of table and column names
  3. You can turn off this feature to get a quicker startup with -A
  4. Welcome to the MariaDB monitor. Commands end with ; or \g.
  5. Your MySQL connection id is 1377314
  6. Server version: 5.5.46-log MySQL Community Server (GPL)
  7. Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
  8. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  9. MySQL [ad_67fd2577d50deb5]>

Piping queries or dumps into mysql

The mysql child process inherits standard input, output and error. Piping content in and out of cf mysql works
just like it does with plain mysql:

  1. $ cat database-dump.sql | cf mysql my-db

Passing arguments to mysql

Any parameters after the database name are added to the mysql invocation:

  1. $ echo "select 1 as foo, 2 as bar;" | cf mysql my-db --xml
  2. <?xml version="1.0"?>
  3. <resultset statement="select 1 as foo, 2 as bar" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  4. <row>
  5. <field name="foo">1</field>
  6. <field name="bar">2</field>
  7. </row>
  8. </resultset>

Dumping a database

Running cf mysqldump with a database name will dump the whole database:

  1. $ cf mysqldump my-db --single-transaction > dump.sql

Dumping individual tables

Passing table names in addition to the database name will just dump those tables:

  1. $ cf mysqldump my-db table1 table2 --single-transaction > two-tables.sql

Removing service keys

The plugin creates a service key called ‘cf-mysql’ for each service instance a user connects to. The keys are reused
when available and never deleted. Keys need to be removed manually before their service instances can be removed:

  1. $ cf delete-service -f somedb
  2. Deleting service somedb in org afleig-org / space acceptance as afleig@pivotal.io...
  3. FAILED
  4. Cannot delete service instance. Service keys, bindings, and shares must first be deleted.

Deleting the service failed. The CLI hints at service keys and app bindings that might still exist.

  1. $ cf service-keys somedb
  2. Getting keys for service instance somedb as afleig@pivotal.io...
  3. name
  4. cf-mysql

A key called ‘cf-mysql’ is found for the service instance ‘somedb’, because we have used the plugin with ‘somedb’
earlier. After removing the key, the service instance can be deleted:

  1. $ cf delete-service-key -f somedb cf-mysql
  2. Deleting key cf-mysql for service instance somedb as afleig@pivotal.io...
  3. OK
  4. $ cf delete-service -f somedb
  5. Deleting service somedb in org afleig-org / space acceptance as afleig@pivotal.io...
  6. OK

This behavior might change in the future as it’s not optimal to leave a key around.

Installing and uninstalling

The easiest way is to install from the repository:

  1. $ cf install-plugin -r "CF-Community" mysql-plugin

You can also download a binary release or build yourself by running go build. Then, install the plugin with

  1. $ cf install-plugin /path/to/cf-mysql-plugin

The plugin can be uninstalled with:

  1. $ cf uninstall-plugin mysql

Building

  1. # install ginkgo test runner
  2. go install github.com/onsi/ginkgo/v2/ginkgo
  3. # run tests and build
  4. ginkgo -r
  5. go build

Details

Obtaining credentials

cf-mysql-plugin creates a service key called ‘cf-mysql’ to obtain credentials. It no longer retrieves credentials from
application environment variables, because with the introduction of CredHub,
service brokers can decide to return a CredHub reference instead.

The service key is currently not deleted after closing the connection. It can be deleted by running:

  1. cf delete-service-key service-instance-name cf-mysql

A started application instance is still required in the current space for setting up an SSH tunnel. If you don’t
have an app running, try the following to start an nginx app:

  1. TEMP_DIR=`mktemp -d`
  2. pushd $TEMP_DIR
  3. touch Staticfile
  4. cf push static-app -m 128M --no-route
  5. popd
  6. rm -r $TEMP_DIR