This is quite a long command. Let’s walk through the pieces to see what it’s doing.
The first section defines the privileges the user account has on various database(s). This statement
allows the user account to query the database data (the SELECT privilege), insert new data records,
delete existing data records, and update existing data records.
The pytest.* entry defines the database and tables that the privileges apply to. This is specified in
the following format:
database.table
As you can see from this example, you’re allowed to use wildcard characters when specifying the
database and tables. This format applies the specified privileges to all the tables contained in the
database named pytest.
Finally, you specify the user account(s) that the privileges apply to (test, in this example). The
MySQL server also allows you to restrict the assigned privileges to apply only when the user account
connects from a specific location. You restrict the test user account to only log in from the
localhost location, which means only from scripts running on your Raspberry Pi system.
The neat thing about the GRANT statement is that if the user account doesn’t exist, it creates it.
IDENTIFIED BY allows you to set the password for the new user account.
When you’re done working with the MySQL server, use the exit command to get back to the
standard Linux command prompt:
mysql> exit;
pi@raspberrypi ~ $
You can test the new user account directly from the mysql program, like this:
Click here to view code image
pi@raspberrypi ~ $ mysql pytest -u test -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 76
Server version: 5.5.31-0+wheezy1 (Debian)
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
The first parameter specifies the default database to use (pytest), and as you’ve already seen, the -
u parameter defines the login user account, and -p to queries for the password (remember, you set
that to “test” earlier). After you enter the password assigned to the test user account, you should be
connected to the server.
Now that you have a database and a user account, you’re ready to create some tables for the data.
Creating Tables
The MySQL server is a relational database. In a relational database, data is organized by data fields,