13.09.2016 Views

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Introducing <strong>MySQL</strong>’s Privilege System<br />

223<br />

Setting Up Users <strong>and</strong> Privileges<br />

A <strong>MySQL</strong> system can have many users.The root user should generally be used for<br />

administration purposes only, for security reasons. For each user who needs to use the<br />

system, you need to set up an account <strong>and</strong> password.They do not need to be the same as<br />

usernames <strong>and</strong> passwords outside <strong>MySQL</strong> (for example, Unix or NT usernames <strong>and</strong><br />

passwords).The same principle applies to root. Having different passwords for the system<br />

<strong>and</strong> for <strong>MySQL</strong> is a good idea, especially when it comes to the root password.<br />

Setting up passwords for users isn’t compulsory, but we strongly recommend that you<br />

set up passwords for all the users you create. For the purposes of setting up a web database,<br />

it’s a good idea to set up at least one user per web application.You might ask,“Why<br />

would I want to do this?”The answer lies in privileges.<br />

Introducing <strong>MySQL</strong>’s Privilege System<br />

One of the best features of <strong>MySQL</strong> is that it supports a sophisticated privilege system. A<br />

privilege is the right to perform a particular action on a particular object <strong>and</strong> is associated<br />

with a particular user.The concept is similar to file permissions.When you create a user<br />

within <strong>MySQL</strong>, you grant her a set of privileges to specify what she can <strong>and</strong> cannot do<br />

within the system.<br />

Principle of Least Privilege<br />

The principle of least privilege can be used to improve the security of any computer<br />

system. It’s a basic but important principle that is often overlooked.The principle is as<br />

follows:<br />

A user (or process) should have the lowest level of privilege required to perform his<br />

assigned task.<br />

It applies in <strong>MySQL</strong> as it does elsewhere. For example, to run queries from the <strong>Web</strong>, a<br />

user does not need all the privileges to which root has access.You should therefore create<br />

another user who has only the necessary privileges to access the database you just<br />

created.<br />

User Setup: The GRANT Comm<strong>and</strong><br />

The GRANT <strong>and</strong> REVOKE comm<strong>and</strong>s enable you to give rights to <strong>and</strong> take them away from<br />

<strong>MySQL</strong> users at these four levels of privilege:<br />

n Global<br />

n Database<br />

n Table<br />

n Column

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

Saved successfully!

Ooh no, something went wrong!