OceanBase logo

OceanBase

A unified distributed database ready for your transactional, analytical, and AI workloads.

DEPLOY YOUR WAY

OceanBase Cloud

The best way to deploy and scale OceanBase

OceanBase Enterprise

Run and manage OceanBase on your infra

TRY OPEN SOURCE

OceanBase Community Edition

The free, open-source distributed database

OceanBase seekdb

Open source AI native search database

Customer Stories

Real-world success stories from enterprises across diverse industries.

View All
BY USE CASES

Mission-Critical Transactions

Global & Multicloud Application

Elastic Scaling for Peak Traffic

Real-time Analytics

Active Geo-redundancy

Database Consolidation

Resources

Comprehensive knowledge hub for OceanBase.

Blog

Live Demos

Training & Certification

Documentation

Official technical guides, tutorials, API references, and manuals for all OceanBase products.

View All
PRODUCTS

OceanBase Cloud

OceanBase Database

Tools

Connectors and Middleware

QUICK START

OceanBase Cloud

OceanBase Database

BEST PRACTICES

Practical guides for utilizing OceanBase more effectively and conveniently

Company

Learn more about OceanBase – our company, partnerships, and trust and security initiatives.

About OceanBase

Partner

Trust Center

Contact Us

International - English
中国站 - 简体中文
日本 - 日本語
Sign In
Start on Cloud

A unified distributed database ready for your transactional, analytical, and AI workloads.

DEPLOY YOUR WAY

OceanBase Cloud

The best way to deploy and scale OceanBase

OceanBase Enterprise

Run and manage OceanBase on your infra

TRY OPEN SOURCE

OceanBase Community Edition

The free, open-source distributed database

OceanBase seekdb

Open source AI native search database

Customer Stories

Real-world success stories from enterprises across diverse industries.

View All
BY USE CASES

Mission-Critical Transactions

Global & Multicloud Application

Elastic Scaling for Peak Traffic

Real-time Analytics

Active Geo-redundancy

Database Consolidation

Comprehensive knowledge hub for OceanBase.

Blog

Live Demos

Training & Certification

Documentation

Official technical guides, tutorials, API references, and manuals for all OceanBase products.

View All
PRODUCTS
OceanBase CloudOceanBase Database
ToolsConnectors and Middleware
QUICK START
OceanBase CloudOceanBase Database
BEST PRACTICES

Practical guides for utilizing OceanBase more effectively and conveniently

Learn more about OceanBase – our company, partnerships, and trust and security initiatives.

About OceanBase

Partner

Trust Center

Contact Us

Start on Cloud
编组
All Products
    • Databases
    • iconOceanBase Database
    • iconOceanBase Cloud
    • iconOceanBase Tugraph
    • iconInteractive Tutorials
    • iconOceanBase Best Practices
    • Tools
    • iconOceanBase Cloud Platform
    • iconOceanBase Migration Service
    • iconOceanBase Developer Center
    • iconOceanBase Migration Assessment
    • iconOceanBase Admin Tool
    • iconOceanBase Loader and Dumper
    • iconOceanBase Deployer
    • iconKubernetes operator for OceanBase
    • iconOceanBase Diagnostic Tool
    • iconOceanBase Binlog Service
    • Connectors and Middleware
    • iconOceanBase Database Proxy
    • iconEmbedded SQL in C for OceanBase
    • iconOceanBase Call Interface
    • iconOceanBase Connector/C
    • iconOceanBase Connector/J
    • iconOceanBase Connector/ODBC
    • iconOceanBase Connector/NET
icon

OceanBase Connector/C

V2.0.0

  • Overview
  • Installation guide
  • User Guide
    • Basic data structures
    • Basic operations
      • Basic API operations
      • API operations on prepared statements
  • C API functions
    • Overview
    • my_init()
    • mysql_affected_rows()
    • mysql_autocommit()
    • mysql_change_user()
    • mysql_character_set_name()
    • mysql_client_find_plugin()
    • mysql_client_register_plugin()
    • mysql_close()
    • mysql_commit()
    • mysql_connect()
    • mysql_create_db()
    • mysql_data_seek()
    • mysql_debug()
    • mysql_drop_db()
    • mysql_dump_debug_info()
    • mysql_eof()
    • mysql_errno()
    • mysql_error()
    • mysql_escape_string()
    • mysql_fetch_field()
    • mysql_fetch_field_direct()
    • mysql_fetch_fields()
    • mysql_fetch_lengths()
    • mysql_fetch_row()
    • mysql_field_count()
    • mysql_field_seek()
    • mysql_field_tell()
    • mysql_free_result()
    • mysql_get_character_set_info()
    • mysql_get_client_info()
    • mysql_get_client_version()
    • mysql_get_host_info()
    • mysql_get_proto_info()
    • mysql_get_server_info()
    • mysql_get_server_version()
    • mysql_get_ssl_cipher()
    • mysql_hex_string()
    • mysql_info()
    • mysql_init()
    • mysql_insert_id()
    • mysql_kill()
    • mysql_library_end()
    • mysql_library_init()
    • mysql_list_dbs()
    • mysql_list_fields()
    • mysql_list_processes()
    • mysql_list_tables()
    • mysql_load_plugin()
    • mysql_load_plugin_v()
    • mysql_more_results()
    • mysql_next_result()
    • mysql_num_fields()
    • mysql_num_rows()
    • mysql_options()
    • mysql_options4()
    • mysql_ping()
    • mysql_plugin_options()
    • mysql_query()
    • mysql_real_connect()
    • mysql_real_escape_string()
    • mysql_real_query()
    • mysql_refresh()
    • mysql_reload()
    • mysql_rollback()
    • mysql_row_seek()
    • mysql_row_tell()
    • mysql_select_db()
    • mysql_server_end()
    • mysql_server_init()
    • mysql_set_character_set()
    • mysql_set_local_infile_default()
    • mysql_set_local_infile_handler()
    • mysql_set_server_option()
    • mysql_shutdown()
    • mysql_sqlstate()
    • mysql_ssl_set()
    • mysql_stat()
    • mysql_stmt_affected_rows()
    • mysql_stmt_attr_get()
    • mysql_stmt_attr_set()
    • mysql_stmt_bind_param()
    • mysql_stmt_bind_result()
    • mysql_stmt_close()
    • mysql_stmt_data_seek()
    • mysql_stmt_errno()
    • mysql_stmt_error()
    • mysql_stmt_execute()
    • mysql_stmt_fetch()
    • mysql_stmt_fetch_column()
    • mysql_stmt_field_count()
    • mysql_stmt_free_result()
    • mysql_stmt_init()
    • mysql_stmt_insert_id()
    • mysql_stmt_next_result()
    • mysql_stmt_num_rows()
    • mysql_stmt_param_count()
    • mysql_stmt_param_metadata()
    • mysql_stmt_prepare()
    • mysql_stmt_reset()
    • mysql_stmt_result_metadata()
    • mysql_stmt_row_seek()
    • mysql_stmt_row_tell()
    • mysql_stmt_send_long_data()
    • mysql_stmt_sqlstate()
    • mysql_stmt_store_result()
    • mysql_store_result()
    • mysql_thread_end()
    • mysql_thread_id()
    • mysql_thread_init()
    • mysql_thread_safe()
    • mysql_use_result()
    • mysql_warning_count()

Download PDF

Overview Installation guide Basic data structures Basic API operations API operations on prepared statements Overview my_init() mysql_affected_rows() mysql_autocommit() mysql_change_user() mysql_character_set_name() mysql_client_find_plugin() mysql_client_register_plugin() mysql_close() mysql_commit() mysql_connect() mysql_create_db() mysql_data_seek() mysql_debug() mysql_drop_db() mysql_dump_debug_info() mysql_eof() mysql_errno() mysql_error() mysql_escape_string() mysql_fetch_field() mysql_fetch_field_direct() mysql_fetch_fields() mysql_fetch_lengths() mysql_fetch_row() mysql_field_count() mysql_field_seek() mysql_field_tell() mysql_free_result() mysql_get_character_set_info() mysql_get_client_info() mysql_get_client_version() mysql_get_host_info() mysql_get_proto_info() mysql_get_server_info() mysql_get_server_version() mysql_get_ssl_cipher() mysql_hex_string() mysql_info() mysql_init() mysql_insert_id() mysql_kill() mysql_library_end() mysql_library_init() mysql_list_dbs() mysql_list_fields() mysql_list_processes() mysql_list_tables() mysql_load_plugin() mysql_load_plugin_v() mysql_more_results() mysql_next_result() mysql_num_fields() mysql_num_rows() mysql_options() mysql_options4() mysql_ping() mysql_plugin_options() mysql_query() mysql_real_connect() mysql_real_escape_string() mysql_real_query() mysql_refresh() mysql_reload() mysql_rollback() mysql_row_seek() mysql_row_tell() mysql_select_db() mysql_server_end() mysql_server_init() mysql_set_character_set() mysql_set_local_infile_default() mysql_set_local_infile_handler() mysql_set_server_option() mysql_shutdown() mysql_sqlstate() mysql_ssl_set() mysql_stat() mysql_stmt_affected_rows() mysql_stmt_attr_get() mysql_stmt_attr_set() mysql_stmt_bind_param() mysql_stmt_bind_result() mysql_stmt_close() mysql_stmt_data_seek() mysql_stmt_errno() mysql_stmt_error() mysql_stmt_execute() mysql_stmt_fetch() mysql_stmt_fetch_column() mysql_stmt_field_count() mysql_stmt_free_result() mysql_stmt_init() mysql_stmt_insert_id() mysql_stmt_next_result()
OceanBase logo

The Unified Distributed Database for the AI Era.

Follow Us
Products
OceanBase CloudOceanBase EnterpriseOceanBase Community EditionOceanBase seekdb
Resources
DocsBlogLive DemosTraining & Certification
Company
About OceanBaseTrust CenterLegalPartnerContact Us
Follow Us

© OceanBase 2026. All rights reserved

Cloud Service AgreementPrivacy PolicySecurity
Contact Us
Document Feedback
  1. Documentation Center
  2. OceanBase Connector/C
  3. V2.0.0
iconOceanBase Connector/C
V 2.0.0
  • V 2.0.0

mysql_real_connect()

Last Updated:2026-04-10 03:06:40  Updated
share
What is on this page
Syntax
Return values
Errors
Notes

folded

share

You can call the mysql_real_connect() function to establish a connection to an OBServer running on a host.

Syntax

MYSQL *
mysql_real_connect(MYSQL *mysql,
                   const char *host,
                   const char *user,
                   const char *passwd,
                   const char *db,
                   unsigned int port,
                   const char *unix_socket,
                   unsigned long client_flag)

Return values

A MYSQL* connection handler is returned if the connection was successful, and NULL is returned if the connection was unsuccessful.

For a successful connection, the return value is the same as the value of the first argument.

Errors

  • CR_CONN_HOST_ERROR: Unable to connect to the OBServer.

  • CR_CONNECTION_ERROR: Unable to connect to the local OBServer.

  • CR_IPSOCK_ERROR: Unable to create an IP socket.

  • CR_OUT_OF_MEMORY: The memory is insufficient.

  • CR_SOCKET_CREATE_ERROR: Unable to create a Unix socket.

  • CR_UNKNOWN_HOST: Unable to find the IP address of the host name.

  • CR_VERSION_ERROR: A protocol mismatch is resulted from an attempt to connect to a server with a client library that uses a different protocol version.

  • CR_NAMEDPIPEOPEN_ERROR: Unable to create a named pipe on Windows.

  • CR_NAMEDPIPEWAIT_ERROR: Failed to wait for a named pipe on Windows.

  • CR_NAMEDPIPESETSTATE_ERROR: Unable to obtain a pipe handler on Windows.

  • CR_SERVER_LOST: connect_timeout > 0 and the time taken to connect to the server exceeded connect_timeout, or the server was down when executing init-command.

  • CR_ALREADY_CONNECTED: The MYSQL connection handler was already connected.

Notes

Arguments

  • For the first argument mysql, specify the address of an existing MYSQL structure. Before you call mysql_real_connect(), call mysql_init() to initialize the MYSQL structure.

  • The value of host can be a host name or IP address. The client attempts to connect as follows:

    • If the value of host is NULL or the string localhost, assume that the client attempts to connect to the local host:

      • On Windows, if the server enables shared-memory connections, the client connects by using a shared-memory connection.

      • On Unix, the client connects by using a Unix socket file. The unix_socket argument or the MYSQL_UNIX_PORT environment variable can be used to specify the socket name.

    • On Windows, if the value of host is ".", or if TCP/IP is not enabled and unix_socket is not specified or the host parameter is unspecified, the client connects to the server by using a named pipe, provided that the server enables named-pipe connections. If named-pipe connections are not enabled, an error occurs.

    • Otherwise, TCP/IP is used.

  • The user argument contains the user's logon ID. If user is NULL or the empty string "", the current user is assumed. In Unix, the current logon name is used. In Windows ODBC, the current username must be explicitly specified.

  • The passwd argument contains the password of the user. If passwd is NULL, only entries with a blank (empty) password field in the user table are checked. In this way, the DBA can set up the privilege system so that users obtain different privileges based on whether they have specified a password.

    Note

    Do not attempt to encrypt the password before calling mysql_real_connect(). Password encryption is automatically handled by the client API.

  • The user and passwd arguments use the character set configured for the MYSQL object. By default, the character set is latin1, but can be changed by calling mysql_options(mysql, MYSQL_SET_CHARSET_NAME, "charset_name") before connection.

  • db is the database name. If db is not NULL, the connection identifies it as the default database.

  • If port is not 0, the value is used as the port number for the TCP/IP connection. Note that the host argument determines the type of the connection.

  • If unix_socket is not NULL, the string specifies the socket or named pipe to be used. Note that the host argument determines the type of the connection.

  • The value of client_flag is usually 0, but can be set to a combination of the following flags to enable certain features:

    • CAN_HANDLE_EXPIRED_PASSWORDS: allows the client to handle expired passwords.

    • CLIENT_COMPRESS: uses compression in the client/server protocol.

    • CLIENT_FOUND_ROWS: returns the number of found (matched) rows, not the number of modified rows.

    • CLIENT_IGNORE_SIGPIPE: prevents the client library from installing a SIGPIPE signal handler. This can avoid conflicts with a handler already installed by the application.

    • CLIENT_IGNORE_SPACE: allows for spaces after function names, and makes all function names reserved words.

    • CLIENT_INTERACTIVE: allows for a period of inactivity of interactive_timeout seconds (rather than wait_timeout seconds) before closing the connection. The session variable wait_timeout of the client is set to the value of the session variable interactive_timeout.

    • CLIENT_LOCAL_FILES: enables LOAD DATA LOCAL.

    • CLIENT_MULTI_RESULTS: notifies the server that the client can handle multiple result sets from multi-statement executions or stored procedures. This flag is automatically enabled if CLIENT_MULTI_STATEMENTS is enabled.

    • CLIENT_MULTI_STATEMENTS: notifies the server that the client can send multiple statements in a single string (separated by ;). If this flag is not set, multi-statement execution is disabled.

    • CLIENT_NO_SCHEMA: prohibits the db_name.tbl_name.col_name syntax. If this syntax is used, the parser generates an error. This is useful for trapping bugs in some ODBC programs.

    • CLIENT_ODBC: not used.

    • CLIENT_SSL: uses SSL (encryption protocol). It is set internally in the client library and cannot be set within applications.

    • CLIENT_REMEMBER_OPTIONS: remembers options specified for mysql_options() calls. Without this option, if mysql_real_connect() fails, you must repeatedly call mysql_options() before trying to reconnect. With this option, you do not need to repeatedly call mysql_options().

If the program uses CALL statements to execute stored procedures, the CLIENT_MULTI_RESULTS flag must be enabled. This is because each CALL statement returns a result to indicate the call status, in addition to the result sets that might be returned by statements executed within the procedure. Because CALL can return multiple results, call mysql_next_result() in a loop manner to determine whether more results exist.

If you CLIENT_MULTI_STATEMENTS or CLIENT_MULTI_RESULTS is enabled, call mysql_next_result() in a loop manner to call the result of mysql_real_query() or mysql_query() to determine whether more results exist.

For some arguments, you can obtain their values from an option file rather than from an explicit value in a call. Therefore, call mysql_options() with the MYSQL_READ_DEFAULT_FILE or MYSQL_READ_DEFAULT_GROUP option before calling mysql_real_connect(). Then, during the mysql_real_connect() call, specify the "no-value" value for each argument to be read from an option file.

  • For host, specify NULL or the empty string ("").

  • For user, specify NULL or the empty string.

  • For passwd, specify NULL.

  • For db, specify NULL or the empty string.

  • For port, specify 0.

  • For unix_socket, specify NULL.

If no value is found in an option file for a argument, the default value is used.

Previous topic

mysql_query()
Last

Next topic

mysql_real_escape_string()
Next
What is on this page
Syntax
Return values
Errors
Notes