Use a local database as a source or destination
If you want to use a database running in your local environment as a data source or destination for synthetic data, you can use ngrok
to expose the local database for external connections over the Internet and then add the ngrok
forwarding address and port in a new MOSTLY AI connector.
Task 1: Install ngrok
ngrok
(opens in a new tab) makes it possible to expose a locally running server to the Internet. To do so, you need an ngrok
account and an ngrok
authentication token.
Steps
-
Go to https://dashboard.ngrok.com/signup (opens in a new tab) and sign up for an
ngrok
account. -
Install
ngrok
with the steps for your operating system from https://dashboard.ngrok.com/get-started/setup (opens in a new tab). -
Copy your authentication token from https://dashboard.ngrok.com/get-started/your-authtoken (opens in a new tab).
-
Add the authentication token to your local environment.
ngrok config add-authtoken TOKEN
Result
ngrok
can now use the token to connect to your account.
Task 2: Run a local database
With Docker Compose (opens in a new tab), you can quickly run a database locally as a Docker container just by defining a new service in a YAML file.
The steps below show how to run a PostgreSQL database.
Steps
- Create a new YAML file with the name
mostly-ai-postgresql-compose.yaml
and paste the contents below.mostly-ai-postgresql-compose.yamlversion: '3.7' services: mostly_ai_sd_db: image: postgres environment: POSTGRES_USER: postgres POSTGRES_PASSWORD: changeme POSTGRES_DB: my_synthetic_db ports: - "5432:5432" volumes: - mostly_ai_sd_db:/var/lib/postgresql/data volumes: mostly_ai_sd_db:
- Set the credentials and database name in the lines highlighted below.
mostly-ai-postgresql-compose.yaml
... POSTGRES_USER: postgres POSTGRES_PASSWORD: changeme POSTGRES_DB: my_synthetic_db ...
- Run
docker compose
to start the service defined in the YAML file.docker compose -f ./mostly-ai-postgresql-compose.yaml logs
Result
The PostgreSQL database is now running locally.
Task 3: Expose your database with ngrok
To expose your database from your local environment, you need to know the port at which the database service is listening. Check your database configuration for that. For example, the default port for PostgreSQL is 5432
, for MySQL - 3306
.
Steps
- Run
ngrok
to expose your database port.Step result:ngrok tcp PORT
ngrok
randomly assigns a hostname and a forwarding port for your database. - Copy the forwarding address that is listed in the
ngrok
command status.
Task 4: Create a connector
You can use your local database as a data source, data destination, or both. In either case, you need a separate connector (a source or a destination) in MOSTLY AI.
Steps
- From the Connectors page, click + New connector.
- Select the connector type for your local database.
- Provide the connector details.
- For Access type, select Source or Destination.
- For Host, paste the
ngrok
forwarding address. - For Port, paste the
ngrok
forwarding port number.💡You can find the forwarding address and port in the output of the
ngrok
command from Task 3. - For Username, Password, and Database, set the database credentials and the database name.
💡
If you plan to use the same database as a source and destination, create a separate database for the destination connector to prevent potential loss of original data.
- Click Save.
What's next
To use the local database as a source, see Use as a data source.
To use the local database as a destination, see Use as a data destination.