# Connect and query an instance

CloudSigma OmniFabric database instances use the MySQL protocol. After creating a database instance, configure its network policy and TLS settings, connect with a MySQL-compatible client or driver, and run `SELECT 1` to verify connectivity.

## Before you begin

1. Create a database instance and wait until its status becomes **Available**. See [Database Instance](https://omnifabric.cloudsigma.com/docs/guides/matrixone/instances.html.md).
2. Open the instance's connection action in the console and copy the **host**, **port**, **user name**, and sample connection command.
3. Confirm your administrator password (`admin`) or the `username:role_name` credential created for your account. See [OmniFabric permissions](https://omnifabric.cloudsigma.com/docs/guides/matrixone/permissions.html.md).

## Configure IP allowlist and TLS

Before connecting from an external client or application:

1. **IP allowlist (Network Policy)**:
   - Open the instance's **Network Policy** settings.
   - For production workloads, choose **Access from a specified IP address** and add the public egress IP address or CIDR block of your client host or application server.
   - Use **Access from anywhere** only for temporary testing, and restrict the allowlist once testing is complete.
2. **TLS encryption**:
   - Use the TLS parameters shown in the console's connection command (for example, `--ssl-mode=REQUIRED` for the MySQL CLI, `ssl` options in Python, or `sslMode=REQUIRED` in JDBC).
   - If the console provides a CA certificate bundle for your region, download it and pass it to your client's CA option to verify the server certificate.

## Connect with the MySQL client

Replace the placeholders with the host, port, and user name copied from the console:

```bash
mysql \
  -h <host> \
  -P <port> \
  -u admin \
  -p \
  --ssl-mode=REQUIRED
```

Enter the password when prompted. To connect as a regular database user with an explicit role, pass `-u <username>:<role_name>`.

### Verify with `SELECT 1`

After signing in, run a quick connectivity and catalog check:

```sql
SELECT 1;
SHOW DATABASES;
```

If `SELECT 1;` returns `1`, the network path, TLS handshake, and authentication succeeded.

## Connect with Python

Install `PyMySQL` (or another MySQL-compatible driver):

```bash
pip install pymysql
```

Store connection values in environment variables and run a `SELECT 1` check:

```python
import os
import pymysql

conn = pymysql.connect(
    host=os.environ["OMNIFABRIC_HOST"],
    port=int(os.environ.get("OMNIFABRIC_PORT", "6001")),
    user=os.environ["OMNIFABRIC_USER"],
    password=os.environ["OMNIFABRIC_PASSWORD"],
    ssl={"ssl": {}},
    connect_timeout=10,
)

try:
    with conn.cursor() as cursor:
        cursor.execute("SELECT 1")
        print(cursor.fetchone())
finally:
    conn.close()
```

Expected output:

```text
(1,)
```

If your deployment requires CA certificate verification, pass `ssl={"ca": "/path/to/ca.pem"}` instead of `ssl={"ssl": {}}`.

## Connect with Java (JDBC)

Add the MySQL Connector/J dependency to your project (Maven example):

```xml
<dependency>
  <groupId>com.mysql</groupId>
  <artifactId>mysql-connector-j</artifactId>
  <version>8.4.0</version>
</dependency>
```

Connect using JDBC with TLS enabled and execute `SELECT 1`:

```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class OmniFabricConnectExample {
    public static void main(String[] args) throws Exception {
        String host = System.getenv("OMNIFABRIC_HOST");
        String port = System.getenv().getOrDefault("OMNIFABRIC_PORT", "6001");
        String user = System.getenv("OMNIFABRIC_USER");
        String password = System.getenv("OMNIFABRIC_PASSWORD");

        String url = String.format(
            "jdbc:mysql://%s:%s/?sslMode=REQUIRED&useUnicode=true&characterEncoding=utf8",
            host, port
        );

        try (Connection conn = DriverManager.getConnection(url, user, password);
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery("SELECT 1")) {
            if (rs.next()) {
                System.out.println("Result: " + rs.getInt(1));
            }
        }
    }
}
```

Expected output:

```text
Result: 1
```

## Troubleshoot common connection errors

| Symptom or error | Likely cause | What to do |
| --- | --- | --- |
| Connection timed out (`2003` / `Can't connect to MySQL server`) | Client public IP is not in the instance IP allowlist, host/port is wrong, or outbound port is blocked by a local firewall | Confirm the instance status is **Available**, verify host and port from the console, add your client's current egress IP under **Network Policy**, and check outbound firewall rules |
| TLS / SSL handshake error (`SSL connection error` / `certificate verify failed`) | Client TLS mode does not match the instance requirement or CA file path is invalid | Match the TLS flags shown in the console connection command (`--ssl-mode=REQUIRED` / `sslMode=REQUIRED`), and verify the CA file path when certificate verification is enabled |
| Authentication failed (`1045` / `Access denied for user`) | Incorrect password, wrong user name, or missing role suffix for a regular user | Confirm the password for `admin`, or sign in as `<username>:<role_name>` for regular users; see [OmniFabric permissions](https://omnifabric.cloudsigma.com/docs/guides/matrixone/permissions.html.md) |
| Insufficient privileges on database or table (`1142` / `permission denied`) | Active role does not have the required `GRANT` privileges | Run `SHOW GRANTS;` to inspect active permissions and grant the needed privileges from an administrator session |
| Connection rejected or instance unavailable | Instance is still creating, restricted due to quota/Credit limits, or stopped | Check the instance state in [Database Instance](https://omnifabric.cloudsigma.com/docs/guides/matrixone/instances.html.md), review [Events](https://omnifabric.cloudsigma.com/docs/guides/matrixone/events.html.md), and verify Credit balance in [OmniFabric billing](https://omnifabric.cloudsigma.com/docs/guides/billing/matrixone-billing.html.md) |

## Related documents

- [Database Instance](https://omnifabric.cloudsigma.com/docs/guides/matrixone/instances.html.md)
- [OmniFabric permissions](https://omnifabric.cloudsigma.com/docs/guides/matrixone/permissions.html.md)
- [SQL Reference](https://omnifabric.cloudsigma.com/docs/reference/sql/index.html.md)
- [OmniFabric billing](https://omnifabric.cloudsigma.com/docs/guides/billing/matrixone-billing.html.md)
