search
HomeDatabaseMysql Tutorialmysql用户与权限管理笔记_MySQL

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']] ...

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

create user ‘nero’@‘localhost’ identified by ‘123456’

删除用户的语法则是:

DROP USER user [, user] ...

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

drop user ‘nero’@‘localhost’

更改用户密码:

方法一:

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

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

$ mysqladmin -u nero -p password '123'

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

方法二:

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

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

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

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

二、用户权限:

其实创建用户就是为了管理权限,我们不想要每个人都能对这个数据库做改动,有些账户可能我只希望你能查看(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
View Code

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

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

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

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

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

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

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

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

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

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

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

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';

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

 

题外话:

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

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

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

英语不是我们的母语,学习外文知识总是更慢更没效率的,为什么国外的小屁孩八九岁就能做应用,因为英语就是他们的母语,他们看得懂帮助文档,比看市面上的那些“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
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
Explain the InnoDB Buffer Pool and its importance for performance.Explain the InnoDB Buffer Pool and its importance for performance.Apr 19, 2025 am 12:24 AM

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

MySQL vs. Other Programming Languages: A ComparisonMySQL vs. Other Programming Languages: A ComparisonApr 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.

Learning MySQL: A Step-by-Step Guide for New UsersLearning MySQL: A Step-by-Step Guide for New UsersApr 19, 2025 am 12:19 AM

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL: Essential Skills for Beginners to MasterMySQL: Essential Skills for Beginners to MasterApr 18, 2025 am 12:24 AM

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL: Structured Data and Relational DatabasesMySQL: Structured Data and Relational DatabasesApr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL: Key Features and Capabilities ExplainedMySQL: Key Features and Capabilities ExplainedApr 18, 2025 am 12:17 AM

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

The Purpose of SQL: Interacting with MySQL DatabasesThe Purpose of SQL: Interacting with MySQL DatabasesApr 18, 2025 am 12:12 AM

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

MySQL for Beginners: Getting Started with Database ManagementMySQL for Beginners: Getting Started with Database ManagementApr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

See all articles

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

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.