What does -e mean in mysql database?Detailed use of the -e parameter

Keywords: MySQL SQL Database shell

Can be manipulated with shell scripts mysqldata base , mysql-e parameter can be used to perform various sql operations (create, delete, add, delete, change, check).

usage

The sql statement of mysql-hhostname-Pport-uusername-ppassword-e related MySQL allows you to manipulate mysql's methods in a shell without running MySQL at the prompt of mysql.

Example:

#!/bin/bash
 
HOSTNAME="192.168.111.84"                                           #database information
PORT="3306"
USERNAME="root"
PASSWORD=""
 
DBNAME="test_db_test"                                                       #Database Name
TABLENAME="test_table_test"                                            #Name of table in database
 
  
 
#Create a database
create_db_sql="create database IF NOT EXISTS ${DBNAME}"
mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD} -e"${create_db_sql}"
 
  
 
#Create Table
create_table_sql="create table IF NOT EXISTS ${TABLENAME} (  name varchar(20), id int(11) default 0 )"
mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD} ${DBNAME} -e"${create_table_sql}"
 
  
 
#insert data
insert_sql="insert into ${TABLENAME} values('billchen',2)"
mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD} ${DBNAME} -e"${insert_sql}"
 
  
 
#query
select_sql="select * from ${TABLENAME}"
mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD} ${DBNAME} -e"${select_sql}"
 
  
 
#Update Data
update_sql="update ${TABLENAME} set id=3"
mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD} ${DBNAME} -e"${update_sql}"
mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD} ${DBNAME} -e"${select_sql}"
 
  
 
#Delete data
delete_sql="delete from ${TABLENAME}"
mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD} ${DBNAME} -e"${delete_sql}"
mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD} ${DBNAME} -e"${select_sql}"

 

Role of v in mysql-e

When importing the specified file with the result of mysql-e generation:

To display the statement itself at the same time: -v

To increase the number of rows of query results: -vv

To increase execution time: -vvv

Some monitoring scripts can be used to simplify code

㈠ Without v Test:  
   
[mysql@even ~]$ mysql -uroot -poracle -e "SELECT VERSION();SELECT NOW()" > /home/mysql/test.sql  
[mysql@even ~]$ cat /home/mysql/test.sql  
VERSION()  
5.5.16-log  
NOW()  
2013-05-08 18:06:35 
   
   
㈡ With one v Test:  
   
[mysql@even ~]$ mysql -uroot -poracle -e  "SELECT VERSION();SELECT NOW()" -v  > /home/mysql/test02.sql  
[mysql@even ~]$ cat /home/mysql/test02.sql  
--------------  
SELECT VERSION()  
--------------  
   
VERSION()  
5.5.16-log  
--------------  
SELECT NOW()  
--------------  
   
NOW()  
2013-05-08 18:08:40 
   
   
㈢ With two v Test:  
   
[mysql@even ~]$ mysql -uroot -poracle -e  "SELECT VERSION();SELECT NOW()" -vv  > /home/mysql/test03.sql  
[mysql@even ~]$ cat /home/mysql/test03.sql  
--------------  
SELECT VERSION()  
--------------  
   
VERSION()  
5.5.16-log  
1 row in set  
   
--------------  
SELECT NOW()  
--------------  
   
NOW()  
2013-05-08 18:14:05 
1 row in set  
   
Bye  
   
   
㈢ With three v Test:  
   
   
[mysql@even ~]$ mysql -uroot -poracle -e  "SELECT VERSION();SELECT NOW()" -vvv  > /home/mysql/test04.sql  
[mysql@even ~]$ cat /home/mysql/test04.sql  
--------------  
SELECT VERSION()  
--------------  
   
+------------+  
| VERSION()  |  
+------------+  
| 5.5.16-log |  
+------------+  
1 row in set (0.00 sec)  
   
--------------  
SELECT NOW()  
--------------  
   
+---------------------+  
| NOW()               |  
+---------------------+  
| 2013-05-08 18:14:49 |  
+---------------------+  
1 row in set (0.00 sec)  
   
Bye

 

Posted by n14charlie on Mon, 10 Feb 2020 08:26:27 -0800