[請聲明出處:http://write.blog.csdn.net/postedit/45565521](http://write.blog.csdn.net/postedit/45565521)
本文主要是記錄本人在CentOS系統下面使用Mysql的一些命令和操作,特此記錄。
[本文檔的下載地址:http://download.csdn.net/detail/u012377333/8673599](http://download.csdn.net/detail/u012377333/8673599)
1?檢查是否安裝了mysql
~~~
?#rpm –qa | grep mysql
~~~

2?檢查mysqld服務是否開啟
`?#service mysqld status
`

3?啟動mysqld服務
`?#service mysqld start`
?
第一次啟動會初始化,時間會有點久…


4?設置用戶root的密碼
?#/usr/bin/mysqladmin –u root password ‘dragonwake’

5?本地連接數據庫
?#mysql –u root -pdragonwake

6?顯示所有的數據庫
mysql>show databases;

7?使用mysql數據庫
mysql>use mysql;

8?顯示當下數據庫(mysql)所有的表
mysql>show tables;

9?查看表(mysql.user)結構
mysql>describe user;
還有其他的方法:
a)???????mysql>desc user;
b)???????mysql>show columns from user;
c)???????mysql>show create tables user;

10?添加一個mysql用戶
mysql>insert into mysql.user(Host,User,password) values(‘localhost’,’mysql’,password(‘mysql’));
刷新系統權限表
mysql>flush privileges;
主機為’localhost’,說明只能在本地登錄,要想遠程登錄,主機改為’%’。

11?創建一個數據庫smartDB
mysql>create database smartDB;

12?授權mysql用戶擁有數據庫smartDB所有權限(某個數據庫的全部權限)
~~~
mysql>grant all privileges on smartDB.* to mysql@localhost identified by ‘mysql’;
~~~
刷新系統權限表
`mysql>flush privileges;`
上面是對本地的授權@localhost,對于非本地授權@”%”。

13?退出連接
`mysql>quit;`
a) mysql>exit;

14?使用mysql用戶登錄
`?#mysql –u mysql –pmysql`
和上面root用戶登錄是一樣的方法。

15?創建數據庫smartDB的表p2p_tb_camera
切換到數據庫smartDB
`mysql>use smartDB;`
創建數據庫表p2p_tb_camera
~~~
mysql>create table p2p_tb_camera(
?????????ipc_id char(7) not null primary key,
?????????sn varchar(16) not null,
?????????entid varchar(20) not null,
?????????enc varchar(30) not null
?????????);
~~~
顯示當選數據庫smartDB下面所有的表
mysql>show tables;
顯示表p2p_tb_camera的結構
mysql>desc p2p_tb_camera;

17?插入數據
~~~
mysql>insert p2p_tb_camera values(‘758871’, ‘01AE465D08141280’, ‘1426822572_e3575b
18208b’);
~~~
當然,上面這么寫是因為插入所有的數據,如果要指定字段插入數據,只插入ipc_id的值:
`mysql>insert p2p_tb_camera(ipc_id) values(‘123456’);`
實際上,沒有辦法把數據插入到表中,因為表限制了sn,entid,enc的值為非空。

18?查詢數據
`mysql>select * from p2p_tb_camera;`

19?更新數據
更新表p2p_tb_camera中字段sn的值為111,更新條件為ipc_id的值758871和entid的值1
`mysql>update p2p_tb_camera set sn=’111’ where ipc_id=’758871’ and entid=’1’;
`
查詢更新后的數據
`mysql>select * from p2p_tb_camera;`

20?刪除數據
刪除表p2p_tb_camera中的數據記錄,刪除條件為ipc_id的值758871和sn的值111
`mysql>delete from p2p_tb_camera where ipc_id=’758871’ and sn=’111’;
`
查詢更新后的數據
~~~
mysql>select * from p2p_tb_camera;
~~~
表p2p_tb_camera中沒有任何數據

21?刪除表
刪除表p2p_tb_camera
~~~
mysql>drop table p2p_tb_camera;
~~~
查詢當前數據庫smartDB刪除表之后的表
`mysql>show tables;`
刪除表p2p_tb_camera之后,數據庫smartDB沒有表了

22?執行sql腳本
腳本create_table_p2p_tb_camera.sql的內容:
~~~
use smartDB;
create table p2p_tb_camera(
?????????ipc_id char(7) not null primary key,
?????????sn varchar(16) not null,
?????????entid varchar(20) not null,
?????????enc varchar(30) not null
?????????);
~~~
執行腳本/opt/smartcare/p2pserver/tools/mysql/create_p2p_tb_camera.sql
`mysql>source /opt/smartcare/p2pserver/tools/mysql/create_p2p_tb_camera.sql`

23?刪除數據庫
刪除數據庫smartDB
mysql>drop database smartDB;

24?修改mysql用戶密碼
修改用戶mysql的密碼為dragonwake
`mysql>update mysql.user ser password-password(‘dragonwake’) where User=’mysql’;`

25?刪除用戶
刪除用戶mysql
`mysql>delete form mysql.user where User=’mysql’;`

26刪除用戶權限
刪除用戶mysql的權限
`mysql>drop user mysql@localhost;`
