In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
The following mainly brings you the method of using shell script to connect to the database to operate mysql. I hope these contents can bring you practical use. This is also the main purpose of this article that I edit the method of using shell script to connect to the database to operate mysql. All right, don't talk too much nonsense, let's just read the following.
Various operation statements of various sql can be executed using the mysql-e parameter. You can manipulate the methods of mysql in shell without running mysql at the prompt of mysql.
Format: mysql-hhostname-Pport-uusername-ppassword-e ""
#! / bin/bash
HOSTNAME= "192.168.70.161" # Database CVM address
PORT= "3306"
USERNAME= "root"
PASSWORD= "123456"
DBNAME= "test_db_test" # Database name
Name of the table in the TABLENAME= "test_table_test" # database
# you can also write HOSTNAME= "localhost". The port number PORT can not be set.
# 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}"
Note:-p$ {PASSWORD} cannot have spaces in the middle
# create a 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}-D ${DBNAME}-e "${create_db_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}"
# updating 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}"
For the above about the use of shell script to connect to the database to operate mysql, we do not think it is very helpful. If you need to know more, please continue to follow our industry information. I'm sure you'll like it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.