MySQL is one of the most popular database management systems. In this tutorial we will cover the steps needed to create new MySQL user and grant permissions to it in CentOS 6.4, Debian or Ubuntu platform.

Requirements

    • CentOS 6.4, Debian or Ubuntu installed on your computer/server
    • SSH access (Command line access to the server)
    • root privileges
    • Basic skills for working on a Linux environment
    • LAMP installed on the server

All operation will be executed inside a MySQL prompt with the root user:

mysql -p -u root

You will be prompted to fill in the MySQL root password.

Create a new user

We can create new MySQL user with the following command:

CREATE USER 'user'@'localhost' IDENTIFIED BY 'password';

where:

    • user – the name of the MySQL user which will be created
    • password – the password which we want to assign to that user

All MySQL commands are engin with a semicolon (;).

Grant permissions for a user

The next thing that we will have to do is to grant privileges for that user in order to be able to access the MySQL client and to work with the corresponding database/s:

GRANT ALL PRIVILEGES ON database.table TO 'user'@'localhost';

where:

    • database – the name of the MySQL database to which we grant access
    • table – the name of the database table to which we grant access

We are allowed to use the asterisk wildcard symbol (*) when we want to grant access to all databases/tables:

GRANT ALL PRIVILEGES ON database.* TO 'user'@'localhost';

or

GRANT ALL PRIVILEGES ON *.* TO 'user'@'localhost';

With the first command we grant all privileges to the MySQL user to all database tables related to the database with name "database".
In the second case access for the user is granted to all databases.

Here is a list of the MySQL privileges which are most commonly used:

    • ALL PRIVILEGES – grants all privileges to the MySQL user
    • CREATE – allows the user to create databases and tables
    • DROP - allows the user to drop databases and tables
    • DELETE - allows the user to delete rows from specific MySQL table
    • INSERT - allows the user to insert rows into specific MySQL table
    • SELECT – allows the user to read the database
    • UPDATE - allows the user to update table rows

这里插入一下 如果我们申明 ALL PRIVILEGES 那么所代表的权限将包括:

GRANT SELECT, CREATE, DROP, DELETE, INSERT, UPDATE, RELOAD, SHUTDOWN, PROCESS, FILE, REFERENCES, INDEX, ALTER, 
SHOW DATABASES, SUPER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, REPLICATION SLAVE, REPLICATION CLIENT,
CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, CREATE USER, EVENT, TRIGGER, CREATE TABLESPACE, USAGE

Here is a sample syntax where only two privileges are granted for the user:

GRANT SELECT, INSERT, DELETE ON database.* TO 'user'@'localhost';

In order for the changes to take effect and the privileges to be saved the following command should be executed at the end:

FLUSH PRIVILEGES;

Remove an existing MySQL user

A MySQL user can be deleted with the following command:

DROP USER 'user'@'localhost'

------------------------------------------------------------------分割线------------------------------------------------------------------

另外补充一点,当我们 drop 掉 user 之后我们将会删除关于该 user 的权限并且删除该账号。

MySQL 的权限是由 user + 生效地址 组成的。所以即使使用 revoke 命令撤销某些权限,也需要带上具体生效的地址比如 localhost 比如 % 代表的「所有远程地址」连接。

另外还需要提一点是, MySQL 在授予权限的时候其实是存在两组权限的。

一个是本地权限 localhost 针对本地生效。

另外一个是 % 为代表的远程访问权限。如果需要任何地方包括本地都能访问,需要将两个权限都设置上,才可以实现,这里是一个坑。

e.g.

mysql> GRANT ALL on maxwell.* to 'maxwell'@'%' identified by 'XXXXXX';
mysql> GRANT SELECT, REPLICATION CLIENT, REPLICATION SLAVE on *.* to 'maxwell'@'%'; # or for running maxwell locally: mysql> GRANT SELECT, REPLICATION CLIENT, REPLICATION SLAVE on *.* to 'maxwell'@'localhost' identified by 'XXXXXX';
mysql> GRANT ALL on maxwell.* to 'maxwell'@'localhost'; # if we want block a ip with user
mysql> GRANT USAGE ON *.* TO 'user'@'<blockIP>';

Reference:

https://cloud.tencent.com/developer/article/1056271    MySQL 包含的 29 个权限

https://www.cnblogs.com/richardzhu/p/3318595.html

https://kyup.com/tutorials/create-new-user-grant-permissions-mysql/  How to create a new user and grant permissions in MySQL

http://blog.51cto.com/gfsunny/1554627  浅析mysql主从复制中复制用户的权限管理

https://jaminzhang.github.io/mysql/the-difference-between-localhost-and-127-0-0-1-in-mysql-connection/  MySQL 连接中 localhost 和 127.0.0.1 的区别

【转】How to create a new user and grant permissions in MySQL的更多相关文章

  1. How To Create a New User and Grant Permissions in MySQL

    How to Create a New User Let’s start by making a new user within the MySQL shell: CREATE USER 'newus ...

  2. How to create/restore a slave using GTID replication in MySQL 5.6

    MySQL 5.6 is GA! Now we have new things to play with and in my personal opinion the most interesting ...

  3. [SQL] 简单新建(create)删除(drop\delete)权限(grant/revoke)修改(set\update)

    一.前言 说起来 数据库(Structured Query Language),本站写过很多类似文章. 如: Mysql创建.删除用户 phpMyAdmin 登陆需要密码 记一次裸迁 MySQL 经历 ...

  4. Can't create a new thread (errno 11) 解决办法 mysql无法连接

    问题的现象: 错误信息: ERROR 1135 (00000): Can't create a new thread (errno 11); if you are not out of availab ...

  5. 转载:Create a Flash Login System Using PHP and MySQL

    本文共两部分: 1. http://dev.tutsplus.com/tutorials/create-a-flash-login-system-using-php-and-mysql-part-1- ...

  6. [Windows Azure] Getting Started with Windows Azure SQL Database

    In this tutorial you will learn the fundamentals of Windows Azure SQL Database administration using ...

  7. LinuxCentos7下安装Mysql8.x以及密码修改

    LinuxCentos7下安装Mysql以及密码修改 引言: 之前都是用Docker或者yum自动安装,这次主要是下载压缩包解压安装,中间也有些小波折,记录如下,以供参考: 1.删除旧的MySQL 检 ...

  8. 基于Apache+php+mysql的许愿墙网站的搭建create database xyq; //创建xyq数据库

    1.准备CentOS7与CentOS5的基础配置 2.在两台虚拟机中配置yum. 3.在CentOS7中安装httpd与php与php-mysql PS:截图时已安装 CentOS7 关闭防火墙与se ...

  9. 13.1.17 CREATE TABLE Syntax

    13.1.17 CREATE TABLE Syntax 13.1.17.1 CREATE TABLE ... LIKE Syntax 13.1.17.2 CREATE TABLE ... SELECT ...

随机推荐

  1. 学JAVA第三天,JAVA第二章《JAVA数据类型》

    ---恢复内容开始--- <JAVA数据类型> 我们一般都用int类型,因为int类行一般的日常生活的数据都能满足了. 当然,想李嘉诚,马云这种有钱人,int类行就不能满足帮他记钱的了,像 ...

  2. 2018/12/21:Date类

    1.Date类 getDate()返回一个月的某一天 1-31 getDay()返回一周的某一天 getFullyear()返回四位数的年份 getMonth()返回月份 比实际情况小 1 0代表1月 ...

  3. java多线程中的三种特性

    java多线程中的三种特性 原子性(Atomicity) 原子性是指在一个操作中就是cpu不可以在中途暂停然后再调度,既不被中断操作,要不执行完成,要不就不执行. 如果一个操作时原子性的,那么多线程并 ...

  4. angular路由为空重定向到指定路由

    { path: '', redirectTo: 'home', pathMatch: 'full' }

  5. 关于Windows系统不会变慢的设想

    记录软件安装的过程,比如创建了哪些服务,哪些计划任务以及启动项等等. 然后软件安装完成后把关于软件的进程,服务,计划任务等都删掉. 然后手动创建一个脚本,用脚本代替软件的启动.比如,如果要启动sqls ...

  6. 42.Odoo产品分析 (四) – 工具板块(10) – 问卷(2)

    查看Odoo产品分析系列--目录 接上一篇Odoo产品分析 (四) – 工具板块(10) – 问卷(1) 4 页面 即问卷,点开一项查看:  可以看出,网页就是问卷本身的子目录,其中指明了该目录包括哪 ...

  7. pthread_exit在main线程中的用处

    在main线程中调用pthread_exit会起到只让main线程退出,但是保留进程资源,供其他由main创建的线程使用,直至所有线程都结束,但在其他线程中不会有这种效果 https://stacko ...

  8. Android 为TV端助力

    记录两次事情: 第一个给view添加动画效果,需要保证view是可以获取焦点的 第二个给listview,GridView设置选择器 listselector时,要保证他的子item无背景,否则选择器 ...

  9. MVC Controller return 格式分类及用法

    概述 所看到的Action都是return View();我们可以看作这个返回值用于解析一个aspx文件.而它的返回类型是ActionResult如 public ActionResult Index ...

  10. 什么是TLB?

    TLB:Translation Lookaside Buffer. 根据功能可以译为快表,直译可以翻译为旁路转换缓冲,也可以把它理解成页表缓冲.里面存放的是一些页表文件(虚拟地址到物理地址的转换表). ...