您的位置:首页 > 数据库 > MySQL

mysql中的mysql_real_connect连接参数设置

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



//函数原型描述
MYSQL*mysql_real_connect(MYSQL*mysql,constchar*host,constchar*user,
constchar*passwd,constchar*db,unsignedintport,constchar*unix_socket,
unsignedlongclient_flag)

Description

mysql_real_connect()attemptstoestablishaconnectiontoaMySQLdatabaseengine
runningonhost.mysql_real_connect()mustcompletesuccessfullybeforeyoucan
executeanyotherAPIfunctionsthatrequireavalidMYSQLconnectionhandlestructure.

Theparametersarespecifiedasfollows:

*

ThefirstparametershouldbetheaddressofanexistingMYSQLstructure.Before
callingmysql_real_connect()youmustcallmysql_init()toinitializetheMYSQL
structure.Youcanchangealotofconnectoptionswiththemysql_options()call.
SeeSectionÂ17.2.3.47,“mysql_options()”.
*

ThevalueofhostmaybeeitherahostnameoranIPaddress.IfhostisNULLorthe
string"localhost",aconnectiontothelocalhostisassumed.IftheOSsupportssockets
(Unix)ornamedpipes(Windows),theyareusedinsteadofTCP/IPtoconnecttotheserver.
*

Theuserparametercontainstheuser'sMySQLloginID.IfuserisNULLortheempty
string"",thecurrentuserisassumed.UnderUnix,thisisthecurrentloginname.Under
WindowsODBC,thecurrentusernamemustbespecifiedexplicitly.SeeSectionÂ18.1.9.2,
“ConfiguringaMyODBCDSNonWindows”.
*

Thepasswdparametercontainsthepasswordforuser.IfpasswdisNULL,onlyentries
intheusertablefortheuserthathaveablank(empty)passwordfieldarecheckedfora
match.ThisallowsthedatabaseadministratortosetuptheMySQLprivilegesystemin
suchawaythatusersgetdifferentprivilegesdependingonwhethertheyhavespecified
apassword.

Note:Donotattempttoencryptthepasswordbeforecallingmysql_real_connect();
passwordencryptionishandledautomaticallybytheclientAPI.
*

dbisthedatabasename.IfdbisnotNULL,theconnectionsetsthedefaultdatabase
tothisvalue.
*

Ifportisnot0,thevalueisusedastheportnumberfortheTCP/IPconnection.Note
thatthehostparameterdeterminesthetypeoftheconnection.
*

Ifunix_socketisnotNULL,thestringspecifiesthesocketornamedpipethatshould
beused.Notethatthehostparameterdeterminesthetypeoftheconnection.
*

Thevalueofclient_flagisusually0,butcanbesettoacombinationofthefollowing
flagstoenablecertainfeatures:

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

FlagNameFlagDescription
CLIENT_COMPRESSUsecompressionprotocol.
CLIENT_FOUND_ROWSReturnthenumberoffound(matched)rows,notthenumberof
changedrows.
CLIENT_IGNORE_SPACEAllowspacesafterfunctionnames.Makesallfunctionsnames
reservedwords.
CLIENT_INTERACTIVEAllowinteractive_timeoutseconds(insteadofwait_timeout
seconds)ofinactivitybeforeclosingtheconnection.Theclient'ssessionwait_timeout
variableissettothevalueofthesessioninteractive_timeoutvariable.
CLIENT_LOCAL_FILESEnableLOADDATALOCALhandling.
CLIENT_MULTI_STATEMENTSTelltheserverthattheclientmaysendmultiple
statementsinasinglestring(separatedby‘;’).Ifthisflagisnotset,
multiple-statementexecutionisdisabled.AddedinMySQL4.1.
CLIENT_MULTI_RESULTSTelltheserverthattheclientcanhandlemultipleresult
setsfrommultiple-statementexecutionsorstoredprocedures.Thisisautomatically
setifCLIENT_MULTI_STATEMENTSisset.AddedinMySQL4.1.
CLIENT_NO_SCHEMADon'tallowthedb_name.tbl_name.col_namesyntax.Thisisfor
ODBC.Itcausestheparsertogenerateanerrorifyouusethatsyntax,whichisuseful
fortrappingbugsinsomeODBCprograms.
CLIENT_ODBCTheclientisanODBCclient.Thischangesmysqldtobemore
ODBC-friendly.
CLIENT_SSLUseSSL(encryptedprotocol).Thisoptionshouldnotbesetby
applicationprograms;itissetinternallyintheclientlibrary.Instead,use
mysql_ssl_set()beforecallingmysql_real_connect().

Forsomeparameters,itispossibletohavethevaluetakenfromanoptionfilerather
thanfromanexplicitvalueinthemysql_real_connect()call.Todothis,call
mysql_options()withtheMYSQL_READ_DEFAULT_FILEorMYSQL_READ_DEFAULT_GROUPoption
beforecallingmysql_real_connect().Then,inthemysql_real_connect()call,specify
the“no-value”valueforeachparametertobereadfromanoptionfile:

*

Forhost,specifyavalueofNULLortheemptystring("").
*

Foruser,specifyavalueofNULLortheemptystring.
*

Forpasswd,specifyavalueofNULL.(Forthepassword,avalueoftheemptystringin
themysql_real_connect()callcannotbeoverriddeninanoptionfile,becausetheempty
stringindicatesexplicitlythattheMySQLaccountmusthaveanemptypassword.)
*

Fordb,specifyavalueofNULLortheemptystring.
*

Forport,specifyavalueof0.
*

Forunix_socket,specifyavalueofNULL.

Ifnovalueisfoundinanoptionfileforaparameter,itsdefaultvalueisusedas
indicatedinthedescriptionsgivenearlierinthissection.

ReturnValues

AMYSQL*connectionhandleiftheconnectionwassuccessful,NULLiftheconnection
wasunsuccessful.Forasuccessfulconnection,thereturnvalueisthesameasthevalue
ofthefirstparameter.
//返回值:当连接成功时,返回MYSQL连接句柄,失败,返回NULL。当成功时,返回值与第一个参数值是//相同的。

Errors

*

CR_CONN_HOST_ERROR

FailedtoconnecttotheMySQLserver.
*

CR_CONNECTION_ERROR

FailedtoconnecttothelocalMySQLserver.
*

CR_IPSOCK_ERROR

FailedtocreateanIPsocket.
*

CR_OUT_OF_MEMORY

Outofmemory.
*

CR_SOCKET_CREATE_ERROR

FailedtocreateaUnixsocket.
*

CR_UNKNOWN_HOST

FailedtofindtheIPaddressforthehostname.
*

CR_VERSION_ERROR

Aprotocolmismatchresultedfromattemptingtoconnecttoaserverwithaclient
librarythatusesadifferentprotocolversion.Thiscanhappenifyouuseaveryold
clientlibrarytoconnecttoanewserverthatwasn'tstartedwiththe--old-protocol
option.
*

CR_NAMEDPIPEOPEN_ERROR

FailedtocreateanamedpipeonWindows.
*

CR_NAMEDPIPEWAIT_ERROR

FailedtowaitforanamedpipeonWindows.
*

CR_NAMEDPIPESETSTATE_ERROR

FailedtogetapipehandleronWindows.
*

CR_SERVER_LOST

Ifconnect_timeout>0andittooklongerthanconnect_timeoutsecondstoconnectto
theserveroriftheserverdiedwhileexecutingtheinit-command.

因此mysql_real_connect()函数调用为:
mysql_real_connect(mysql,"localhost","root",NULL,"test",0,NULL,0);
判断是否出错,出错调用mysql_error()
函数显示出错信息,或使用mysql_errno()函数获取出错代号。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: