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.
I want to write a database creation script like so:
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, 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.
BTW, connect command only reconnects but there are only two optional parameters as documentation states: db and host.