CentOS安裝和操作mysql

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

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

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

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

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

    創建一個可以從任何地方連接服務器的一個完全的超級用戶,但是必須使用一個口令
    mysql> grant all privileges on *.* to user@localhostidentified 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 *.* fromroot@”%”;
    mysql> delete from user where user=”root” andhost=”%”;
    mysql> flushprivileges;
  9. 細粒度授權
    創建一個用戶custom在特定客戶端it363.com登錄,可訪問特定數據庫fangchandb
    mysql >grant select, insert, update, delete,create,drop on fangchandb.* to custom@ it363.com identified by ‘passwd’

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

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

  16. 可將查詢存儲在一個文件中並告訴mysql從文件中讀取查詢而不是等待鍵盤輸入。
    可利用輸入重定向實用程序來完成這項工作。例如,如果在文件my_file.sql 中存放有查詢,可如下執行這些查詢:
    如果您想將建表語句提前寫在sql.txt中: mysql > mysql -h yourhost -uroot -p yourdatabase <sql.txt
發佈了30 篇原創文章 · 獲贊 5 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章