Provision a Database and Import a SQL File for initialization
This tutorial will talk about how to provision a relational database with SQL file imported, and bootstrap an application which depends on the database.
Favorite Links is an interesting project which can store all your favorite web links in one application. It is using Node.js and MySQL. It has been built into a container image oamdev/nodejs-mysql-links:v0.0.1. Let’s bootstrap the application and see how to provision a database, and import a SQL file (which means to create tables, and insert data into them if needed).
Currently, it only works on Alibaba Cloud ComponentDefinition alibaba-rds-preview is the feature preview for Alibaba Cloud RDS, and will be merged into alibaba-rds later.
Feel free to skip this section if you are not interested.
Alibaba Cloud RDS Preview supports importing SQL file when create an RDS instance with these properties:
| Name | Description | Type | Required | Default |
|---|---|---|---|---|
| sql_file | The name of SQL file in the bucket, like db.sql | string | false | |
| sql_bucket_name | The bucket name of the SQL file. like oss://example | string | false | |
| sql_bucket_endpoint | The endpoint of the bucket. like oss-cn-hangzhou.aliyuncs.com | string | false |
After an RDS database is created, the SQL file from OSS bucket will be imported into the database by the power of Terraform local-exec provisioner which is referenced by Alibaba Cloud RDS Preview ComponentDefinition.
Enable addon terraform-alibaba
Remember to store the SQL file in an Alibaba Cloud OSS bucket
Let’s say we have an OSS bucket oss://favorite-links which contains a SQL file db.sql in it, and the bucket endpoint is oss-cn-hongkong.aliyuncs.com.
Use the following Application to provision a database links, import the SQL file db.sql, and bootstrap the application Favorite Links.
apiVersion: core.oam.dev/v1beta1kind: Applicationmetadata:name: favorite-linksspec:components:- name: webtype: webserviceproperties:image: oamdev/nodejs-mysql-links:v0.0.1port: 4000traits:- type: service-bindingproperties:envMappings:DATABASE_HOST:secret: db-connkey: DB_PUBLIC_HOSTDATABASE_NAME:secret: db-connkey: DATABASE_NAMEDATABASE_USER:secret: db-connkey: DB_USERDATABASE_PASSWORD:secret: db-connkey: DB_PASSWORD- name: dbtype: alibaba-rdsproperties:instance_name: favorite-linksdatabase_name: linksaccount_name: oamtestpassword: U34rfwefwefffakedsecurity_ips: [ "0.0.0.0/0" ]privilege: ReadWritesql_file: db.sqlsql_bucket_endpoint: oss-cn-hongkong.aliyuncs.comsql_bucket_name: oss://favorite-linkswriteConnectionSecretToRef:name: db-conn
After the application is successfully deployed, you can access the web application by the following URL:
$ vela lsAPP COMPONENT TYPE TRAITS PHASE HEALTHY STATUS CREATED-TIMEfavorite-links web webservice service-binding running healthy Ready:1/1 2022-02-21 14:15:45 +0800 CST└─ db alibaba-rds-preview running healthy Cloud resources are deployed and ready to use 2022-02-21 14:15:45 +0800 CST
$ vela port-forward favorite-links 4000:4000Forwarding from 127.0.0.1:4000 -> 4000Forwarding from [::1]:4000 -> 4000Forward successfully! Opening browser ...Handling connection for 4000Handling connection for 4000

Last updated on Feb 9, 2023 by dependabot[bot]