您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

【转】MySQL中GRANT权限的时分特殊字符转义

2024/11/13 18:29:02发布15次查看
【转】mysql中grant权限的时候特殊字符转义 碰到grant的问题,发现这篇文章,做个转载: ? 原文地址:http://blog.useasp.net/archive/2013/05/21/mysql-special-character-escape-method-when-grant-privilages-to-user.aspx ? 在mysql中使用了下划线的数据
【转】mysql中grant权限的时候特殊字符转义
碰到grant的问题,发现这篇文章,做个转载:
?
原文地址:http://blog.useasp.net/archive/2013/05/21/mysql-special-character-escape-method-when-grant-privilages-to-user.aspx
?
在mysql中使用了下划线的数据库名,今天在grant的时候,死活无法为用户赋予权限,一直报错。查看了官方的文档,文档中说:如果使用下划线的数据库在使用grant的时候,需要对数据库的下划线进行转义。
官方文档的全文如下:
12345678 the “_” and “%” wildcards are permitted when specifyingdatabase names in grant statements that grant privileges atthe global or database levels. this means, for example, thatif you want to use a “_” character as part of a database name,you should specify it as “\_” in the grant statement, toprevent the user from being able to access additionaldatabases matching the wildcard pattern; for example,grant ... on `foo\_bar`.* to ....
grant语句用于在全局层级或数据库层级赋予权限。当在grant语句中指定数据库名称时,允许使用‘_’和‘%’通配符。这意味着,如果您想要使用‘_’字符作为一个数据库名称的一部分,您应该在grant语句中指定它为‘\_’,以防止用户可以访问其它符合此通配符格式的数据库;例如,grant ... on `foo\_bar`.* to ...。
说明非常明确,就是在碰到下划线和百分号的时候,为了防止grant的时候权限漂移,需要明确的对这两个通配符进行转义,转义符为\,因此有了如下的语句:
1 grant all on 'db\_test'.*?to 'test_user';
?恩,看上去是如此的完美,但,非常遗憾的是,mysql接受到这个语句直接报错:
12 error 1064 (42000): you have an error?in your sql syntax;?check the manual thatcorresponds?to your mysql server version?for the?right syntax?to use near?''db\_test'.* to 'test_user'' at line 1
经过多次测试,终于发现这个语法应该变成:
1 grant all on `db\_test`.*?to 'test_user';
发现不同了吗?至少我当时没有发现,经过尝试才发现,转义的时候,不仅原有的通配符需要反斜杆[\]进行转义,就连包含转义字串的引号也要变成反勾号!反勾号是啥?就是标准键盘中主键盘数字键1前面的那个键(也就是esc下面的那个键)。
经过修正后的sql语句非常顺利的执行了,类似的情况,在grant中需要注意的是to后面的user,平时user字串是可以不用单引号的,但如果user字串中包含了中划线(-)的时候,或者含有通配符(%)的时候,则必须要有引号包含。
?
注,jdbc的url 中同样需要转义:
url=jdbc:mysql://127.0.0.1:3306/db\_dev?useunicode=true&characterencoding=utf-8
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product