2011-08-18 73 views
1

我想知道是否可以在由mysql执行的sql脚本中切换“连接”用户。我可以在mysql源代码的sql脚本中切换“连接”用户吗?

下面是我的目标的描述。也许,没有必要让用户和另一个解决方案给我我想要的。

我想写一个数据库创建脚本像这样:

create databse some_db; 

create user [email protected] identified by 'pw'; 

grant all on u.* to u; 

-- Here's where I want to switch the user, but *obviously* the 
-- following command fails 

connect [email protected]/pw; 

-- Now, as new user, create the tables: 

use some_db; 

create table t (... 

这个脚本然后将命令行称为像

$ mysql -u root -p"....." < create_db.sql 

这是可能的,或者我必须离开脚本以切换连接的用户?

+0

为什么要切换用户中间脚本?你从中得不到什么。 –

+0

@Justin也许有一个用户具有CREATE DATABASE和GRANT权限,但没有CREATE TABLE,而另一个用户只具有CREATE TABLE。 – Shi

+0

@Shi我站好了,有些害怕了。 –

回答

2

AFAIK,你不能改变用户在脚本中:

如果连不工作,你需要一个脚本用户。

顺便说一句,connect命令只重新连接,但只有两个可选参数作为文档状态:db和host。

0

我不知道从SQL脚本中切换用户的常规方法。

不规则可能是使用stored procedure或使用table triggers。两者都允许设置DEFINER。因此,您可以尝试将“特殊用户代码”放入存储过程或触发器中,调用存储过程或触发触发器并执行代码。除非你创建与其他的凭据一个全新的连接

#!/bin/sh -eu 
mysql --host localhost --user root --password root_pw < create_user.sql 
mysql --host localhost --user u --password pw < create_table.sql 
相关问题