Problem Solution Discussion Connecting to the MySQL Server, Selecting a Database, and Disconnecting

t o a MySQL server running on t he local host t o access a dat abase nam ed cookbook . I f you need t o creat e t he account or t he dat abase, see t he inst ruct ions in t hat chapt er. • The recipes assum e a cert ain basic underst anding of t he API languages. I f a recipe uses const ruct s wit h which youre not fam iliar, consult a good general t ext for t he language in which youre int erest ed. Appendix C list s som e sources t hat m ay be helpful. • Proper execut ion of som e of t he program s m ay require t hat you set environm ent variables t hat cont rol t heir behavior. See Recipe 1.9 for det ails about how t o do t his.

2.2 Connecting to the MySQL Server, Selecting a Database, and Disconnecting

2.2.1 Problem

You need t o est ablish a connect ion t o t he server t o access a dat abase, and t o shut dow n t he connect ion when youre done.

2.2.2 Solution

Each API provides funct ions for connect ing and disconnect ing. The connect ion rout ines require t hat you provide param et ers specifying t he MySQL user account you want t o use. You can also specify a dat abase t o use. Som e API s allow t his at connect ion t im e; ot hers require a separat e call aft er connect ing.

2.2.3 Discussion

The program s in t his sect ion show how t o perform t hree fundam ent al operat ions t hat are com m on t o t he vast m aj orit y of MySQL program s: • Est a blish in g a con n e ct ion t o t h e M ySQL se r ve r . Every program t hat uses MySQL does t his, no m at t er which API you use. The det ails on specifying connect ion param et ers vary bet ween API s, and som e API s provide m ore flexibilit y t han ot hers. However, t here are m any com m on elem ent s. For exam ple, you m ust specify t he host where t he server is running, as well as t he nam e and password for t he MySQL account t hat youre using t o access t he server. • Se le ct in g a da t a ba se . Most MySQL program s select a dat abase, eit her when t hey connect t o t he server or im m ediat ely t hereaft er. • D iscon n e ct in g fr om t h e se r ve r Each API provides a m eans of shut t ing down an open connect ion. I t s best t o close t he connect ion as soon as youre done wit h t he server so t hat it can free up any resources t hat are allocat ed t o servicing t he connect ion. Ot herwise, if your program perform s addit ional com put at ions aft er accessing t he server, t he connect ion will be held open longer t han necessary. I t s also preferable t o close t he connect ion explicit ly. I f a program sim ply t erm inat es wit hout closing t he connect ion, t he MySQL server event ually not ices, but shut t ing down t he connect ion explicit ly allows t he server t o perform an orderly close on it s end im m ediat ely. Our exam ple program s for each API in t his sect ion show how t o connect t o t he server, select t he cookbook dat abase, and disconnect . However, on occasion you m ight want t o writ e a MySQL program t hat doesnt select a dat abase. This would be t he case if you plan t o issue a query t hat doesnt require a default dat abase, such as SHOW VARIABLES or SHOW DATABASES . Or perhaps youre writ ing an int eract ive program t hat connect s t o t he server and allows t he user t o specify t he dat abase aft er t he connect ion has been m ade. To cover such sit uat ions, t he discussion for each API also indicat es how t o connect wit hout select ing any dat abase. The Meaning of localhost in MySQL One of t he param et ers you specify when connect ing t o a MySQL server is t he host w here t he server is running. Most program s t reat t he host nam e localhost and t he I P address 127.0.0.1 as synonym ous. Under Unix, MySQL program s behave different ly; by convent ion, t hey t reat t he host nam e localhost specially and at t em pt t o connect t o t he server using a Unix dom ain socket file. To force a TCP I P connect ion t o t he local host , use t he I P address 127.0.0.1 rat her t han t he host nam e localhost . Under Windows, localhost and 127.0.0.1 are t reat ed t he sam e, because Windows doesnt have Unix dom ain socket s. The default port is 3306 for TCP I P connect ions. The pat hnam e for t he Unix dom ain socket varies, alt hough it s oft en t m p m ysql.sock. The recipes indicat e how t o specify t he socket file pat hnam e or TCP I P port num ber explicit ly if you dont want t o use t he default .

2.2.4 Perl