Learning Mysql


Creating and Using New Users



Download 4.24 Mb.
View original pdf
Page274/366
Date04.08.2023
Size4.24 Mb.
#61806
1   ...   270   271   272   273   274   275   276   277   ...   366
Learning MySQL
Creating and Using New Users
To create anew user, you need to have permission to do so the root user has this permission, so connect to the monitor as the root user mysql --user=root --password=the_mysql_root_password
Now create anew user called allmusic who’ll connect from the same system as the one the MySQL server is running on (
localhost
). We’ll grant this user all privileges on all tables in the music database (music) and assign the password
the_password
:
mysql> GRANT ALL ON music TO 'allmusic'@'localhost' IDENTIFIED BY 'the_password';
Query OK, 0 rows affected (0.02 sec)
This instruction creates the new user and assigns some privileges. Now, let’s discuss what we’ve done in more detail.
The
GRANT
statement gives privileges to users. Immediately following the keyword
GRANT
is the list of privileges that are given, which, in the previous case, is
ALL
(all simple
Creating and Using New Users | 299

privileges we discuss the actual privileges later. Following the privilege list is the required keyword ON, and the databases or tables that the privileges are for. In the example, we grant the privileges for music, which means the music database and all its tables. If the specified MySQL user account does not exist, it will be created automatically by the
GRANT
statement.
In the example, we’re assigning privileges to 'allmusic'@'localhost'
, which means the user has the name allmusic and can connect to the server only from the localhost
, the machine on which the database server is installed. Theresa character limit on usernames. The at symbol (
@
) implies that the user is trying to connect to the server from the specified host the MySQL user account doesn’t need to correspond to any system user account on that host, and so there is no relation to any email address. The quotes surrounding the username and the client hostname are optional you need them only if either the username or the hostname has special characters, such as hyphens (
-
) or wildcard characters. For example, you could write:
mysql> GRANT ALL ON music TO ali@localhost IDENTIFIED BY 'the_password';
Query OK, 0 rows affected (0.02 sec)
However, we recommend that you use the quotes all the time to avoid any surprises.
The optional IDENTIFIED BY 'the_password'
component sets the user’s password to
the_password
. There’s no limit on password length, but we recommend using eight or fewer characters because this avoids problems with system libraries on some platforms.
You’ll find many examples using
GRANT
throughout this chapter.
Let’s experiment with our new user. Quit the monitor using the
QUIT
command. Then run it again and connect as the user allmusic
:
$ mysql --user=allmusic --password=the_password
Note that this time, we’ve specified the MySQL user allmusic for the user parameter and passed this user’s password to the password parameter.
You should seethe mysql>
prompt again. You will now be able to use the music database by typing

Download 4.24 Mb.

Share with your friends:
1   ...   270   271   272   273   274   275   276   277   ...   366




The database is protected by copyright ©ininet.org 2024
send message

    Main page