Problem Solution Discussion Specifying Connection Parameters by Using Option Files

1.5 Specifying Connection Parameters by Using Option Files

1.5.1 Problem

You dont want t o t ype connect ion param et ers on t he com m and line every t im e you invoke m ysql.

1.5.2 Solution

Put t he param et ers in an opt ion file.

1.5.3 Discussion

To avoid ent ering connect ion param et ers m anually, put t hem in an opt ion file for m ysql t o read aut om at ically. Under Unix, your personal opt ion file is nam ed .m y.cnf in your hom e direct ory. There are also sit e- wide opt ion files t hat adm inist rat ors can use t o specify param et ers t hat apply globally t o all users. You can use et c m y.cnf or t he m y.cnf file in t he MySQL servers dat a direct ory. Under Windows, t he opt ion files you can use are C: \ m y.cnf, t he m y.ini file in your Windows syst em direct ory, or m y.cnf in t he servers dat a direct ory. Windows m ay hide filenam e ext ensions when displaying files, so a file nam ed m y.cnf m ay appear t o be nam ed j ust m y. Your version of Windows m ay allow you t o disable ext ension- hiding. Alt ernat ively, issue a DIR com m and in a DOS window t o see full nam es. The following exam ple illust rat es t he form at used t o writ e MySQL opt ion files: general client program connection options [client] host=localhost user=cbuser password=cbpass options specific to the mysql program [mysql] no-auto-rehash specify pager for interactive mode pager=usrbinless This form at has t he following general charact erist ics: • Lines are writ t en in groups. The first line of t he group specifies t he group nam e inside of square bracket s, and t he rem aining lines specify opt ions associat ed wit h t he group. The exam ple file j ust shown has a [client] group and a [mysql] group. Wit hin a group, opt ion lines are writ t en in name=value form at , where name corresponds t o an opt ion nam e wit hout leading dashes and value is t he opt ions value. I f an opt ion doesnt t ake any value such as for t he no-auto-rehash opt ion , t he nam e is list ed by it self wit h no t railing =value part . • I f you dont need som e part icular param et er, j ust leave out t he corresponding line. For exam ple, if you norm ally connect t o t he default host localhost , you dont need any host line. I f your MySQL usernam e is t he sam e as your operat ing syst em login nam e, you can om it t he user line. • I n opt ion files, only t he long form of an opt ion is allowed. This is in cont rast t o com m and lines, where opt ions oft en can be specified using a short form or a long form . For exam ple, t he host nam e can be given using eit her - h hostname or - - host = hostname on t he com m and line; in an opt ion file, only host= hostname is allow ed. • Opt ions oft en are used for connect ion param et ers such as host , user , and password . However, t he file can specify opt ions t hat have ot her purposes. The pager opt ion shown for t he [mysql] group specifies t he paging program t hat m ysql should use for displaying out put in int eract ive m ode. I t has not hing t o do wit h how t he program connect s t o t he server. • The usual group for specifying client connect ion param et ers is [client] . This group act ually is used by all t he st andard MySQL client s, so by creat ing an opt ion file t o use w it h m ysql, you m ake it easier t o invoke ot her program s such as m ysqldum p and m ysqladm in as well. • You can define m ult iple groups in an opt ion file. A com m on convent ion is for a program t o look for param et ers in t he [client] group and in t he group nam ed aft er t he program it self. This provides a convenient way t o list general client param et ers t hat you want all client program s t o use, but st ill be able t o specify opt ions t hat apply only t o a part icular program . The preceding sam ple opt ion file illust rat es t his convent ion for t he m ysql program , which get s general connect ion param et ers from t he [client] group and also picks up t he no-auto-rehash and pager opt ions from t he [mysql] group. I f you put t he m ysql- specific opt ions in t he [client] group, t hat will result in unknown opt ion errors for all ot her program s t hat use t he [client] group and t hey wont run properly. • I f a param et er is specified m ult iple t im es in an opt ion file, t he last value found t akes precedence. This m eans t hat norm ally you should list any program - specific groups aft er t he [client] group so t hat if t here is any overlap in t he opt ions set by t he t wo groups, t he m ore general opt ions will be overridden by t he program -specific values. • Lines beginning w it h or ; charact ers are ignored as com m ent s. Blank lines are ignored, t oo. • Opt ion files m ust be plain t ext files. I f you creat e an opt ion file w it h a w ord processor t hat uses som e non- t ext form at by default , be sure t o save t he file explicit ly as t ext . Windows users especially should t ake not e of t his. • Opt ions t hat specify file or direct ory pat hnam es should be writ t en using as t he pat hnam e separat or charact er, even under Windows. I f you want t o find out which opt ions will be t aken from opt ion files by m ysql, use t his com m and: mysql --print-defaults You can also use t he m y_print _default s ut ilit y, which t akes as argum ent s t he nam es of t he opt ion file groups t hat it should read. For exam ple, m ysql looks in bot h t he [client] and [mysql] groups for opt ions, so you can check which values it will t ake from opt ion files like t his: my_print_defaults client mysql

1.6 Protecting Option Files