search
HomeDatabaseMysql Tutorial(转)MySQL增多用户

(转)MySQL增多用户

Jun 07, 2016 pm 04:16 PM
crmysqlcreateOrderIncreaseMulti-useruser

(转)MySQL增加用户 创建用户: 命令: CREATE USER 'username'@'host' IDENTIFIED BY 'password'; 说明:username – 你将创建的用户名, host – 指定该用户在哪个主机上可以登陆,如果是本地用户可用localhost, 如果想让该用户可以从任意远程主机登陆,可以使用通

(转)MySQL增加用户
创建用户:
命令:
CREATE USER 'username'@'host' IDENTIFIED BY 'password';


说明:username – 你将创建的用户名, host – 指定该用户在哪个主机上可以登陆,如果是本地用户可用localhost,  如果想让该用户可以从任意远程主机登陆,可以使用通配符%. password –  该用户的登陆密码,密码可以为空,如果为空则该用户可以不需要密码登陆服务器.

CREATE USER 'dog'@'localhost' IDENTIFIED BY '123456'; 
CREATE USER 'pig'@'192.168.1.101_' IDENDIFIED BY '123456'; 
CREATE USER 'pig'@'%' IDENTIFIED BY '123456'; 
CREATE USER 'pig'@'%' IDENTIFIED BY ''; 
CREATE USER 'pig'@'%';



授权:
命令:
GRANT privileges ON databasename.tablename TO 'username'@'host'

说明: privileges – 用户的操作权限,如SELECT , INSERT , UPDATE  等(详细列表见该文最后面).如果要授予所的权限则使用ALL.;databasename –  数据库名,tablename-表名,如果要授予该用户对所有数据库和表的相应操作权限则可用*表示, 如*.*.
例子:

GRANT SELECT, INSERT ON test.user TO 'pig'@'%'; 
GRANT ALL ON *.* TO 'pig'@'%';


注意:用以上命令授权的用户不能给其它用户授权,如果想让该用户可以授权,用以下命令:
GRANT privileges ON databasename.tablename TO 'username'@'host' WITH GRANT OPTION;
设置与更改用户密码
命令:
SET PASSWORD FOR 'username'@'host' = PASSWORD('newpassword');

如果是当前登陆用户用
SET PASSWORD = PASSWORD("newpassword");

撤销用户权限
命令:
REVOKE privilege ON databasename.tablename FROM 'username'@'host';

说明: privilege, databasename, tablename – 同授权部分.
例子
REVOKE SELECT ON *.* FROM 'pig'@'%';

注意: 假如你在给用户’pig’@'%’授权的时候是这样的(或类似的):GRANT SELECT ON test.user TO  ‘pig’@'%’, 则在使用REVOKE SELECT ON *.* FROM  ‘pig’@'%’;命令并不能撤销该用户对test数据库中user表的SELECT 操作.相反,如果授权使用的是GRANT SELECT ON  *.* TO ‘pig’@'%’;则REVOKE SELECT ON test.user FROM  ‘pig’@'%’;命令也不能撤销该用户对test数据库中user表的Select 权限.
具体信息可以用命令SHOW GRANTS FOR ‘pig’@'%’; 查看.
删除用户
命令:
DROP USER ‘username’@'host’;

一个典型的数据库建表, 建用户过程:
##创建用于localhost连接的用户并指定密码 
mysql> create user 'pcom'@'localhost' identified by 'aaa7B2249'; 
Query OK, 0 rows affected (0.00 sec) 
     
##创建数据库 
mysql> create database pcom default character set utf8 collate utf8_bin; 
Query OK, 1 row affected (0.00 sec) 
     
##给本地用户授权, 这里不需要指定密码 
mysql> grant all on pcom.* to 'pcom'@'localhost'; 
Query OK, 0 rows affected (0.00 sec) 
     
##给其他IP地址下的用户授权, 注意: 这里必须指定密码, 否则就可以无密码访问 
mysql> grant all on pcom.* to 'pcom'@'192.168.0.0/255.255.0.0' identified by 'aaa7B2249'; 
Query OK, 0 rows affected (0.00 sec) 
     
##同理 
mysql> grant all on pcom.* to 'pcom'@'172.20.0.0/255.255.0.0' identified by 'aaa7B2249'; 
Query OK, 0 rows affected (0.00 sec) 
     
Done!

##刷新系统权限表。 
mysql> flush privileges;


附表:在MySQL中的操作权限
ALTER
## Allows use of ALTER TABLE. 
    
ALTER ROUTINE 
## Alters or drops stored routines. 
    
CREATE
## Allows use of CREATE TABLE. 
    
CREATE ROUTINE 
## Creates stored routines. 
    
CREATE TEMPORARY TABLE
## Allows use of CREATE TEMPORARY TABLE. 
    
CREATE USER
## Allows use of CREATE USER, DROP USER, RENAME USER, and REVOKE ALL  PRIVILEGES. 
    
CREATE VIEW
## Allows use of CREATE VIEW. 
    
DELETE
## Allows use of DELETE. 
    
DROP
## Allows use of DROP TABLE. 
    
EXECUTE
## Allows the user to run stored routines. 
    
FILE 
## Allows use of SELECT… INTO OUTFILE and LOAD DATA INFILE. 
    
INDEX
## Allows use of CREATE INDEX and DROP INDEX. 
    
INSERT
## Allows use of INSERT. 
    
LOCK TABLES 
## Allows use of LOCK TABLES on tables for which the user also has SELECT privileges. 
    
PROCESS 
## Allows use of SHOW FULL PROCESSLIST. 
    
RELOAD 
## Allows use of FLUSH. 
    
REPLICATION 
## Allows the user to ask where slave or master 
    
CLIENT 
## servers are. 
    
REPLICATION SLAVE 
## Needed for replication slaves. 
    
SELECT
## Allows use of SELECT. 
    
SHOW DATABASES 
## Allows use of SHOW DATABASES. 
    
SHOW VIEW
## Allows use of SHOW CREATE VIEW. 
    
SHUTDOWN 
## Allows use of mysqladmin shutdown. 
    
SUPER 
## Allows use of CHANGE MASTER, KILL, PURGE MASTER LOGS, and SET GLOBAL SQL statements. Allows mysqladmin debug command. Allows one extra connection to be made if maximum connections are reached. 
    
UPDATE
## Allows use of UPDATE. 
    
USAGE 
## Allows connection without any specific privileges.


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
How do you handle database upgrades in MySQL?How do you handle database upgrades in MySQL?Apr 30, 2025 am 12:28 AM

The steps for upgrading MySQL database include: 1. Backup the database, 2. Stop the current MySQL service, 3. Install the new version of MySQL, 4. Start the new version of MySQL service, 5. Recover the database. Compatibility issues are required during the upgrade process, and advanced tools such as PerconaToolkit can be used for testing and optimization.

What are the different backup strategies you can use for MySQL?What are the different backup strategies you can use for MySQL?Apr 30, 2025 am 12:28 AM

MySQL backup policies include logical backup, physical backup, incremental backup, replication-based backup, and cloud backup. 1. Logical backup uses mysqldump to export database structure and data, which is suitable for small databases and version migrations. 2. Physical backups are fast and comprehensive by copying data files, but require database consistency. 3. Incremental backup uses binary logging to record changes, which is suitable for large databases. 4. Replication-based backup reduces the impact on the production system by backing up from the server. 5. Cloud backups such as AmazonRDS provide automation solutions, but costs and control need to be considered. When selecting a policy, database size, downtime tolerance, recovery time, and recovery point goals should be considered.

What is MySQL clustering?What is MySQL clustering?Apr 30, 2025 am 12:28 AM

MySQLclusteringenhancesdatabaserobustnessandscalabilitybydistributingdataacrossmultiplenodes.ItusestheNDBenginefordatareplicationandfaulttolerance,ensuringhighavailability.Setupinvolvesconfiguringmanagement,data,andSQLnodes,withcarefulmonitoringandpe

How do you optimize database schema design for performance in MySQL?How do you optimize database schema design for performance in MySQL?Apr 30, 2025 am 12:27 AM

Optimizing database schema design in MySQL can improve performance through the following steps: 1. Index optimization: Create indexes on common query columns, balancing the overhead of query and inserting updates. 2. Table structure optimization: Reduce data redundancy through normalization or anti-normalization and improve access efficiency. 3. Data type selection: Use appropriate data types, such as INT instead of VARCHAR, to reduce storage space. 4. Partitioning and sub-table: For large data volumes, use partitioning and sub-table to disperse data to improve query and maintenance efficiency.

How can you optimize MySQL performance?How can you optimize MySQL performance?Apr 30, 2025 am 12:26 AM

TooptimizeMySQLperformance,followthesesteps:1)Implementproperindexingtospeedupqueries,2)UseEXPLAINtoanalyzeandoptimizequeryperformance,3)Adjustserverconfigurationsettingslikeinnodb_buffer_pool_sizeandmax_connections,4)Usepartitioningforlargetablestoi

How to use MySQL functions for data processing and calculationHow to use MySQL functions for data processing and calculationApr 29, 2025 pm 04:21 PM

MySQL functions can be used for data processing and calculation. 1. Basic usage includes string processing, date calculation and mathematical operations. 2. Advanced usage involves combining multiple functions to implement complex operations. 3. Performance optimization requires avoiding the use of functions in the WHERE clause and using GROUPBY and temporary tables.

An efficient way to batch insert data in MySQLAn efficient way to batch insert data in MySQLApr 29, 2025 pm 04:18 PM

Efficient methods for batch inserting data in MySQL include: 1. Using INSERTINTO...VALUES syntax, 2. Using LOADDATAINFILE command, 3. Using transaction processing, 4. Adjust batch size, 5. Disable indexing, 6. Using INSERTIGNORE or INSERT...ONDUPLICATEKEYUPDATE, these methods can significantly improve database operation efficiency.

Steps to add and delete fields to MySQL tablesSteps to add and delete fields to MySQL tablesApr 29, 2025 pm 04:15 PM

In MySQL, add fields using ALTERTABLEtable_nameADDCOLUMNnew_columnVARCHAR(255)AFTERexisting_column, delete fields using ALTERTABLEtable_nameDROPCOLUMNcolumn_to_drop. When adding fields, you need to specify a location to optimize query performance and data structure; before deleting fields, you need to confirm that the operation is irreversible; modifying table structure using online DDL, backup data, test environment, and low-load time periods is performance optimization and best practice.

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

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment