student.sql 下載
mysql 命令參數詳解
-u 用戶名
-p 密碼
-h 服務器ip
-D 連接的數據庫
-N 不輸出列信息
-B 使用tab鍵代替默認交互分隔符
-e 執行sql語句
其他選項
-E 垂直輸出
-H 以HTML格式輸出
-X 以XML格式輸出
1.安裝服務
apt-get install mariadb-server
2.啓用啓動服務
sudo systemctl start mariadb
sudo systemctl enable mariad
3.後續設置命令
sudo mysql_secure_installation
root身份登錄
$ sudo mysql
創建school數據庫
create database school default character set utf8;
創建低權限的用戶
grant all on school.* to chencl@'%' identified by '123456';
創建低權限的用戶
grant all on school.* to chencl@'localhost' identified by '123456';
使用創建的低權限登錄數據庫
mysql -uchencl -p123456 -h localhost
導入數據到school數據庫中
$ mysql -uchencl -p123456 -h localhost school < /home/chencl/桌面/student.sql
登錄數據庫
$ mysql -uchencl -p123456 -h localhost
顯示所有的數據庫
show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| school |
+--------------------+
2 rows in set (0.00 sec)
切換到school庫中
use school;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
查詢數據庫school中的所有的表
show tables;
+------------------+
| Tables_in_school |
+------------------+
| course |
| score |
| student |
| teacher |
+------------------+
4 rows in set (0.00 sec)
查詢 course 表的所有內容
select * from course;
+------+---------+------+
| c_id | c_name | t_id |
+------+---------+------+
| 1001 | chinese | 1002 |
| 1002 | math | 1001 |
| 1003 | english | 1003 |
+------+---------+------+
3 rows in set (0.00 sec)
查詢表的描述 表結構
describe course;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| c_id | varchar(20) | NO | PRI | NULL | |
| c_name | varchar(20) | NO | | | |
| t_id | varchar(20) | NO | | NULL | |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql的常用選項:
-E 垂直顯示
mysql -uchencl -p123456 -h localhost -E -B -N -D school -e "select * from student;";
*************************** 1. row ***************************
1001
zhaolei
1990-1001-1001
male
-H 以HTML格式顯示
mysql -uchencl -p123456 -h localhost -H -B -N -D school -e "select * from student;" > result.html
-X 爲XML文件格式
mysql -uchencl -p123456 -h localhost -X -B -N -D school -e "select * from student;" > result.xml
<?xml version="1.0"?>
<resultset statement="select * from student" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<row>
<field name="s_id">1001</field>
<field name="s_name">zhaolei</field>
<field name="s_birth">1990-1001-1001</field>
<field name="s_sex">male</field>
</row>
</resultset>
編寫腳本處理SQL數據 opera.sh
sh opera.sh "select * from score" school
sh opera.sh "insert into score values('1020','1002','100')" school
opera.sh 內容
#!/bin/bash
user="chencl"
password="123456"
host="localhost"
SQL="$1"
DBNAME="$2"
mysql -u"$user" -p"$password" -h"$host" -D "$DBNAME" -B -e "$SQL"