# 18. Connecting Your Local Database

This guide will help you connect your local database to AI2sql using ngrok. By following these steps, you can securely expose your local database to AI2sql, enabling seamless integration.

### Step 1: Install ngrok

#### 1.1 Download ngrok

* Go to the [ngrok download page](https://ngrok.com/download).
* Download the appropriate version for your operating system.

#### 1.2 Install ngrok

* Extract the downloaded file to a directory accessible from the command line.

#### 1.3 Create an ngrok Account

* Sign up for a free ngrok account at [ngrok signup page](https://dashboard.ngrok.com/signup).

#### 1.4 Authenticate ngrok

* Copy your auth token from the ngrok dashboard.
* Open your terminal and run the following command, replacing `<your_auth_token>` with the token you copied:

  ```sh
  ngrok authtoken <your_auth_token>
  ```

### Step 2: Start ngrok Tunnel

#### 2.1 Start the Tunnel

* For a local MySQL database running on port 3306, run the following command:

  ```sh
  ngrok tcp 3306
  ```

#### 2.2 Copy the Forwarding URL

* After starting the tunnel, ngrok will provide a forwarding URL in the format `tcp://0.tcp.ngrok.io:12345`.
* Copy this URL; you will need it to connect AI2sql to your local database.

### Step 3: Connect ngrok URL to AI2sql

#### 3.1 Open AI2sql

* Log in to your AI2sql account.

#### 3.2 Navigate to Database Connectors

* Go to the database connectors section within AI2sql.

#### 3.3 Add a New Database Connection

* Click on "Add New Connection" or a similar option.

#### 3.4 Enter Connection Details

* **Database URL:** Paste the ngrok forwarding URL you copied earlier (e.g., `tcp://0.tcp.ngrok.io:12345`).
* **Username:** Enter your local database username.
* **Password:** Enter your local database password.
* **Database Name:** Enter the name of your local database.

<div align="left"><figure><img src="https://3005060795-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fg9oPPpXcfNXETmlVmVUd%2Fuploads%2FdVKDtPecSHet9QtmwdTq%2FXnip2024-06-12_01-27-00.jpg?alt=media&#x26;token=0d55f8d4-ba1f-40a3-9c73-33ad0dbb4f3f" alt="" width="375"><figcaption></figcaption></figure></div>

<div align="left"><figure><img src="https://3005060795-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fg9oPPpXcfNXETmlVmVUd%2Fuploads%2FvvoAsKyobL8FXEyNKTjQ%2FXnip2024-06-12_01-13-25.jpg?alt=media&#x26;token=75c1e32b-46f7-4b11-9084-4ca572ce0737" alt="" width="375"><figcaption></figcaption></figure></div>

#### 3.5 Save the Connection

* Save the new database connection settings.

#### 3.6 Test the Connection

* Use the test connection feature (if available) to ensure everything is set up correctly.

### Step 4: Using the Database in AI2sql

* You can now use your local database in AI2sql as if it were a cloud-based database.
* Enjoy the features and benefits of AI2sql with your local data securely connected.

### Troubleshooting

#### Common Issues

* **Tunnel Not Working:** Ensure ngrok is running and you have copied the correct forwarding URL.
* **Authentication Failed:** Double-check your database username and password.
* **Connection Timeout:** Verify that your local database is running and accessible.

#### Getting Help

* Visit the [ngrok documentation](https://ngrok.com/docs) for more detailed information.
* Check the AI2sql help center for additional support.
