分享
 
 
 

mysql中的mysql_real_connect连接参数设置

王朝mysql·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

在前一篇文章中,讲述了如何进行mysql源程序代码的编译链接,但是没有讲述运行情况,在按照上一篇文章代码下进行编译运行后,发现无法链接数据库文件,显然是在mysql_real_connect()函数中出现了问题。在mysql的英文手册中找到关于mysql_real_connect()的如下描述:

//函数原型描述 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)

Description

mysql_real_connect() attempts to establish a connection to a MySQL database engine

running on host. mysql_real_connect() must complete successfully before you can

execute any other API functions that require a valid MYSQL connection handle structure.

The parameters are specified as follows:

*

The first parameter should be the address of an existing MYSQL structure. Before

calling mysql_real_connect() you must call mysql_init() to initialize the MYSQL

structure. You can change a lot of connect options with the mysql_options() call.

See Section 17.2.3.47, “mysql_options()”.

*

The value of host may be either a hostname or an IP address. If host is NULL or the

string "localhost", a connection to the local host is assumed. If the OS supports sockets

(Unix) or named pipes (Windows), they are used instead of TCP/IP to connect to the server.

*

The user parameter contains the user's MySQL login ID. If user is NULL or the empty

string "", the current user is assumed. Under Unix, this is the current login name. Under

Windows ODBC, the current username must be specified explicitly. See Section 18.1.9.2,

“Configuring a MyODBC DSN on Windows”.

*

The passwd parameter contains the password for user. If passwd is NULL, only entries

in the user table for the user that have a blank (empty) password field are checked for a

match. This allows the database administrator to set up the MySQL privilege system in

such a way that users get different privileges depending on whether they have specified

a password.

Note: Do not attempt to encrypt the password before calling mysql_real_connect();

password encryption is handled automatically by the client API.

*

db is the database name. If db is not NULL, the connection sets the default database

to this value.

*

If port is not 0, the value is used as the port number for the TCP/IP connection. Note

that the host parameter determines the type of the connection.

*

If unix_socket is not NULL, the string specifies the socket or named pipe that should

be used. Note that the host parameter 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:

//上面描述了五个参数的主要取值,MYSQL *为mysql_init函数返回的指针,host为null或 // localhost时链接的是本地的计算机,当mysql默认安装在unix(或类unix)系统中,root账户是没// 有密码的,因此用户名使用root,密码为null,当db为空的时候,函数链接到默认数据库,在进行 // mysql安装时会存在默认的test数据库,因此此处可以使用test数据库名称,port端口为0,使用 // unix连接方式,unix_socket为null时,表明不使用socket或管道机制,最后一个参数经常设置为0

Flag Name Flag Description

CLIENT_COMPRESS Use compression protocol.

CLIENT_FOUND_ROWS Return the number of found (matched) rows, not the number of

changed rows.

CLIENT_IGNORE_SPACE Allow spaces after function names. Makes all functions names

reserved words.

CLIENT_INTERACTIVE Allow interactive_timeout seconds (instead of wait_timeout

seconds) of inactivity before closing the connection. The client's session wait_timeout

variable is set to the value of the session interactive_timeout variable.

CLIENT_LOCAL_FILES Enable LOAD DATA LOCAL handling.

CLIENT_MULTI_STATEMENTS Tell the server that the client may send multiple

statements in a single string (separated by ‘;’). If this flag is not set,

multiple-statement execution is disabled. Added in MySQL 4.1.

CLIENT_MULTI_RESULTS Tell the server that the client can handle multiple result

sets from multiple-statement executions or stored procedures. This is automatically

set if CLIENT_MULTI_STATEMENTS is set. Added in MySQL 4.1.

CLIENT_NO_SCHEMA Don't allow the db_name.tbl_name.col_name syntax. This is for

ODBC. It causes the parser to generate an error if you use that syntax, which is useful

for trapping bugs in some ODBC programs.

CLIENT_ODBC The client is an ODBC client. This changes mysqld to be more

ODBC-friendly.

CLIENT_SSL Use SSL (encrypted protocol). This option should not be set by

application programs; it is set internally in the client library. Instead, use

mysql_ssl_set() before calling mysql_real_connect().

For some parameters, it is possible to have the value taken from an option file rather

than from an explicit value in the mysql_real_connect() call. To do this, call

mysql_options() with the MYSQL_READ_DEFAULT_FILE or MYSQL_READ_DEFAULT_GROUP option

before calling mysql_real_connect(). Then, in the mysql_real_connect() call, specify

the “no-value” value for each parameter to be read from an option file:

*

For host, specify a value of NULL or the empty string ("").

*

For user, specify a value of NULL or the empty string.

*

For passwd, specify a value of NULL. (For the password, a value of the empty string in

the mysql_real_connect() call cannot be overridden in an option file, because the empty

string indicates explicitly that the MySQL account must have an empty password.)

*

For db, specify a value of NULL or the empty string.

*

For port, specify a value of 0.

*

For unix_socket, specify a value of NULL.

If no value is found in an option file for a parameter, its default value is used as

indicated in the descriptions given earlier in this section.

Return Values

A MYSQL* connection handle if the connection was successful, NULL if the connection

was unsuccessful. For a successful connection, the return value is the same as the value

of the first parameter.

// 返回值:当连接成功时,返回MYSQL连接句柄,失败,返回NULL。当成功时,返回值与第一个参数值是// 相同的。

Errors

*

CR_CONN_HOST_ERROR

Failed to connect to the MySQL server.

*

CR_CONNECTION_ERROR

Failed to connect to the local MySQL server.

*

CR_IPSOCK_ERROR

Failed to create an IP socket.

*

CR_OUT_OF_MEMORY

Out of memory.

*

CR_SOCKET_CREATE_ERROR

Failed to create a Unix socket.

*

CR_UNKNOWN_HOST

Failed to find the IP address for the hostname.

*

CR_VERSION_ERROR

A protocol mismatch resulted from attempting to connect to a server with a client

library that uses a different protocol version. This can happen if you use a very old

client library to connect to a new server that wasn't started with the --old-protocol

option.

*

CR_NAMEDPIPEOPEN_ERROR

Failed to create a named pipe on Windows.

*

CR_NAMEDPIPEWAIT_ERROR

Failed to wait for a named pipe on Windows.

*

CR_NAMEDPIPESETSTATE_ERROR

Failed to get a pipe handler on Windows.

*

CR_SERVER_LOST

If connect_timeout > 0 and it took longer than connect_timeout seconds to connect to

the server or if the server died while executing the init-command.

因此mysql_real_connect()函数调用为:

mysql_real_connect(mysql,"localhost","root",NULL,"test",0,NULL,0);

判断是否出错,出错调用mysql_error()函数显示出错信息,或使用mysql_errno()函数获取出错代号。

http://blog.csdn.net/newrain021011/archive/2007/03/05/1521622.aspx

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有