Linux shell command Execute SQL (MySQL primer)

Source: Internet
Author: User

Get started with a shell operation database under Linux today

1.shell File Contents:

#!/bin/bashhostname= "11.8.8.87" port= "3306" username= "root" password= "root" dbname= "testa" tablename= "test" use_db_ Sql= "Use ${dbname}" Mysql-h${hostname}-p${port}-u${username}-p${password}-E "${use_db_sql}" insert_sql= "INSERT INTO ${tablename} values (' 7 ', ' Mike ', ' 123456 ') "Mysql-h${hostname}-p${port}-u${username}-p${password} ${dbname}-E" ${ Insert_sql} "

Host Address: 11.8.8.87 Port: 3306 database: Testa table: Test

2.TEST Table Structure:

Mysql> desc test;+----------+-------------+------+-----+---------+----------------+| Field    | Type        | Null | Key | Default | Extra          |+----------+-------------+------+-----+---------+----------------+| t_id     | int     | NO   | PRI | NULL    | auto_increment | | username | varchar (32) | YES  |     | NULL    |                | | password | varchar (32) | YES  |     | NULL    |                | +----------+-------------+------+-----+---------+----------------+3 rows in Set (0.00 sec)

3. Here I named the shell file named: Mysqltestshell, uploaded to the Linux server after editing on Windows,

Modify permissions: chmod +x Mysqltestshell

Execute./mysqltestshell,

Error:-bash:./mysqltestshell:/bin/bash^m:bad interpreter:no such file or directory

Workaround:

VI Mysqltestshell

Then use the command: set FF?

You can see the Word dos or UNIX. If it is a DOS format, then you can use set Ff=unix to force it into UNIX format, and then save the disk to exit. Run it again and see.

4. Running Results

Please consciously ignore the garbled case (here is just the test), you can see a new record

Mysql> SELECT * FROM test;+------+----------+----------------------+| t_id | Username | Password             |+------+----------+----------------------+|    1 | Jung-     Xuan hides scythe             | |    2 |??       | Xian Xuan hides scythe             | |    3 |??       | A busy cat stove? Mob stove?         | |    4 |??       | ????                 ||    5 |??       | ????                 ||    6 |??       | ????                 ||    7 | Mike     | 123456               |+------+----------+----------------------+7 rows in Set (0.00 sec)

----------------------------------------------------------Supplement------------------------------------------------------------- ---

MYSQL-HHOSTNAME-PPORT-UUSERNAME-PPASSWORD-E mysql-related SQL statements without running MySQL at the prompt of MySQL, which means you can manipulate MySQL in the shell.

#!/bin/bash

Hostname= "192.168.111.84" #数据库信息

Port= "3306"

Username= "Root"

Password= ""

Dbname= "Test_db_test" #数据库名称

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

#创建数据库

Create_db_sql= "CREATE database IF not EXISTS ${dbname}"

Mysql-h${hostname}-p${port}-u${username}-p${password}-E "${create_db_sql}"

#创建表

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} ${dbname}-E "${create_table_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}"

    • This article is from: Linux Tutorial Network

Linux shell command Execute SQL (MySQL primer)

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.