Shell to operate mysql database _ MySQL

Source: Internet
Author: User
Shell to operate mysql database bitsCN.com

Shell operations on mysql databases

Mysql-hhostname-Pport-uusername-ppassword-e SQL statements related to mysql. you can operate mysql in shell without running mysql at a mysql prompt.

#! /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 the table in the database

# Creating a database

Create_db_ SQL = "create database IF NOT EXISTS $ {DBNAME }"

Mysql-h $ {HOSTNAME}-P $ {PORT}-u $ {USERNAME}-p $ {PASSWORD}-e "$ {create_db_ SQL }"

# Creating a table

Create_table_ SQL = "create table IF NOT EXISTS $ {TABLENAME} (namevarchar (20), id int (11) default 0 )"

Mysql-h $ {HOSTNAME}-P $ {PORT}-u $ {USERNAME}-p $ {PASSWORD }$ {DBNAME}-e "$ {create_table_ SQL }"

# Insert data

Insert_ SQL = "insertinto $ {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 }"

# Deleting data

Delete_ SQL = "deletefrom $ {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 }"

BitsCN.com

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.