How to retrieve data from sql or mongo database?

Hi all,
I have an aws ec2 instance created with openedx ami. May I know how to retrieve data from the sql or mongo database? For example, if I want to create a new plugin that will need to read and write data from or to the database. How should I do ? Also, how can I connect to them externally from client such as Mysql workbench ? Thanks.

Which AMI exactly are you using?

In any case, MySQL should be running within the EC2 VM either as a daemon or as a docker container. If running as a daemon, you should be able to connect to it with a local socket connection using mysql_client - if not, check the AMI instructions for the password. If it’s running as a docker container, run docker ps to get the container name then follow these steps to access it. There’s nothing Open edX specific here; these are just the “normal” ways to access a running MySQL instance.

Once you have that working, you can use ssh tunneling to connect to it remotely from e.g. MySQL workbench.

Plugins are created using the django app plugins framework, and then your plugin can use all the standard Django and Open edX python APIs to read from the database. If you don’t want to learn Django models and the Open edX APIs, the most basic approach is to run raw SQL queries which is fine if you’re only reading data but definitely not recommended if you’re writing data.

Hi Braden,
Thanks so much for your reply.
I use this aws ami to create the instance.
Now I can access the database from inside of the instance:
docker exec -uroot -it tutor_local_mysql_1 bash
mysql -u root -p
I guess this is docker container instead of daemon ?
I also tried to follow this guide to connect database from sqlworkbench but failed. I could not find any mysql57 section from the docker-compose.yml, which locates at /home/tutor/.local/share/tutor/env/local.
I don’t know about Django and Openedx APIs. Thanks for providing those keywords for my further research.

Yes. Tutor is docker-based, and the AMI you are using sets up Tutor.

That guide is for “devstack”, which is different from Tutor, and focused on running Open edX on your own computer, not on AWS.

Since you are able to access the instance via SSH, you can set up an SSH tunnel from your computer to the VM, and then use that SSH tunnel to connect to MySQL using sqlworkbench. However, it will require a bit of work to ensure that all the ports are forwarded at each step. You need to ensure that the mysql container is listening to connections from the VM and that its docker container has the right port exposed to the VM, and then you need to tunnel that port to your own computer. Start by trying to get access to MySQL from the VM without first having to run docker exec, and once you’ve gotten that working, you can use SSH tunnelling to get it working from your own computer and sqlworkbench.

Alternately, just run Tutor/Open edX on your own computer instead of on an AWS VM, and you can skip the SSH tunnelling and inspect what’s happening more easily.