简体   繁体   中英

Run Query to MySQL from a Bash Script

please help, whats wrong?

sudo -u root /etc/scripts/mysql.sh root 111111

#!/bin/bash 
mysql --host=localhost --user=root --password=111111
mysql 1<< EOF
INSERT INTO
table1(id)
SELECT MAX(id) + 1 FROM table1;
EOF

You should just invoke MySQL once and specify the default database:

#!/bin/bash 
mysql --host=localhost --user=root --password=111111 1 << EOF
INSERT INTO
table1(id)
SELECT MAX(id) + 1 FROM table1;
EOF

Use below script:

#!/bin/bash 

USER='root'
PASS='root123'

mysql -u$USER -p$PASS mydb -e"insert into table1 (id) select (max(id) + 1) from table1;"

Note: As you are executing sql from same server so no need of localhost.

Now use as from directory where your script exists-

sh myscript.sh

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM