MySQL uses a shell script to link the database for operation

Source: Internet
Author: User
Tags mysql in

Use the MYSQL-E parameter to execute various operation statements of various SQL. Instead of running MySQL at the prompt of MySQL, you can manipulate MySQL in the shell.

Format: Mysql-hhostname-pport-uusername-ppassword-e ""

  

#!/bin/bash

Hostname= "192.168.70.161" #数据库服务器地址
Port= "3306"
Username= "Root"
Password= "123456"

Dbname= "Test_db_test" #数据库名称
Tablename= "Test_table_test" #数据库中表的名称

#也可以写 hostname= "localhost", port number port can not be set

#创建数据库
Create_db_sql= "CREATE database IF not EXISTS ${dbname}"
Mysql-h${hostname}-p${port}-u${username}-p${password}-E "${create_db_sql}"

Note: There can be no spaces in the middle of-p${password}

#创建表
Create_table_sql= "CREATE table IF not EXISTS ${tablename} (name varchar (), id int (one) default 0)"
Mysql-h${hostname}-p${port}-u${username}-p${password}-D ${dbname}-E "${create_db_sql}"

#插入数据
Insert_sql= "INSERT INTO ${tablename} values (' Billchen ', 2)"
Mysql-h${hostname}-p${port}-u${username}-p${password} ${dbname}-E "${insert_sql}"

#查询
Select_sql= "SELECT * FROM ${tablename}"
Mysql-h${hostname}-p${port}-u${username}-p${password} ${dbname}-E "${select_sql}"

#更新数据
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_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}"

MySQL uses a shell script to link the database for operation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.