bitsCN.com
MySQL中添加用户的两种方法
下面的例子显示出如何使用MySQL客户安装新用户。这些例子假定权限已缺省被安装。这意味着为了改变,你必须在MySQL正在运行同一台机器上,你必须作为MySQL root用户连接,并且root用户必须对MySQL数据库有insert权限和reload管理权限。另外,如果你改变了root用户口令,你必须如下的MySQL命令指定它。
你可以通过发出GRANT语句增加新用户:
shell> mysql --user=root mysqlmysql> GRANT ALL PRIVILEGES ON *.* TO monty@localhostIDENTIFIED BY 'something' WITH GRANT OPTION;mysql> GRANT ALL PRIVILEGES ON *.* TO monty@"%"IDENTIFIED BY 'something' WITH GRANT OPTION;mysql> GRANT RELOAD,PROCESS ON *.* TO admin@localhost;mysql> GRANT USAGE ON *.* TO dummy@localhost;
这些GRANT语句安装3个新用户:
monty:可以从任何地方连接服务器的一个完全的超级用户,但是必须使用一个口令('something'做这个。注意,我们必须对monty@localhost和monty@"%"发出GRANT语句。如果我们增加localhost条目,对localhost的匿名用户条目在我们从本地主机连接接时由mysql_install_db创建的条目将优先考虑,因为它有更特定的Host字段值,所以以user表排列顺序看更早到来。
admin:可以从localhost没有一个口令进行连接并且被授予reload和process管理权限的用户。这允许用户执行mysqladmin reload、mysqladmin refresh和mysqladmin flush-*命令,还有mysqladmin processlist。没有授予数据库有关的权限。他们能在以后通过发出另一个GRANT语句授权。
dummy:可以不用一个口令连接的一个用户,但是只能从本地主机。全局权限被设置为'N'--USAGE权限类型允许你无需权限就可设置一个用户。它假定你将在以后授予数据库相关的权限。
你也可以直接通过发出INSERT语句增加同样的用户存取信息,然后告诉服务器再次装入授权表:
shell> mysql --user=root mysqlmysql> INSERT INTO user VALUES('localhost','monty',PASSWORD('something'),'Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y')mysql> INSERT INTO user VALUES('%','monty',PASSWORD('something'),'Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y','Y')mysql> INSERT INTO user SET Host='localhost',User='admin',Reload_priv='Y', Process_priv='Y';mysql> INSERT INTO user (Host,User,Password)VALUES('localhost','dummy','');mysql> FLUSH PRIVILEGES;
取决于你的MySQL版本,对上述,你可能必须使用一个不同数目'Y'值(在3.22.11以前的版本有更少的权限列)。对admin用户,只用在3.22.11开始的版本具有的更加可读的INSERT扩充的语法。
注意,为了设置一个超级用户,你只需创造一个user表条目,其权限字段设为'Y'。不需要db或host表的条目。
在user表中的权限列不是由最后一个INSERT语句明确设置的(对dummy用户),因此那些列被赋予缺省值'N'。这是GRANT USAGE做的同样的事情。
下列例子增加一个用户custom,他能从主机 localhost、server.domain和whitehouse.gov连接。他只想要从localhost存取bankaccount数据库,从whitehouse.gov存取expenses数据库和从所有3台主机存取customer数据库。他想要从所有3台主机上使用口令stupid。
为了使用GRANT语句设置个用户的权限,运行这些命令:
shell> mysql --user=rootmysql mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROPON bankaccount.*TO custom@localhostIDENTIFIED BY 'stupid';mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROPON expenses.*TO custom@whitehouse.govIDENTIFIED BY 'stupid';mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROPON customer.*TO custom@'%'IDENTIFIED BY 'stupid';
通过直接修改授权表设置用户权限,运行这些命令(注意,在结束时FLUSH PRIVILEGES):
shell> mysql --user=rootmysql mysql> INSERT INTO user (Host,User,Password)VALUES('localhost','custom',PASSWORD('stupid'));mysql> INSERT INTO user (Host,User,Password)VALUES('server.domain','custom',PASSWORD('stupid'));mysql> INSERT INTO user (Host,User,Password)VALUES('whitehouse.gov','custom',PASSWORD('stupid'));mysql> INSERT INTO db(Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv)VALUES('localhost','bankaccount','custom','Y','Y','Y','Y','Y','Y');mysql> INSERT INTO db(Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv)VALUES('whitehouse.gov','expenses','custom','Y','Y','Y','Y','Y','Y');mysql> INSERT INTO db(Host,Db,User,Select_priv,Insert_priv,Update_priv,Delete_priv,Create_priv,Drop_priv)VALUES('%','customer','custom','Y','Y','Y','Y','Y','Y');mysql> FLUSH PRIVILEGES;
头3个INSERT语句增加user表条目,允许用户 custom用给定口令从不同的主机进行连接,但是没有授予任何许可(所有权限被设置为缺省值'N')。后3个INSERT语句增加db表条目,授予 custom以bankaccount、expenses和customer数据库权限,但是只能在从正确的主机存取时。通常,在授权表直接被修改时,服务器必须被告知再次装入他们(用FLUSH PRIVILEGES)以便使权限修改生效。 如果你想要给特定的用户从一个给定的域上的任何机器上存取权限,你可以发出一个如下的GRANT语句:
mysql> GRANT ...ON *.*TO myusername@"%.mydomainname.com"IDENTIFIED BY 'mypassword';
为了通过直接修改授权表做同样的事情,这样做:
mysql> INSERT INTO user VALUES ('%.mydomainname.com', 'myusername',PASSWORD('mypassword'),...);mysql> FLUSH PRIVILEGES;
你也可以使用xmysqladmin、mysql_webadmin甚至xmysql在授权表中插入、改变和更新值。你可以在MySQL的Contrib目录找到这些实用程序。
通过上文中在MySQL中添加用户两种方法的学习,相信现在在MySQL中添加用户的问题大家已经都学会了,很高兴与大家分享这些小经验,希望能够对大家有所帮助。
bitsCN.com
MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

MySQloffersechar, Varchar, text, Anddenumforstringdata.usecharforfixed-Lengthstrings, VarcharerForvariable-Length, text forlarger text, AndenumforenforcingdataAntegritywithaetofvalues.

Optimizing MySQLBLOB requests can be done through the following strategies: 1. Reduce the frequency of BLOB query, use independent requests or delay loading; 2. Select the appropriate BLOB type (such as TINYBLOB); 3. Separate the BLOB data into separate tables; 4. Compress the BLOB data at the application layer; 5. Index the BLOB metadata. These methods can effectively improve performance by combining monitoring, caching and data sharding in actual applications.

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
