1 like 0 dislike
608 views
in Computers & Internet by

1 Answer

1 like 0 dislike
by
 
Best answer

The GRANT statement gives you the power to enable all, or a selection of, user privileges for a database or for a specific table. In order to GRANT privileges, first you need to log in to the MySQL server using an SSH client. Once you’ve opened an SSH console, please connect to the MySQL server using the following command: 
 

An example on how to log in to a MySQL server using SSH

$mysqladmin -u db_user password  db_passwd

Note: that db_user and db_passwd are your database username and database password


Once logged on to the server, you will see MySQL's SQL command line. Now you can add specific privileges to a user. Let's for example grant SELECT privileges to a user db_user for database 'db_base'. Here is the SQL query: 

 

An example of how to add SELECT privileges to a user using GRANT

GRANT SELECT ON db_base.* TO db_user@'localhost' IDENTIFIED BY 'db_passwd';

If SELECT is not enough for our user we can add more privileges using a query similar to the one below: 



An example of how to add a selection of privileges to a user using GRANT

GRANT SELECT, INSERT, DELETE ON db_base.* TO db_user@'localhost' IDENTIFIED BY 'db_passwd';

If you want to GRANT ALL the privileges to a user then use this query:

An example of how to Grant Privileges in MySQL

GRANT ALL PRIVILEGES ON db_base.* TO db_user @'%' IDENTIFIED BY 'db_passwd';

As you see in the latest example we use '%' instead of localhost, which means that our user can use all the privileges from every host. 

Sometimes you need to grant privileges to a user for a specific table. To specify the table, replace '*' in 'db_base.*' with your table's name. 


An example of how to Grant Privileges in MySQL

GRANT ALL PRIVILEGES ON db_base.phonebook TO db_user @'%' IDENTIFIED BY 'db_passwd';

Once you have given the desired privileges for your user, you will need to FLUSH privileges in order to complete the setup and to make the new settings work. To do so, run this command within the SQL command prompt: 



An example of how to FLUSH privileges

FLUSH PRIVILEGES;

 

Related questions

1 like 0 dislike
1 answer 3 views
1 like 0 dislike
1 answer 55 views
0 like 0 dislike
1 answer 570 views
0 like 0 dislike
1 answer 168 views
0 like 0 dislike
3 answers 743 views
0 like 0 dislike
1 answer 134 views
0 like 0 dislike
1 answer 103 views
0 like 0 dislike
1 answer 800 views
0 like 0 dislike
1 answer 503 views
...