我可以在mysql派生的sql脚本中切换“已连接"用户吗? [英] Can I switch the 'connected' user within an sql script that is sourced by mysql?
问题描述
我想知道是否可以在由mysql
执行的sql脚本中切换已连接"的用户.
I was wondering if I can switch the "connected" user within an sql script that is executed by mysql
.
这是我的目标的描述.也许,不必夹住用户,而另一种解决方案可以满足我的需求.
Here's a description of my goal. Perhaps, it's not necessary to swich the user and another solution gives me what I want.
我想这样写一个数据库创建脚本:
create databse some_db;
create user u@localhost 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 u@localhost/pw;
-- Now, as new user, create the tables:
use some_db;
create table t (...
然后将在命令行中调用该脚本,例如
This script would then be called from the command line like
$ mysql -u root -p"....." < create_db.sql
这是可能的还是我必须离开脚本才能切换连接的用户?
Is this possible or do I have to leave the script in order to switch the connected user?
推荐答案
AFAIK,除非使用其他用户的凭据创建全新的连接,否则无法在脚本中间更改用户.
AFAIK, you can not change the user in the middle of the script unless you create a brand new connection with the credentials of the other user.
顺便说一句,connect命令仅重新连接,但是只有两个可选参数作为文档状态:db和host.
BTW, connect command only reconnects but there are only two optional parameters as documentation states: db and host.
这篇关于我可以在mysql派生的sql脚本中切换“已连接"用户吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!