Using MySQL with ngrok
Test your API locally using ngrok
and a MySQL database.
Before you begin
Install ngrok
on your machine, using the appropriate tab for your operating
system.
About this task
The following steps are for using MySQL on Mac. If you are using Windows, Chocolatey follows a similar installation method for MySQL.
Procedure
-
Install MySQL.
If you already have a MySQL database running locally, skip to the next step.
- Install MySQL:
brew install mysql
- Verify that MySQL installed successfully:
mysql --version mysql Ver 8.0.25 for macos10.15 on x86_64 (Homebrew)
- Start the MySQL server:
brew services start mysql
- Access the MySQL server:
mysql -u root -p password
- Create a new database and user:
mysql> create database tutorialdb; Query OK, 1 row affected (0.00 sec) mysql> create user 'tutorialuser'@'localhost' identified by 'tutorialuserpassword'; Query OK, 0 rows affected (0.01 sec) mysql> grant all on tutorialdb.* to 'tutorialuser'@'localhost'; Query OK, 0 rows affected (0.01 sec)
- Exit the MySQL server and log in as the new user:
mysql> exit desktop % mysql -u tutorialuser -p tutorialdb Enter password: ************ // tutorialuserpassword
- Create a table in the new database:
mysql> CREATE TABLE weather ( city varchar(80), temp_lo int, -- low temperature temp_hi int, -- high temperature prcp real, -- precipitation date date );
- Install MySQL:
- Point
ngrok
to your local MySQL server.- Determine where your MySQL database is running locally.
There are many different ways to check where a program is running on a local machine; for example, by using
grep
ornetstat
. - Determine which port number MySQL is using:
mysql> SHOW VARIABLES WHERE Variable_name = 'port'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | port | 3306 | +---------------+-------+ 1 row in set (0.00 sec)
The following are alternative commands to check for
port
on a local machine:mysql> SHOW GLOBAL VARIABLES LIKE 'port';
$ netstat -tlnp
- Set the port for
ngrok
to MySQL's port number (for example, port3306
):ngrok tcp 3306 // change 3306 to your port number
This command generates the TCP endpoint that you will use in the
config.yaml
configuration file:Session Status online Account Test Account Version 2.3.40 Region United States (us) Web Interface http://127.0.0.1:1234 Forwarding tcp://2.tcp.ngrok.io:1234 -> localhost:3306 Connections ttl opn rt1 rt5 p50 p90 0 0 0.00 0.00 0.00 0.00
- Determine where your MySQL database is running locally.
- Connect MySQL to API Connect Essentials
From the information collected earlier with the
\port
andngrok tcp 3306
commands, add the MySQL TCP endpoint along with the username and password from the database, toconfig.yaml
for the schema.- Create the
config.yaml
configuration file with the following content:
where:configurationset: - configuration: name: mysql_config dsn: tutorialuser:tutorialuserpassword@tcp(2.tcp.ngrok.io:1234)/tutorialdb
name
is a name for this configuration. If you are adding this configuration to an existing GraphQL schema, change the name of the configuration to match the name in your schema.dsn
contains your database user name and password, the TCP endpoint that was created when you ran thengrok tcp 3306
command in the previous step, and the database name using the following format:dsn: <db_user>:<db_password>@tcp(<tcp_endpoint>)/<db_name>
In the sample configuration file:dsn: tutorialuser:tutorialuserpassword@tcp(2.tcp.ngrok.io:1234)/tutorialdb
<db_user>
istutorialuser
<db_password>
istutorialuserpassword
<tcp_endpoint>
is2.tcp.ngrok.io:1234
<db_name>
istutorialdb
- List the tables in the database by running the following command:
mysql> show tables; +----------------------+ | Tables_in_tutorialdb | +----------------------+ | weather | +----------------------+ 1 row in set (0.00 sec)
In this example, there is one table called "weather". You created the table earlier in this example:
mysql> CREATE TABLE weather ( city varchar(80), temp_lo int, -- low temperature temp_hi int, -- high temperature prcp real, -- precipitation date date );
- Create the following GraphQL schema to execute one mutation and one query to the
tutorialdb
database:- The
createWeather
mutation creates new rows in theweather
table. - The
weather
query retrieves all of the existing rows in theweather
table.
type Weather { city: String temp_lo: Int temp_hi: Int prcp: Float date: Date } type Query { weather: [Weather] @dbquery( type: "mysql" query: "select * from weather" configuration: "mysql_config" ) } type Mutation { createWeather(city: String!, temp_lo: Int!, temp_hi: Int!, prcp: Float!, date: Date!): Weather @dbquery( type: "mysql" table: "weather" dml: INSERT configuration: "mysql_config" ) }
- The
- Create the