Error Code Troubleshooting Guide

When CaoLiao's data API connects to your MySQL 5.7 database, you may encounter the following errors. Select the corresponding troubleshooting method based on the error code.

I. Database Server Unavailable or Connection Timed Out​

Meaning: Cannot connect to the MySQL service; the server is unresponsive or blocked by a firewall.

Common causes (MySQL 5.7)

  • MySQL service is not running
  • Incorrect host address or port (default: 3306)
  • Firewall/security group on the server has not opened port 3306
  • MySQL bind-address only allows local access
  • High network latency or broken link

Troubleshooting steps

  1. Check whether the MySQL service is running

    systemctl status mysqld
    
  2. Confirm the port is open

    netstat -tunlp | grep 3306
    
  3. Check the firewall and security group (cloud server): verify that port 3306 is open to the public internet and that the CaoLiao server IP is on the allowlist.

  4. Check whether my.cnf restricts access

    bind-address = 127.0.0.1   # If present, change to 0.0.0.0 to open access
    

Solutions

  • Start the MySQL service
  • Open the corresponding port
  • Add the CaoLiao server IP to the allowlist
  • Modify bind-address and restart MySQL

II. Database User Has Insufficient Permissions​

Meaning: The database user lacks sufficient permissions for the target database or table.

Common causes

  • User has not been granted access to the database
  • Missing SELECT, INSERT, UPDATE, or DELETE permissions
  • Permissions are only granted for local access (localhost), not remote access

Troubleshooting steps

  • Run the following statement to check user permissions:

    SHOW GRANTS FOR 'user'@'%';
    

Solutions

  • Grant at least the following permissions based on your API usage scenario:

    GRANT SELECT, INSERT, UPDATE, DELETE ON db_name.* TO 'user'@'%';
    FLUSH PRIVILEGES;
    
  • If table creation is required, also grant:

    GRANT CREATE ON db_name.* TO 'user'@'%';
    

III. Incorrect Username or Password​

Meaning: MySQL refuses the login.

Common causes

  • Incorrect username or password
  • User is not authorized for remote login
  • Password contains special characters that were not properly escaped

Troubleshooting steps

  • Test locally:

    mysql -u user -p -h 127.0.0.1
    
  • Check whether the user is allowed to log in remotely:

    SELECT host, user FROM mysql.user WHERE user='user';
    

Solutions

  • Verify the password is correct

  • Allow remote access:

    UPDATE mysql.user SET host='%' WHERE user='user';
    FLUSH PRIVILEGES;
    
  • Avoid using unescaped special characters when setting passwords

IV. Unique Key Conflict Causing Insert or Update Failure​

Common causes

  • Duplicate primary key
  • Duplicate unique index field

Troubleshooting steps

  • Identify the conflicting unique key

  • Query for duplicate values

    SELECT * FROM table WHERE unique_col = 'value';
    

Solutions

  • Modify the data so that unique keys no longer conflict

V. Attempting to Access a Non-Existent Table​

Common causes

  • Table name is misspelled
  • Table has not been created
  • Wrong database (incorrect USE statement)

Troubleshooting

SHOW TABLES LIKE 'table_name';

Solutions

  • Fix the table name

  • Create the missing table

  • Execute operations in the correct database:

    USE db_name;
    

VI. Foreign Key Constraint Not Satisfied​

Meaning: The record being inserted/updated violates foreign key rules.

Common causes

  • The parent record referenced by the child table does not exist
  • Incorrect insertion order (parent table must be written first)

Troubleshooting

  • Confirm the foreign key constraint:

    SHOW CREATE TABLE child_table;
    

Solutions

  • Insert the parent table first, then the child table
  • Ensure the foreign key field value exists in the parent table

VII. Incompatible Data Type Inserted into a Field​

Meaning: The inserted value does not match the field type, for example:

  • A string is written to an INT field
  • An invalid date format is written to a DATE field
  • A UTF-8 encoding error character

Troubleshooting steps

  • Check the field type:

    DESCRIBE table;
    

Solutions

  • Adjust the field type or convert the data type

VIII. Database Server Closed or Connection Dropped​

Meaning: The connection was dropped or the server closed.

Common causes

  • Connection timed out and was disconnected
  • Service restarted
  • Packet too large (exceeded max_allowed_packet)

Troubleshooting

  • View the MySQL error log

    cat /var/log/mysqld.log
    

Solutions

  • Increase the timeout duration
  • Adjust max_allowed_packet
  • Optimize network quality

IX. Operation Failed Due to Connection Timeout​

Meaning: Request execution was interrupted.

Common causes

  • Network jitter
  • Query timeout
  • Large query caused the connection to drop

Solutions

  • Optimize the SQL
  • Increase net_read_timeout and net_write_timeout

X. Insufficient Database Connection or Query Resources​

Common causes (MySQL 5.7)

  • Too many connections: MySQL's default max_connections (usually 151) is not enough and gets exhausted under high concurrency.
  • Connections not released: The application does not properly close database connections, leaving a large number of "Sleep" connections that occupy slots.
  • Slow query backlog: A large number of complex queries get stuck and consume all available threads.

Solutions

  • Temporarily increase the maximum number of connections (takes effect immediately, reset on restart)
  • Make the change permanent (modify my.cnf and restart)
  • Clean up zombie connections: Manually kill IDs in SHOW FULL PROCESSLIST that have been in the Sleep state for a long time, or optimize application code to ensure connections are closed promptly.

XI. Data Violates Database Constraint — Unique Key / Foreign Key Conflict​

Solutions

  • Ensure primary keys / unique keys have no duplicates
  • Ensure foreign key values exist in the parent table
  • Modify business logic or table structure

XII. SQL Statement Has Syntax or Logic Errors​

Common causes

  • SQL spelling errors
  • Reserved words not wrapped in backticks
  • Insufficient permissions preventing the statement from executing

Solutions

  • Check SQL syntax against MySQL 5.7 documentation

  • If a field or table name is a reserved word (e.g., order), wrap it in backticks:

    `order`
    

XIII. Miscellaneous Database Operation Errors​

Covers all MySQL exceptions that cannot be categorized elsewhere.

Troubleshooting directions

  • Check the MySQL error log
  • Check the slow query log
  • Check network stability and connection pool settings

If you cannot resolve the issue on your own, contact CaoLiao customer support for technical assistance.