mysql_real_connect - Man Page

Name

mysql_real_connect - establishes a connection to a MariaDB database server

Synopsis

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 flags);

Description

Establishes a connection to a database server.

Parameter

  • mysql - a mysql handle, which was previously allocated by mysql_init(3)
  • host - can be either a host name or an IP address. Passing the NULL value or the string “localhost” to this parameter, the local host is assumed. When possible, pipes will be used instead of the TCP/IP protocol. Since version 3.3.0 it is also possible to provide a comma separated list of hosts for simple fail over in case of one or more hosts are not available.
  • user - the user name. If NULL or an empty string “” is specified, the current user will be used.
  • passwd - If provided or NULL, the server will attempt to authenticate the user against those user records which have no password only. This allows one username to be used with different permissions (depending on if a password as provided or not).
  • db - if provided will specify the default database to be used when performing queries.
  • port - specifies the port number to attempt to connect to the server.
  • unix_socket - specifies the socket or named pipe that should be used.
  • flags - the flags allows various connection options to be set
FlagDescription
CLIENT_FOUND_ROWSReturn the number of matched rows instead of number of changed rows.
CLIENT_NO_SCHEMAForbids the use of database.tablename.column syntax and forces the SQL parser to generate an error.
CLIENT_COMPRESSUse compression protocol
CLIENT_IGNORE_SPACEAllows spaces after function names. All function names will become reserved words.
CLIENT_LOCAL_FILESAllows LOAD DATA LOCAL statements
CLIENT_MULTI_STATEMENTSAllows the client to send multiple statements in one command. Statements will be divided by a semicolon.
CLIENT_MULTI_RESULTSIndicates that the client is able to handle multiple result sets from stored procedures or multi statements. This option will be automatically set if CLIENT_MULTI_STATEMENTS is set.
CLIENT_REMEMBER_OPTIONSRemembers options passed to mysql_optionsv(3) if a connect attempt failed. If MYSQL_OPTIONS_RECONNECT option was set to true, options will be saved and used for reconnection.

Return value

returns a connection handle (same as passed for 1st parameter) or NULL on error. On error, please check mysql_errno(3) and mysql_error(3) functions for more information.

Notes

  • The password doesn’t need to be encrypted before executing mysql_real_connect(). This will be handled in the client server protocol.
  • The connection handle can’t be reused for establishing a new connection. It must be closed and reinitialized before.
  • mysql_real_connect() must complete successfully before you can execute any other API functions beside mysql_optionsv(3).
  • host parameter may contain multiple host/port combinations (supported since version 3.3.0). The following syntax is required:

    • hostname and port must be separated by a colon (:)
    • IPv6 addresses must be enclosed within square brackets
    • hostname:port pairs must be be separated by a comma (,)
    • if only one host:port was specified, the host string needs to end with a comma.
    • if no port was specified, the default port will be used.

    Examples for failover host string:

    host=[::1]:3306,192.168.0.1:3306,test.example.com

    host=127.0.0.1:3306,

See also

Referenced By

mariadb_cancel(3), mariadb_connection(3), mariadb_reconnect(3), mysql_affected_rows(3), mysql_autocommit(3), mysql_change_user(3), mysql_close(3), mysql_commit(3), mysql_errno(3), mysql_error(3), mysql_field_count(3), mysql_get_character_set_info(3), mysql_get_host_info(3), mysql_get_proto_info(3), mysql_get_server_info(3), mysql_get_server_version(3), mysql_get_socket(3), mysql_get_ssl_cipher(3), mysql_info(3), mysql_init(3), mysql_kill(3), mysql_more_results(3), mysql_next_result(3), mysql_options(3), mysql_options4(3), mysql_optionsv(3), mysql_ping(3), mysql_query(3), mysql_read_query_result(3), mysql_real_escape_string(3), mysql_real_query(3), mysql_refresh(3), mysql_reset_connection(3), mysql_rollback(3), mysql_select_db(3), mysql_send_query(3), mysql_session_track_get_first(3), mysql_session_track_get_next(3), mysql_set_character_set(3), mysql_set_server_option(3), mysql_shutdown(3), mysql_ssl_set(3), mysql_stat(3), mysql_stmt_init(3), mysql_store_result(3), mysql_thread_id(3), mysql_thread_init(3), mysql_use_result(3), mysql_warning_count(3).

Version 3.4 MariaDB Connector/C