Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of using shell script to connect to database to operate mysql

2025-01-28 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.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report