標籤:mysql操作命令
###########mysql常用操作命令#############
1.安裝mysql
yum install mysql mysql-server
/etc/init.d/mysqld start ##開啟mysqld服務
2.設定及登入
mysql_secure_installation ##第一次安裝mysql以後通過這條命令可以對mysql進行初始設定
mysql -uroot -predhat ##從本機登入mysql資料庫(ps -aux|grep mysql kill -9 )
mysqladmin -uroot -predhat password westos ##修改本地mysql,root密碼
mysqladmin -uroot -predhat -h 172.25.8.1 password westos ##修改遠程172.25.8.1mysql伺服器,root密碼
3.操作命令
庫操作:
show databases; ##顯示資料庫
use mysql; ##進入資料庫(按斷行符號鍵出現Database changed時說明操作成功!)
show tables; ##顯示資料庫中的表
desc user; ##查看user表的結構
flush privileges; ##重新整理資料庫資訊
select host,user,password from user; ##查詢user表中的host,user,password欄位
create database westos; ##建立westos資料庫
use westos; ##進入資料庫westos
表操作:
create table linux( ##建立表,表名linux,欄位username,password
username varchar(15) not null,
password varchar(15) not null);
select * from mysql.user; ##查詢mysql庫下的user表中的所有內容
alter table linux add age varchar(4); ##添加age欄位到linux表中
desc linux; ##查看linux表結構
ALTER TABLE linux DROP age ##刪除linux表中的age欄位
ALTER TABLE linux ADD age VARCHAR(4) AFTER username ##在linux表username欄位後添加欄位age
desc linux; ##查看linux表結構
insert into linux values (‘user1‘,18,‘passwd1‘); ##在linux表中插入username=user1,age=18,password=password1
update linux set password=‘passwd2‘ where username="user1"; ##更新linux表中user1的密碼為password2
delete from linux where username=‘user1‘; ##刪除linux表中user1的所有內容
select * from linux; ##可以進行查看
使用者管理:
CREATE USER [email protected] identified by ‘westos‘; ##建立本機使用者hjy並添加密碼westos,預設密碼是加密的
CREATE USER [email protected]‘%‘ identified by ‘redhat‘; ##建立使用者hee,%表示這個賬戶可以在任何主機登入
select host,User,Password from user; ##查詢user表中的host,user,password欄位
grant select on *.* to [email protected] identified by ‘passwd1‘; ##授權user1,密碼為passwd1,並且只能在本地查詢資料庫的所在內容
grant all on mysql.* to [email protected]‘%‘ identified by ‘passwd2‘; ##授權user2,密碼為passwd2,可以從遠程任意主機登入mysql並且可以對mysql資料庫任意操作(%改為ip可指定此ip登入)
FLUSH PRIVILEGES; ##重載授權表
SHOW GRANTS FOR [email protected]; ##查看使用者授權
REVOKE DELETE,UPDATE,INSERT on mysql.* from [email protected]; ##撤銷使用者對mysql的DELETE,UPDATE,INSERT許可權
REVOKE all on mysql.* from [email protected]; ##撤銷使用者所有許可權
DROP USER [email protected]; ##刪除使用者
備份
/var/lib/mysql
mysqldump -uroot -predhat --all-databases ##命令備份所有資料
mysqldump -uroot -predhat mysql > mysql.bak ##備份mysql庫導到mysql.bak
mysql -uroot -predhat -e "create database westos;" ##建立一個資料庫
mysql -uroot -predhat westos < mysql.bak ##恢複mysql.bak到westos庫
密碼恢複
/etc/init.d/mysqld stop
mysqld_safe --skip-grant-tables & ##跳過grant-tables授權表,不需要認證登入本地mysql資料庫
update mysql.user set password=password(‘westos‘) where user=‘root‘; ##更新mysql.user表中條件root使用者的密碼為westos
/etc/init.d/mysql restart ##重新啟動nysql
本文出自 “12148275” 部落格,請務必保留此出處http://12158275.blog.51cto.com/12148275/1910441
mysql常用操作命令