Bug #101124 mysql batch mode execute sql failed with mysql client
Submitted: 12 Oct 2020 7:35 Modified: 9 Feb 2021 7:20
Reporter: zheng shen Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Command-line Clients Severity:S4 (Feature request)
Version:5.7,8.0 OS:Any
Assigned to: CPU Architecture:Any

[12 Oct 2020 7:35] zheng shen
Description:
[root@promote ~]# mysql -uroot -pxxxx -h127.0.0.1 < 2.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1049 (42000) at line 5: Unknown database 'zhangtaodb01'

[root@promote ~]# cat 2.sql
create database zhangtaodb01;
create database zhangtaodb02;
use `zhangtaodb01`;
create table t1(id int);
DROP DATABASE IF EXISTS zhangtaodb01;
use `zhangtaodb02`;
create table t2(id int);

How to repeat:
[root@promote ~]# cat 2.sql
create database zhangtaodb01;
create database zhangtaodb02;
use `zhangtaodb01`;
create table t1(id int);
DROP DATABASE IF EXISTS zhangtaodb01;
use `zhangtaodb02`;
create table t2(id int);

mysql -uroot -pxxxx -h127.0.0.1 < /root/2.sql 
or 
mysql -uroot -pxxxx -h127.0.0.1 -e "source /root/2.sql"
will failed

[root@promote ~]# mysql -uroot -pxxxx -h127.0.0.1 < /root/2.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 1049 (42000) at line 5: Unknown database 'zhangtaodb01'
but
mysql -uroot -pxxxx -h127.0.0.1
> source /root/2.sql
will success

Suggested fix:
client/mysql.cc has function com_use,com_use has function get_current_db about DROP DATABASE ,but because of "use `zhangtaodb02`;" include "`". get_quote_count(line) > 0 of com_use will lead to function normalize_dbname execute.

function normalize_dbname have current_db of pointer ,current_db value is zhangtaodb01, but zhangtaodb01 of database has been droped
/* Restore the original database. */ 
if (current_db && mysql_select_db(&mysql, current_db)) return 1;

Suggested fix:
in function com_use,can I move get_current_db() before get_quote_count.
[14 Oct 2020 13:48] MySQL Verification Team
Hi Mr. shen,

Thank you for your feature request.

We have analysed it and are verifying it.
[29 Jan 2021 18:30] MySQL Verification Team
This is not a security vulnerability.
[9 Feb 2021 7:20] Paul DuBois
Posted by developer:
 
Fixed in 5.7.34, 8.0.24.

When the mysql client was used in batch mode, its parser could be
confused by USE followed by DROP DATABASE when the USE database name
was quoted.
[9 Feb 2021 13:24] MySQL Verification Team
Thank you, Paul ......