mysql_real_connect(3)
π mysql_real_connect β MariaDB Connector/C π
okay bestie, listen. this is mysql_real_connect(3) from the MariaDB Connector/C manual, and it's literally the moment your code stops being lonely and actually talks to a database server. it's giving first-day-of-school energy. let's get into it π«Ά
β¨ Name β¨
mysql_real_connect β establishes a connection to a MariaDB database server. that's the whole vibe. that's the job. no notes. π
πΈ 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);
look at all those arguments. eight of them. she's high maintenance but she delivers, fr fr. π
π¦ Description π¦
Establishes a connection to a database server. that's it, that's the tea. β but keep reading because the details are where the β¨magicβ¨ lives.
π Parameter π
mysqlβ a mysql handle, which was previously allocated bymysql_init(3). do NOT try to freestyle this one, bestie, it has to come frommysql_init(3)first or it's just a sad little pointer with no dreams. πhostβ can be either a host name or an IP address. if you pass theNULLvalue or the string"localhost"to this parameter, the local host is assumed. When possible, pipes will be used instead of the TCP/IP protocol. and since version 3.3.0 you can also drop a comma separated list of hosts in here for simple fail over, in case one or more of those hosts are just not available (rude of them, honestly). π§userβ the user name. IfNULLor an empty string""is specified, the current user will be used. lowkey convenient.passwdβ If provided orNULL, 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 was provided or not). so like, one username, two personalities, depending on whether you brought a password. main character energy tbh. πdbβ if provided will specify the default database to be used when performing queries. give her a home base.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. here's the full roster, no gatekeeping:CLIENT_FOUND_ROWSβ Return the number of matched rows instead of number of changed rows.CLIENT_NO_SCHEMAβ Forbids the use ofdatabase.tablename.columnsyntax and forces the SQL parser to generate an error.CLIENT_COMPRESSβ Use compression protocol.CLIENT_IGNORE_SPACEβ Allows spaces after function names. All function names will become reserved words.CLIENT_LOCAL_FILESβ AllowsLOAD DATA LOCALstatements.CLIENT_MULTI_STATEMENTSβ Allows the client to send multiple statements in one command. Statements will be divided by a semicolon.CLIENT_MULTI_RESULTSβ Indicates that the client is able to handle multiple result sets from stored procedures or multi statements. This option will be automatically set ifCLIENT_MULTI_STATEMENTSis set.CLIENT_REMEMBER_OPTIONSβ Remembers options passed tomysql_optionsv(3)if a connect attempt failed. IfMYSQL_OPTIONS_RECONNECToption 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. and if you get that NULL? don't panic, just check the mysql_errno(3) and mysql_error(3) functions for more information. they will tell you exactly what went wrong. π΅οΈββοΈ
π Notes π
- The password doesn't need to be encrypted before executing
mysql_real_connect(). This will be handled in the client server protocol. so like, don't do extra credit that nobody asked for. π - The connection handle can't be reused for establishing a new connection. It must be closed and reinitialized before. one handle, one connection, no takebacks. π
mysql_real_connect()must complete successfully before you can execute any other API functions besidemysql_optionsv(3). this is not a suggestion, bestie. it's a rule.hostparameter 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.
- hostname and port must be separated by a colon (
Examples for failover host string:
host=[::1]:3306,192.168.0.1:3306,test.example.com
host=127.0.0.1:3306,
β οΈ note that trailing comma in the single-host example β that's not a typo, that's the rule. she's picky and we respect her. π
β See also β
and that's the whole page, bestie!! you just read all of it, no skimming, I'm so proud. π₯Ήβ¨
Version 3.4 of the MariaDB Connector/C manual, and that's a wrap β go forth and connect responsibly, you absolute icon. πππ¦
π¦ where this page comes from
- alpine 3.22: mariadb-doc 11.4.13-r0 Β· GPL-2.0-or-later
- alpine 3.23: mariadb-doc 11.4.13-r0 Β· GPL-2.0-or-later
- alpine 3.24: mariadb-doc 11.8.9-r0 Β· GPL-2.0-only
- arch rolling: mariadb 13.0.2-2 Β· GPL-2.0-only
- arch rolling: mariadb-lts 12.3.3-2 Β· GPL-2.0-only
- centos-stream 10: mariadb-connector-c-doc 3.4.4-2.el10 Β· LGPL-2.1-or-later
- debian trixie: libmariadb-dev 1:11.8.6-0+deb13u1 Β· GPL-2.0-only
- fedora 43: mariadb-connector-c-doc 3.4.5-2.fc43 Β· LGPL-2.1-or-later AND PHP-3.0 AND PHP-3.01
- fedora 43: mariadb-connector-c-doc 3.4.9-2.fc43 Β· LGPL-2.1-or-later AND PHP-3.0 AND PHP-3.01 AND LicenseRef-Fedora-Public-Domain
- fedora 44: mariadb-connector-c-doc 3.4.8-3.fc44 Β· LGPL-2.1-or-later AND PHP-3.0 AND PHP-3.01
- fedora 44: mariadb-connector-c-doc 3.4.9-2.fc44 Β· LGPL-2.1-or-later AND PHP-3.0 AND PHP-3.01 AND LicenseRef-Fedora-Public-Domain
- nixpkgs unstable: mariadb-server 11.8.8
- opensuse-leap 16.0: libmariadb-devel 3.4.9-160000.1.1 Β· LGPL-2.1-or-later
- opensuse-leap 16.0: libmariadb-devel 3.4.9-160000.1.1 Β· LGPL-2.1-or-later
- opensuse-leap 16.0: libmariadb-devel 3.4.9-160000.1.1 Β· LGPL-2.1-or-later
- opensuse-leap 16.0: libmariadb-devel 3.4.9-160000.1.1 Β· LGPL-2.1-or-later
- ubuntu questing: libmariadb-dev 1:11.8.3-1build1
- ubuntu resolute: libmariadb-dev 1:11.8.6-5 Β· GPL-2.0-only
- ubuntu resolute: libmariadb-dev 1:11.8.6-5ubuntu0.1 Β· GPL-2.0-only
- ubuntu stonking: libmariadb-dev 1:11.8.9+ds-4ubuntu2 Β· GPL-2.0-only
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms