Table of Contents
先说一说mysql的安装:
一、用户的增删修改:
二、用户权限:
Grant:
Revoke:
Home Database Mysql Tutorial mysql用户与权限管理笔记_MySQL

mysql用户与权限管理笔记_MySQL

Jun 01, 2016 pm 01:26 PM
hibernate mysql ubuntu user

bitsCN.com

今天想使用一下李刚那本书上的hibernate的Demo,试出了点问题,过程中就发现mysql的用户管理和权限管理上也有点东西要注意,所以顺便就写一下mysql用户管理和权限管理的笔记。

先说一说mysql的安装:

我们在ubuntu下先安装mysql:

sudo apt-get install mysql-server

安装好了以后呢,我们先用root身份登录到数据库中,我记得安装过程的最后一步里面,如果你在可视化界面下用控制台的话,它好像是会要求你输入root的密码的。不过如果没有也没关系,那mysql就默认root是没有密码的了。

我们可以直接用root登录到数据库中:

$mysql -u root

如果你不使用-u root选项的话,mysql就会默认你是在当前用户的权限下登录的(这个真没啥好说的了)。

进去之后,修改权限和密码:

mysql> GRANT ALL PRIVILEGES ON *.* TO root@localhost IDENTIFIED BY "123456";

那个privileges写不写无所谓,新版本的mysql直接写all就行了,意思是全部操作的权限都给你指定的那个用户

*.*是什么意思呢,它的原型是’db_name.table_name’,所以在此的意思是所有数据库的所有表。

root@localhost表示的就是本地登录的root账户,identified by后跟的就是你要设置的密码。

下次你再想用root身份登录mysql就应该在控制台输入:

$mysql -u root -p

然后控制台会弹出”Enter password:”叫你输入密码。

基本的root设置就是这样了。

如果你希望别人可以通过IP地址访问你的数据库,那么就改一下/etc/mysql/my.cnf中的配置即可:

$sudo gedit /etc/mysql/my.cnf

把bind-address=127.0.0.1中的地址改成你机子分配到的IP地址即可。

想知道自己的网卡信息,用ifconfig命令就可以了,不多说了。

一、用户的增删修改:

用户操作,一般都要以root身份登录到mysql中进行(不一定是root,但凡是具有CREATE USER相关权限和GRANT OPTION相关权限的账户均可)。

新建用户的语法如下:

CREATE USER user [IDENTIFIED BY [PASSWORD] 'password']    [, user [IDENTIFIED BY [PASSWORD] 'password']] ...
Copy after login

最基本的,我想新建一个本地账户,名叫nero,密码是123456(先用root登录到mysql):

create user ‘nero’@‘localhost’ identified by ‘123456’
Copy after login

删除用户的语法则是:

DROP USER user [, user] ...
Copy after login

比如我要删除刚刚新建的用户:

drop user ‘nero’@‘localhost’
Copy after login

更改用户密码:

方法一:

mysqladmin -u 用户名 -p password 新密码

比如说前面新建的nero账户我想把它的密码改成123:

$ mysqladmin -u nero -p password '123'
Copy after login

然后会通过’Enter password:’提示你输入旧密码以确认,确认以后则完成修改,下次你再用nero这个账户登录的时候,需要输入的密码就是123。

方法二:

当然你也可以通过SET PASSWORD语法来更改密码,当然,首先你当前登录的账户要有这个权限(比如root账户):

SET PASSWORD [FOR user] =    {        PASSWORD('some password')      | OLD_PASSWORD('some password')      | 'encrypted password'    }
Copy after login

比如说,更改nero这个本地账户的密码:

set password for ‘nero’@’localhost’ = password(‘123’)
Copy after login

二、用户权限:

其实创建用户就是为了管理权限,我们不想要每个人都能对这个数据库做改动,有些账户可能我只希望你能查看(select),但是没法修改(增删改等)。这个时候我们就可以创建一些权限比较少的用户。

分配权限我们可以用GRANT,而收回(取消)权限则可以用REVOKE

Grant:

GRANT    priv_type [(column_list)]      [, priv_type [(column_list)]] ...    ON [object_type] priv_level    TO user [IDENTIFIED BY [PASSWORD] 'password']        [, user [IDENTIFIED BY [PASSWORD] 'password']] ...    [REQUIRE {NONE | ssl_option [[AND] ssl_option] ...}]    [WITH with_option ...]object_type:    TABLE  | FUNCTION  | PROCEDUREpriv_level:    *  | *.*  | db_name.*  | db_name.tbl_name  | tbl_name  | db_name.routine_namessl_option:    SSL  | X509  | CIPHER 'cipher'  | ISSUER 'issuer'  | SUBJECT 'subject'with_option:    GRANT OPTION  | MAX_QUERIES_PER_HOUR count  | MAX_UPDATES_PER_HOUR count  | MAX_CONNECTIONS_PER_HOUR count  | MAX_USER_CONNECTIONS count
Copy after login
View Code

比如说前面我创建的账户nero,我希望他获取所有的权限:

mysql>GRANT ALL ON *.* TO 'nero’ '@'localhost' identified by ‘123456’ ;
Copy after login

localhost表明nero这个账户从本地登录的时候可以获取全部操作权限。

一旦一个账户拥有所有权限,那么他对数据库就有完全的操作权,包括用户创建、权限修改等等,就不仅是简单的数据库、表操作了。

那如果我希望这个账户从任何一台机远程登录都能够有全部权限,则可以修改为:

mysql>GRANT ALL ON *.*  TO nero@’%’ IDENTIFIED BY "123456";
Copy after login

单引号双引号都是没关系的。

如果我要对某一个特定的账户一些指定的权限,那么输入如下:

mysql>GRANT SELECT, INSERT ON db_name.table_name TO 'someuser'@'somehost';
Copy after login

甚至你也可以更详细地指定到列的权限,比如:

GRANT SELECT (col1), INSERT (col1,col2) ON mydb.mytbl TO 'someuser'@'somehost';
Copy after login

Revoke:

Revoke语法规则如下:

REVOKE

    priv_type [(column_list)]

      [, priv_type [(column_list)]] ...

    ON [object_type] priv_level

    FROM user [, user] ...

撤销所有权限则是:

REVOKE ALL PRIVILEGES, GRANT OPTION

    FROM user [, user] ...

比如我要删除本地账户nero对任何数据库中的任何表的insert权限:

REVOKE INSERT ON *.* FROM ‘nero’@'localhost';
Copy after login

其他的都大同小异,不作赘述,具体可以翻阅帮助文档。

 

题外话:

前阵子朋友看我记笔记,说写网络笔记不用写得太详细吧,记个大概即可,自己能看懂就行。

其实对这样的说法很有感触。像我们初学一门知识的时候,没人指导,没人告诉我们那些看似显而易见但是却晦涩难懂的东西,比如数学证明的一些“我们不妨做如下猜测”、“明显地”之类的话,可能老师自己也不知道为什么,毕竟做老师也只是一份职业,谋一口饭吃,不能奢求太多。但是初学者却确实要因此而绕很多弯路,并且起步很慢,很多实用性强,以后工作里可能经常需要用到的专业知识,老师也不会特地点出来告诉你。

每次想起这些都觉得很痛心。其实我们每次网上搜索一些关键词,哪次不是希望搜到的结果里面,就有一种切实可行的解决方案,很多时候搜索这些词汇的人是非常迫切地希望问题得到解决的(当然也不乏一些投机行为)。而这些人群中,很大一部分就是初学者。

英语不是我们的母语,学习外文知识总是更慢更没效率的,为什么国外的小屁孩八九岁就能做应用,因为英语就是他们的母语,他们看得懂帮助文档,比看市面上的那些“21天精通XXX”的书好上千百倍(这些书就别出版来误人子弟了)。当我们认真去看那些市面上写得比较优秀的书,比如说李刚写的J2EE相关的书,哪些不是从帮助文档里直接拿来然后改改做例子的?但是他能梳理,他有这能力,所以这钱就应该他赚。

综上,我只想说:第一,好好学英语;第二,对新手好点,宽容点。

 

 

最后给出常用权限列表:

Privilege

Meaning

ALL [PRIVILEGES]

Grant all privileges at specified access level except GRANT OPTION

ALTER

Enable use of ALTER TABLE

ALTER ROUTINE

Enable stored routines to be altered or dropped

CREATE

Enable database and table creation

CREATE ROUTINE

Enable stored routine creation

CREATE TABLESPACE

Enable tablespaces and log file groups to be created, altered, or dropped

CREATE TEMPORARY TABLES

Enable use of CREATE TEMPORARY TABLE

CREATE USER

Enable use of CREATE USER, DROP USER, RENAME USER, and REVOKE ALL PRIVILEGES

CREATE VIEW

Enable views to be created or altered

DELETE

Enable use of DELETE

DROP

Enable databases, tables, and views to be dropped

EVENT

Enable use of events for the Event Scheduler

EXECUTE

Enable the user to execute stored routines

FILE

Enable the user to cause the server to read or write files

GRANT OPTION

Enable privileges to be granted to or removed from other accounts

INDEX

Enable indexes to be created or dropped

INSERT

Enable use of INSERT

LOCK TABLES

Enable use of LOCK TABLES on tables for which you have the SELECT privilege

PROCESS

Enable the user to see all processes with SHOW PROCESSLIST

REFERENCES

Not implemented

RELOAD

Enable use of FLUSH operations

REPLICATION CLIENT

Enable the user to ask where master or slave servers are

REPLICATION SLAVE

Enable replication slaves to read binary log events from the master

SELECT

Enable use of SELECT

SHOW DATABASES

Enable SHOW DATABASES to show all databases

SHOW VIEW

Enable use of SHOW CREATE VIEW

SHUTDOWN

Enable use of mysqladmin shutdown

SUPER

Enable use of other adminstrative operations such as CHANGE MASTER TO, KILL,PURGE BINARY LOGS, SET GLOBAL, and mysqladmin debug command

TRIGGER

Enable triggers to be created or dropped

UPDATE

Enable use of UPDATE

USAGE

Synonym for “no privileges”

 

 

 

bitsCN.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

How to install mysql in centos7 How to install mysql in centos7 Apr 14, 2025 pm 08:30 PM

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

Centos install mysql Centos install mysql Apr 14, 2025 pm 08:09 PM

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

See all articles