mysql_real_connect传参_mysql中的mysql_real_connect连接参数设置

mysql_real_connect()函数用于建立与MySQL数据库的连接。该函数需要一个初始化的MYSQL结构体,主机名(可为localhost或IP),用户名,密码,数据库名,端口号,Unix套接字路径及客户端标志。当host为NULL或'localhost'时,默认连接本地主机,若未指定密码,系统将检查无密码账户。db参数可指定默认数据库,client_flag通常为0。若需使用压缩协议、本地文件处理等功能,可设置相应标志位。
摘要由CSDN通过智能技术生成

//函数原型描述

0818b9ca8b590ca3270a3433284dd417.png

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)

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.pngDescription

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.pngmysql_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.

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.pngThe parameters are specified

as

follows:

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

*

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png      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()”.

0818b9ca8b590ca3270a3433284dd417.png

*

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png      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.

0818b9ca8b590ca3270a3433284dd417.png

*

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png      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”.

0818b9ca8b590ca3270a3433284dd417.png

*

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png      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.

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png      Note: Do not attempt to encrypt the password before calling mysql_real_connect();

password encryption

is

handled automatically by the client API.

0818b9ca8b590ca3270a3433284dd417.png

*

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png      db

is

the database name. If db

is

not NULL, the connection sets the

default

database

to

this

value.

0818b9ca8b590ca3270a3433284dd417.png

*

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png      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.

0818b9ca8b590ca3270a3433284dd417.png

*

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png      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.

0818b9ca8b590ca3270a3433284dd417.png

*

0818b9ca8b590ca3270a3433284dd417.png      The value of client_flag

is

usually

0

, but can be

set

to a combination of the following

flags to enable certain features:

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

0818b9ca8b590ca3270a3433284dd417.png      Flag Name    Flag Description

0818b9ca8b590ca3270a3433284dd417.png      CLIENT_COMPRESS    Use compression protocol.

0818b9ca8b590ca3270a3433284dd417.png      CLIENT_FOUND_ROWS    Return the number of found (matched) rows, not the number of

changed rows.

0818b9ca8b590ca3270a3433284dd417.png      CLIENT_IGNORE_SPACE    Allow spaces after function names. Makes all functions names

reserved words.

0818b9ca8b590ca3270a3433284dd417.png      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.

0818b9ca8b590ca3270a3433284dd417.png

CLIENT_LOCAL_FILES    Enable LOAD DATA LOCAL handling.

0818b9ca8b590ca3270a3433284dd417.png      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

.

0818b9ca8b590ca3270a3433284dd417.png      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

.

0818b9ca8b590ca3270a3433284dd417.png      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.

0818b9ca8b590ca3270a3433284dd417.png

CLIENT_ODBC    The client

is

an ODBC client. This changes mysqld to be more

ODBC

-

friendly.

0818b9ca8b590ca3270a3433284dd417.png      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().

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.pngFor 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:

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

*

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png      For host, specify a value of NULL or the empty

string

(

""

).

0818b9ca8b590ca3270a3433284dd417.png

*

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png      For user, specify a value of NULL or the empty

string

.

0818b9ca8b590ca3270a3433284dd417.png

*

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png      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.)

0818b9ca8b590ca3270a3433284dd417.png

*

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png      For db, specify a value of NULL or the empty

string

.

0818b9ca8b590ca3270a3433284dd417.png

*

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png      For port, specify a value of

0

.

0818b9ca8b590ca3270a3433284dd417.png

*

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png      For unix_socket, specify a value of NULL. 

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.pngIf 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.

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.pngReturn Values

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.pngA 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。当成功时,返回值与第一个参数值是// 相同的。

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.pngErrors

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

*

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png      CR_CONN_HOST_ERROR

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png      Failed to connect to the MySQL server.

0818b9ca8b590ca3270a3433284dd417.png

*

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png      CR_CONNECTION_ERROR

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png      Failed to connect to the local MySQL server.

0818b9ca8b590ca3270a3433284dd417.png

*

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png      CR_IPSOCK_ERROR

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png      Failed to create an IP socket.

0818b9ca8b590ca3270a3433284dd417.png

*

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png      CR_OUT_OF_MEMORY

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png      Out of memory.

0818b9ca8b590ca3270a3433284dd417.png

*

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png      CR_SOCKET_CREATE_ERROR

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png      Failed to create a Unix socket.

0818b9ca8b590ca3270a3433284dd417.png

*

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png      CR_UNKNOWN_HOST

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png      Failed to find the IP address

for

the hostname.

0818b9ca8b590ca3270a3433284dd417.png

*

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png      CR_VERSION_ERROR

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png      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.

0818b9ca8b590ca3270a3433284dd417.png

*

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png      CR_NAMEDPIPEOPEN_ERROR

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png      Failed to create a named pipe on Windows.

0818b9ca8b590ca3270a3433284dd417.png

*

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png      CR_NAMEDPIPEWAIT_ERROR

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png      Failed to wait

for

a named pipe on Windows.

0818b9ca8b590ca3270a3433284dd417.png

*

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png      CR_NAMEDPIPESETSTATE_ERROR

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png      Failed to

get

a pipe handler on Windows.

0818b9ca8b590ca3270a3433284dd417.png

*

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png      CR_SERVER_LOST

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png      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.

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值