連結資料庫 :mysql -h localhost -u username -p password
顯示所有資料庫 :show databases;
標明資料庫 :use database_name;
檢視所有表 :show tables;
檢視表中内容 :select * from table_name #select * from wp_users
mysql
> use mysql # 進入mysql 資料庫
mysql> show tables;
MariaDB [hqq]> show tables; # 顯示一共有幾張表
+-----------------------+
| Tables_in_hqq |
+-----------------------+
| wp_commentmeta |
| wp_comments |
| wp_links |
| wp_options |
| wp_postmeta |
| wp_posts |
| wp_term_relationships |
| wp_term_taxonomy |
| wp_termmeta |
| wp_terms |
| wp_usermeta |
| wp_users |
+-----------------------+
12 rows in set (0.00 sec)
Mysql安裝目錄
資料庫目錄:/var/lib/mysql/
配置檔案:/usr/share/mysql(mysql.server指令及配置檔案)
相關指令:/usr/bin(mysqladmin mysqldump等指令)
啟動腳本:/etc/init.d/mysql(啟動腳本檔案mysql的目錄)
系統管理
連接配接MySQL
格式: mysql -h 主機位址 -u使用者名 -p使用者密碼
例 1:連接配接到本機上的 MySQL。
test@ubuntu:~$ mysql -uroot -pmysql;
例 2:連接配接到遠端主機上的 MYSQL。
test@ubuntu:~$ mysql -h 127.0.0.1 -uroot -pmysql;
修改新密碼
在終端輸入:mysql -u使用者名 -p密碼,回車進入Mysql。
> use mysql;
> update user set password=PASSWORD('新密碼') where user='使用者名';
> flush privileges; #更新權限
> quit; #退出
增加新使用者
格式:grant select on 資料庫. to 使用者名@登入主機 identified by '密碼'
舉例:
例 1:增加一個使用者 test1 密碼為 abc,讓他可以在任何主機上登入,并對所有資料庫有
查詢、插入、修改、删除的權限。首先用以 root 使用者連入 MySQL,然後鍵入以下指令:*
mysql>grant select,insert,update,delete on *.* to root@localhost identified by 'mysql';
或者
grant all privileges on *.* to root@localhost identified by 'mysql';
然後重新整理權限設定。
flush privileges;
例 2:如果你不想 root 有密碼操作資料庫“mydb”裡的資料表,可以再打一個指令将密碼消掉。
grant select,insert,update,delete on mydb.* to root@localhost identified by '';
删除使用者
test@ubuntu:~$ mysql -u使用者名 -p密碼
mysql>delete from user where user='使用者名' and host='localhost';
mysql>flush privileges;
//删除使用者的資料庫
mysql>drop database dbname;
資料庫操作
顯示所有的資料庫
mysql> show databases;
建立資料庫
mysql> create database test;
連接配接資料庫
mysql> use test;
檢視目前使用的資料庫
mysql> select database();
目前資料庫包含的表資訊
mysql> show tables;
删除資料庫
mysql> drop database test;
表操作
備注:操作之前使用“use <資料庫名>”應連接配接某個資料庫。
建表
指令:create table <表名> (<字段名 1> <類型 1> [,..<字段名 n> <類型 n>]);
例:
mysql> create table MyClass(
id int(4) not null primary key auto_increment,
name char(20) not null,
sex int(4) not null default '0',
degree double(16,2));
擷取表結構
指令: desc 表名,或者show columns from 表名
mysql> describe MyClass
mysql> desc MyClass;
mysql> show columns from MyClass;
删除表
指令:drop table <表名>
例如:删除表名為 MyClass 的表
mysql> drop table MyClass;
插入資料
指令:insert into <表名> [( <字段名 1>[,..<字段名 n > ])] values ( 值 1 )[, ( 值 n )]
mysql> insert into MyClass values(1,'Tom',96.45),(2,'Joan',82.99), (2,'Wang', 96.59);
查詢表中的資料
查詢所有行
mysql> select * from MyClass;
查詢前幾行資料
例如:檢視表 MyClass 中前 2 行資料
mysql> select * from MyClass order by id limit 0,2;
mysql> select * from MyClass limit 0,2;
删除表中資料
指令:delete from 表名 where 表達式
例如:删除表 MyClass 中編号為 1 的記錄
mysql> delete from MyClass where id=1;
修改表中資料
指令:update 表名 set 字段=新值,... where 條件
mysql> update MyClass set name='Mary' where id=1;
在表中增加字段
指令:alter table 表名 add 字段 類型 其他;
例如:在表 MyClass 中添加了一個字段 passtest,類型為 int(4),預設值為 0
mysql> alter table MyClass add passtest int(4) default '0'
更改表名
指令:rename table 原表名 to 新表名;
例如:在表 MyClass 名字更改為 YouClass
mysql> rename table MyClass to YouClass;
更新字段内容
指令:update 表名 set 字段名 = 新内容
update 表名 set 字段名 = replace(字段名, '舊内容', '新内容');
例如:文章前面加入 4 個空格
update article set content=concat(' ', content);
資料庫導入導出
從資料庫導出資料庫檔案
使用“mysqldump”指令
首先進入 DOS 界面,然後進行下面操作。
1)導出所有資料庫
格式:mysqldump -u [資料庫使用者名] -p -A>[備份檔案的儲存路徑]
2)導出資料和資料結構
格式:mysqldump -u [資料庫使用者名] -p [要備份的資料庫名稱]>[備份檔案的儲存路徑]
例 1:将資料庫 mydb 導出到 e:\MySQL\mydb.sql 檔案中。
打開開始->運作->輸入“cmd”,進入指令行模式。
c:\> mysqldump -h localhost -u root -p mydb >e:\MySQL\mydb.sql
然後輸入密碼,等待一會導出就成功了,可以到目标檔案中檢查是否成功。
例 2:将資料庫 mydb 中的 mytable 導出到 e:\MySQL\mytable.sql 檔案中。
c:\> mysqldump -h localhost -u root -p mydb mytable>e:\MySQL\mytable.sql
例 3:将資料庫 mydb 的結構導出到 e:\MySQL\mydb_stru.sql 檔案中。
c:\> mysqldump -h localhost -u root -p mydb --add-drop-table >e:\MySQL\mydb_stru.sql
備注:-h localhost 可以省略,其一般在虛拟主機上用。
3)隻導出資料不導出資料結構
格式:
mysqldump -u [資料庫使用者名] -p -t [要備份的資料庫名稱]>[備份檔案的儲存路徑]
4)導出資料庫中的Events
格式:mysqldump -u [資料庫使用者名] -p -E [資料庫使用者名]>[備份檔案的儲存路徑]
5)導出資料庫中的存儲過程和函數
格式:mysqldump -u [資料庫使用者名] -p -R [資料庫使用者名]>[備份檔案的儲存路徑]