標籤:
請聲明出處:
本文主要是記錄本人在CentOS系統下面使用Mysql的一些命令和操作,特此記錄。
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 [email protected] 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 [email protected];
基於CentOS的Mysql的使用說明