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 hjy@localhost identified by 'westos';    ##創建本地用戶hjy並添加密碼westos,默認密碼是加密的
CREATE USER hee@'%' identified by 'redhat';        ##創建用戶hee,%表示這個賬戶可以在任何主機登錄
select host,User,Password from user;            ##查詢user表中的host,user,password字段

grant select on  *.* to user1@localhost identified by 'passwd1'; ##授權user1,密碼爲passwd1,並且只能在本地查詢數據庫的所在內容
grant all on mysql.* to user2@'%' identified by 'passwd2';     ##授權user2,密碼爲passwd2,可以從遠程任意主機登錄mysql並且可以對mysql數據庫任意操作(%改爲ip可指定此ip登錄)

FLUSH PRIVILEGES;                        ##重載授權表
SHOW GRANTS FOR hjy@localhost;                    ##查看用戶授權
REVOKE DELETE,UPDATE,INSERT on mysql.* from hjy@localhost;    ##撤銷用戶對mysql的DELETE,UPDATE,INSERT權限
REVOKE all on mysql.* from hjy@localhost;            ##撤銷用戶所有權限
DROP USER hjy@localhost;                    ##刪除用戶


備份
/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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章