Closed Deepak-Vohra closed 5 months ago
Thank you for submitting an idea for our guest blog.
We work through new ideas every few weeks as we put together our content schedule. This means you may not get an immediate response as to whether your idea has been accepted, or any follow-up questions we have to clarify your idea.
If your idea is accepted, we will provide a deadline for first draft and how much we can pay you for the post. You will have a few days to confirm whether you are still able and willing to write the post.
If you have any questions in the meantime, feel free to add a comment to this issue.
Thank you for submitting this idea, but unfortunately we are not accepting it as part of our guest author program, we have similar guides on AWS and don't see a demand for this piece. Please feel free to submit additional ideas in future.
Hi Beth, If you'll reconsider, the emphasis is not on AWS, but is on MySQL. I developed article because I Googled "directus mysql" and did not find any resource/documentation/article. I only found a few issues: https://github.com/directus/directus/discussions/17005 https://github.com/directus/directus/discussions/17658 https://forums.docker.com/t/container-connect-to-mysql-failed/138154/2 https://stackoverflow.com/questions/67136373/directus-in-docker-cannot-connect-to-mysql-database-in-docker
Thanks for providing the additional context. Unfortunately, we aren't able to accept this idea but please do feel free to submit different ideas in the future.
Hi Beth, I have a new article proposal around the new PGQL (Property Graph Query Language), but because Directus Doesn't support it, the article doesn't involve Directus. Does an article have to involve Directus for you to consider?
Typically yes, but if it's directly relevant to our users, we can be a bit more flexible. However, PGQL doesn't make sense for us as a database abstraction layer :)
I want to get some initial feedback on a topic before submitting a detailed proposal such as the first one. Would the topic have any interest: How to Use Directus with Oracle Autonomous Database?
If you are not able to comment just based on topic, please let me know. I understand that initial topic feedback is not an acceptance of topic.
What is your idea?
How to Use Directus with MySQL Database on AWS RDS
Key Takeaways
Directus is a one-of-a-kind Content Management System (CMS), and Data Backend as a Service (BaaS) built on top of your data model, to provide seamless integration with your data. You have the choice to manage your data directly from the Directus web console to perform tasks such as creating a data collection, adding items to a collection, importing/exporting data from CSV and JSON, and interacting with your data using GraphQL and REST APIs. Directus manages the ancillary tasks of authentication, visualizing data on dashboards, and editing & caching data. It supports DevOps operations such as syncing your data over WebSockets, and automating complex workflows.
With a relational database, Directus can be used as an abstraction layer over the SQL data model. In this article I’ll discuss how to use Directus with a MySQL instance hosted on Amazon Relational Database Service. You’ll create a self-hosted Directus project using Docker Compose.
Prerequisite Setup
Figure 1. DNS Settings for AWS VPC
Creating an AWS RDS Instance for MySQL
To create a MySQL Database as a Service (DbaaS) Service, login to the web console and select the RDS service. Select Databases in the navigation margin and click on Create database as shown in Figure 2.
Figure 2. RDS>Databases>Create database
In the Create database window choose a database creation method. Select Easy create if not familiar with RDS as shown in Figure 3. In Configuration select MySQL.
Figure 3. Create database >Easy create>MySQL
Select Edition as MySQL Community>Free Tier as shown in Figure 4. Specify a DB instance identifier (mysqldb). Specify Master username as admin, and select the Self managed option for Credentials management.
Figure 4. Specifying MySQL Database Identifier and Credentials
The default settings for the Easy create option method can be viewed as shown in Figure 5. This may help you in determining whether the Easy create is the right option for you based on which of the settings are editable after an instance is created.
Figure 5. Easy create default settings
Specify a Master password and make a note of it along with the Master username because you’ll need these credentials when creating a Docker Compose-based platform for Directus. Click on Create database as shown in Figure 6.
Figure 6. Create database button
A new database instance gets created as indicated in the message shown in Figure 7.
Figure 7. New RDS Database instance create
Enabling Connectivity to MySQL on RDS
To be able to connect to the MySQL database instance you need to ensure that connectivity to the instance is enabled. The Security group used for the RDS instance must have Inbound/Outbound rules set for the instance to be accessible. The RDS instance must be Publicly accessible if the Linux machine you’ll use to access it is not hosted on AWS EC2. A RDS instance created with the Easy create method is not publicly accessible as indicated in Figure 8.
Figure 8. RDS Instance not Publicly accessible
Click on Modify for the RDS instance as shown in Figure 9.
Figure 9. Modify
In the Modify DB instance window click on Additional configuration in Connectivity and select the Publicly accessible radio button as shown in Figure 10.
Figure 10. Publicly Accessible
The Summary of modifications should list the Public accessibility as Yes as shown in Figure 11. You’ll need to specify the Master password when you modify a RDS instance. Select Schedule modifications as Apply immediately. Click on Modify DB instance.
Figure 11. Modify DB instance
For a short time, the DB instance Status column shows Modifying as in Figure 12.
Figure 12. Modifying Status
After the modification is complete the Publicly accessible setting should show Yes as in Figure 13.
Figure 13. Publicly Accessible>Yes
Obtaining Connection Parameters
To connect to the newly created DB instance you’ll need the following connection parameters:
Some of these you made a note of when creating the database instance such as Username and Password. Click on the Connectivity and security tab to obtain the MySQL Endpoint and Port (3306 by default) as shown in Figure 14. Copy the Endpoint as you’ll need it to connect to the database from Directus.
Figure 14. Database Endpoint (Hostname) and Port
Verifying Connection to MySQL
On your local Linux machine create a directory called
directus
to manage your self-hosted Diectus project. Change to the directory.Connect to the MySQL Database instance you created using the URL command:
The hostname is specified with the
-h
option as the Endpoint you copied in Figure 14. Port is specified with-P
as 3306. Username is specified with-u
as the Master username set when creating a DB instance. Password is specified as-p
as the Master password. Note that the MySQL port is specified with-P
and the password with-p
. When prompted, provide your password. This is the Master password you set when creating the RDS DB instance. The MySQL shell prompt gets displayed when a connection is made.Creating a Database
List all databases with
show databases
command:As these are all system databases used by MySQL internally, create a new database for your Directus project with
CREATE DATABASE
statement.The new database should get listed:
Logout and connect to the new database you just created:
Configuring Docker Compose
Docker Compose is a tool for defining and running multi-container applications with Docker. At the least, it needs a configuration file in one of three formats JSON, YAML, and JS with the corresponding configuration file extensions
.json
,.yaml
, or.js
. Next, configure Docker Compose to run Docker containers for Directus. Create a configuration fileconfig.json
in the/directus
directory.To use MySQL Database with Directus some configuration options must be set in the configuration file. These options along with their values are listed in the following table;
DB_CLIENT
must be “mysql”, and the other option values could vary for users based on what you configured:Table 1. Configuration Options Required for MySQL
The
config.json
is listed below, in which only the MySQL settings in the preceding table may be different for different users, and all the other settings can be used as listed. Optionally,KEY
andSECRET
can be customized.You need to create the directory used by Directus to store data on your local machine, and set its permissions so that Directus can access it. This is the directory set in the volumes under
./database
setting as/directus/database
.Creating and Starting a Directus Container
Next, you’ll create and start a Docker application for Directus using Docker Compose. The tool options and sub-commands may be listed with
docker compose
.Next, create and start a Docker container using Docker Compose tool along with the configuration file. Downloading the Docker image for Directus can take some time and the default timeout setting of 25 seconds for Docker Compose may not be sufficient. Therefore, set the environment variable
COMPOSE_HTTP_TIMEOUT
to a larger timeout, such as 200 seconds. Run the following command, and you may need to specify the administrative password for your Linux machine when prompted:As the partial output indicates, Directus gets launched and becomes accessible at URL http://0.0.0.0:8055.
The docker compose output is shown in Figure 15.
Figure 15. Server started at http://0.0.0.0:8055
Logging into Directus Console
Next, log into the Directus console using the URL http://0.0.0.0:8055 in a browser. In the login screen specify username and password. The username and password can be obtained from the Docker Compose configuration file
config.json
, listed earlier. Click on Sign In as shown in Figure 16.Figure 16. Sign In to Directus Console
Creating a Collection
Create your first collection. To start with you can create a minimal collection and add fields as needed later. Click on Create Collection as shown in Figure 17.
Figure 17. Create Collection
Specify a collection name (tbl1 for example) in the Creating New Collection window as shown in Figure 18. The Primary Key Field (id) is required, and its type is Auto-incremented integer. Click on Next.
Figure 18. Creating a New Collection
Select the Optional Fields to include as shown in Figure 19 in which all optional fields are selected. Click on Finish Setup.
Figure 19. Select Optional Fields and Finish Setup
A new collection will get created as shown in Figure 20. The Data Model tab lists the fields. Additional fields can be created with the Create Field button.
Figure 20. Data Model for the Collection called Tbl1
Verifying Table in MySQL
In the MySQL shell you can verify that the collection you added is created as a new database table. Set the current database to the “directus” database with the
use directus
command. This is the database you created earlier and subsequently configured in the Docker Compose configuration fileconfig.json
.List tables with the
SHOW TABLES
command. Thetbl1
table is listed along with the system tables.Describe the
tbl1
database with theDESC
command. The table definition corresponds to the fields you added when creating the collection.Initially, the table is empty:
Importing Data into Collection
Create a sample data file (
sample.json
) to import data into the new collection.Create the
sample.json
file in the Directus working directory/directus
as shown in Figure 21.Figure 21. Sample Data file
In the Directus web console No items are listed for the Tbl1 collection. Click on Import/Export as shown in Figure 22.
Figure 22. Import/Export
Select the sample.json file with the Import as shown in Figure 23.
Figure 23. Selecting the sample.json file
Click on Start Import as shown in Figure 24 to start the import.
Figure 24. Start Import
The Tbl1 collection has one item added as shown in Figure 25.
Figure 25. One item added to Tbl1 Collection
In the MySQL shell run a
SELECT
query to list the data added:Adding a Collection Item from MySQL Shell
The collection in Directus corresponds to a table in the relational database. You may add data directly in the MySQL shell and it’ll get added to the Directus project collection. As an example, add a new row of data with the SQL statement
INSERT INTO
:A
SELECT
statement should list as the new row added:In the Directus web console, the
Tbl1
console should list two collection items as shown in Figure 26.Figure 26. Two Collection items
Any updates made in the Directus console get applied to the MySQL database table on which the data model is based. As an example, update the value of the status field to Published as shown in Figure 27.
Figure 27. Updating Status of Collection Items to Published
In the MySQL shell a
SELECT
statement lists the status aspublished
:This bi-directional mapping and access to the data model is one of the features that Directus provides.
Exporting Collection Data
To export a collection, select the collection and click on Export Items as shown in Figure 28.
Figure 28. Export Items
In the Export Items window select the format as CSV or JSON, and specify the maximum number of records to export with Limit. Select an Export Location as Download File as shown in Figure 29.
Figure 29. Export Items
Click on Download File as shown in Figure 30.
Figure 30. Download File
An export file (Figure 31) gets downloaded.
Figure 31. Export File
Adding a Collection Item from Console
Three options are available to add a data item to a collection:
We already discussed the first two options. To demonstrate the third option, click on Create Item as shown in Figure 32.
Figure 32. Create Item
A new collection item starts to get created. Click on Save (Figure 33).
Figure 33. Saving a Collection Item
The collection Tbl1 lists 3 items (Figure 34).
Figure 34. Three Collection items
A
SELECT
statement in MySQL shell lists three items as well:Deleting a Collection
A collection can be deleted from the Directus console or the MySQL shell. The Directus project must be connected to the MySQL database to be able to delete a collection. A collection cannot be deleted if the database has been stopped or deleted. Select the
tbl1
data model and click on Delete Collection as shown in Figure 35.Figure 35. Delete Collection
Deleting the AWS RDS Instance
To delete an AWS RDS Database instance select Actions>Delete as shown in Figure 36.
Figure 36. Deleting a AWS RDS Database Instance
A message should indicate that the DB instance has been deleted.
Figure 37. RDS DB Instance Deleted
Conclusion
Diectus is a one-stop integrated platform that abstracts an underlying database and provides all the modern features for data management. In this article, you learned about creating a self-hosted Directus project with Docker Compose and the Docker image for Directus
directus/directus
. You learned about using MySQL database running on AWS Relational Database Service as the underlying database for the Directus data model. You learned about performing create, read, update, and delete (CRUD) operations using the MySQL-based data model.What are the key takeaways from your post?
Key Takeaways
Country of residence
Canada
Terms & Conditions