Detailed explanation of the method of operating mysql in shell
The following content mainly brings you a detailed explanation of the methods of operating mysql in shell. The knowledge mentioned here, which is slightly different from books, is summarized by professional and technical personnel in the process of contact with users, and has a certain value of experience sharing. I hope to bring help to the majority of readers.
The sql statement of MySQL-hhostname-Pport-uusername-ppassword-e related mysql can manipulate the method of mysql in shell without running mysql at the prompt of mysql.
#! / bin/bash
HOSTNAME= "192.168.111.84" # Database Information
PORT= "3306"
USERNAME= "root"
PASSWORD= ""
DBNAME= "test_db_test" # Database name
Name of the table in the TABLENAME= "test_table_test" # database
# 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}"
# 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} ${DBNAME}-e "${create_table_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 detailed explanation of how to operate mysql in shell, if you have more information, you can continue to pay attention to the innovation of our industry. If you need professional solutions, you can contact the pre-sales and after-sales on the official website. I hope this article can bring you some knowledge updates.