centOS中mysql一些常用操作

安裝mysql
yum -y install mysql-server
修改mysql配置
vi /etc/my.cnf 這裏會有很多需要注意的配置項,後面會有專門的筆記
暫時修改一下編碼(添加在密碼下方): default-character-set = utf8
設置mysql隨系統啓動
# chkconfig mysqld on  ← 設置MySQL服務隨系統啓動自啓動
# chkconfig --list mysqld  ← 確認MySQL自啓動mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off  ← 如果2--5爲on的狀態就OK
# /etc/rc.d/init.d/mysqld start  ← 啓動MySQL服務


顯示當前mysql版本和當前日期
select version(),current_date;


修改mysql root密碼
# mysql -u root  ← 用root用戶登錄MySQL服務器
select user,host,password from mysql.user;  ← 查看用戶信息
set password for root@localhost=password('在這裏填入root密碼');  ← 設置root密碼
select user,host,password from mysql.user;  ← 查看用戶信息
exit  ← 退出MySQL服務器


使用密碼登陸mysql
mysql -u root -p 
刪除mysql匿名用戶
select user,host from mysql.user;  ← 查看用戶信息
delete from mysql.user where user='';  ← 刪除匿名用戶
select user,host from mysql.user;  ← 查看用戶信息
查看數據庫
show databases;  ← 查看系統已存在的數據庫
drop database test;  ← 刪除名爲test的空數據庫
show databases;  ← 查看系統已存在的數據庫


mysql查看打開的端口: show variables like 'port';
創建新用戶併爲新用戶授權
grant all privileges on test.* to centospub@localhost identified by '在這裏定義密碼';  ← 建立對test數據庫有完全操作權限的名爲centospub的用戶


創建一個可以從任何地方連接服務器的一個完全的超級用戶,但是必須使用一個口令
mysql> grant all privileges on *.* to user@localhost identified by ’口令’
增加新用戶
格式:
grant select on 數據庫.* to 用戶名@登錄主機 identified by “密碼”
GRANT ALL PRIVILEGES ON *.* TO monty@localhost IDENTIFIED BY ’something’ WITH GRANT OPTION;
GRANT ALL PRIVILEGES ON *.* TO monty@”%” IDENTIFIED BY ’something’ WITH GRANT OPTION;
刪除授權:
mysql> revoke all privileges on *.* from root@”%”;
mysql> delete from user where user=”root” and host=”%”;
mysql> flush privileges;
細粒度授權
創建一個用戶custom在特定客戶端it363.com登錄,可訪問特定數據庫fangchandb
mysql >grant select, insert, update, delete, create,drop on fangchandb.* to custom@ it363.com identified by ‘ passwd’


建立新數據庫
create database test;  ← 建立名爲test的數據庫 (注意是否可以建立這個數據庫是在上面建立新用戶的時候就決定了的)
使用數據庫
use test  ← 連接到數據庫
show tables;  ← 查看數據庫中已存在的表
刪除測試賬戶
revoke all privileges on *.* from centospub@localhost;  ← 取消centospub用戶對數據庫的操作權限
delete from mysql.user where user='centospub' and host='localhost';  ← 刪除centospub用戶
select user from mysql.user where user='centospub';  ← 查找用戶centospub,確認已刪除與否
flush privileges;  ← 刷新,使以上操作生效
刪除數據庫
drop database name 直接刪除數據庫,不提醒
mysqladmin drop databasename 刪除數據庫前,有提示。
表操作
show tables; 顯示錶
describe tablename; 表的詳細描述
重命名錶: mysql > alter table t1 rename t2;


CentOS系統中mysqldump
在shell中執行下面的命令
備份數據庫 shell> mysqldump -h yourhost vi-u root -p dbname >dbname_backup.sql
恢復數據庫 shell> mysqladmin -h yourhost -u root -p create dbname
shell> mysqldump -h yourhost -u root -p dbname < dbname_backup.sql
如果只想Dump建表指令,則命令如下: shell> mysqladmin -u root -p -d databasename > a.sql
如果只想Dump插入數據的sql命令,而不需要建表命令,則命令如下: shell> mysqladmin -u root -p -t databasename > a.sql
那麼如果我只想要數據,而不想要什麼sql命令時,應該如何操作呢? mysqldump -T./ phptest driver
其 中,只有指定了-T參數纔可以卸出純文本文件,表示卸出數據的目錄,./表示當前目錄,即與mysqldump同一目錄。如果不指定driver 表,則將卸出整個數據庫的數據。每個表會生成兩個文件,一個爲.sql文件,包含建表執行。另一個爲.txt文件,只包含數據,且沒有sql指令。


可將查詢存儲在一個文件中並告訴mysql從文件中讀取查詢而不是等待鍵盤輸入。
可利用輸入重定向實用程序來完成這項工作。例如,如果在文件my_file.sql 中存放有查詢,可如下執行這些查詢:
如果您想將建表語句提前寫在sql.txt中: mysql > mysql -h yourhost -u root -p yourdatabase < sql.txt

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