13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

228 Chapter 9 Creating Your <strong>Web</strong> Database<br />

Now you can set up a regular user with no privileges:<br />

mysql> grant usage<br />

-> on books.*<br />

-> to sally identified by ‘magic123’;<br />

After talking to Sally, you know a bit more about what she wants to do, so you can give<br />

her the appropriate privileges:<br />

mysql> grant select, insert, update, delete, index, alter, create, drop<br />

-> on books.*<br />

-> to sally;<br />

Note that you don’t need to specify Sally’s password to give her privileges.<br />

If you decide that Sally has been up to something in the database, you might decide<br />

to reduce her privileges:<br />

mysql> revoke alter, create, drop<br />

-> on books.*<br />

-> from sally;<br />

And later, when she doesn’t need to use the database any more, you can revoke her privileges<br />

altogether:<br />

mysql> revoke all<br />

-> on books.*<br />

-> from sally;<br />

Setting Up a User for the <strong>Web</strong><br />

You need to set up a user for your <strong>PHP</strong> scripts to connect to <strong>MySQL</strong>. Again, you can<br />

apply the privilege of least principle:What should the scripts be able to do?<br />

In most cases, they only need to run SELECT, INSERT, DELETE, <strong>and</strong> UPDATE queries.<br />

You can set up these privileges as follows:<br />

mysql> grant select, insert, delete, update<br />

-> on books.*<br />

-> to bookorama identified by ‘bookorama123’;<br />

Obviously, for security reasons, you should choose a better password than the one shown<br />

here.<br />

If you use a web hosting service, you usually get access to the other user-type privileges<br />

on a database the service creates for you. It typically gives you the same user_name<br />

<strong>and</strong> password for comm<strong>and</strong>-line use (setting up tables <strong>and</strong> so on) <strong>and</strong> for web script<br />

connections (querying the database). Using the same username <strong>and</strong> password for both is<br />

marginally less secure.You can set up a user with this level of privilege as follows:<br />

mysql> grant select, insert, update, delete, index, alter, create, drop<br />

-> on books.*<br />

-> to bookorama identified by ‘bookorama123’;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!