Problem Solution Discussion Setting Up a MySQL User Account

1.2 Setting Up a MySQL User Account

1.2.1 Problem

You need t o creat e an account t o use for connect ing t o t he MySQL server running on a given host .

1.2.2 Solution

Use t he GRANT st at em ent t o set up t he MySQL user account . Then use t hat account s nam e and passw ord t o m ake connect ions t o t he server.

1.2.3 Discussion

Connect ing t o a MySQL server requires a usernam e and password. You can also specify t he nam e of t he host where t he server is running. I f you dont specify connect ion param et ers explicit ly, m ysql assum es default values. For exam ple, if you specify no host nam e, m ysql t ypically assum es t he server is running on t he local host . The following exam ple shows how t o use t he m ysql program t o connect t o t he server and issue a GRANT st at em ent t hat set s up a user account wit h privileges for accessing a dat abase nam ed cookbook . The argum ent s t o m ysql include - h localhost t o connect t o t he MySQL server running on t he local host , - p t o t ell m ysql t o prom pt for a password, and -u root t o connect as t he MySQL root user. Text t hat you t ype is shown in bold; non-bold t ext is program out put : mysql -h localhost -p -u root Enter password: mysql GRANT ALL ON cookbook. TO cbuserlocalhost IDENTIFIED BY cbpass; Query OK, 0 rows affected 0.09 sec mysql QUIT Bye Aft er you ent er t he m ysql com m and shown on t he first line, if you get a m essage indicat ing t hat t he program cannot be found or t hat it is a bad com m and, see Recipe 1.8 . Ot herw ise, when m ysql print s t he password prom pt , ent er t he MySQL root password where you see t he . I f t he MySQL root user has no password, j ust press Ret urn at t he password prom pt . Then issue a GRANT st at em ent like t he one shown. To use a dat abase nam e ot her t han cookbook , subst it ut e it s nam e where you see cookbook in t he GRANT st at em ent . Not e t hat you need t o grant privileges for t he dat abase even if t he user account already exist s. However, in t hat case, youll likely want t o om it t he IDENTIFIED BY cbpass par t of t he st at em ent , because ot herwise youll change t hat account s current password. The host nam e part of cbuserlocalhost indicat es t he host from which youll be connect ing t o t he MySQL server t o access t he cookbook dat abase. To set up an account t hat will connect t o a server running on t he local host , use localhost , as shown. I f you plan t o m ake connect ions t o t he server from anot her host , subst it ut e t hat host in t he GRANT st at em ent . For exam ple, if youll be connect ing t o t he server as cbuser from a host nam ed xyz.com , t he GRANT st at em ent should look like t his: mysql GRANT ALL ON cookbook. TO cbuserxyz.com IDENTIFIED BY cbpass; I t m ay have occurred t o you t hat t heres a bit of a paradox involved in t he procedure j ust described. That is, t o set up a user account t hat can m ake connect ions t o t he MySQL server, you m ust connect t o t he server first so t hat you can issue t he GRANT st at em ent . I m assum ing t hat you can already connect as t he MySQL root user, because GRANT can be used only by a user such as root t hat has t he adm inist rat ive privileges needed t o set up ot her user account s. I f you cant connect t o t he server as root , ask your MySQL adm inist rat or t o issue t he GRANT st at em ent for you. Once t hat has been done, you should be able t o use t he new MySQL account t o connect t o t he server, creat e your own dat abase, and proceed from t here on your ow n. MySQL Accounts and Login Accounts MySQL account s and login account s for your operat ing syst em are different . For exam ple, t he MySQL root user and t he Unix root user are separat e and have not hing t o do wit h each ot her, even t hough t he usernam e is t he sam e in each case. This m eans t hey are very likely t o have different passwords. I t also m eans you cannot creat e new MySQL account s by creat ing login account s for your operat ing syst em ; use t he GRANT st at em ent inst ead.

1.3 Creating a Database and a Sample Table