1. 程式人生 > >MySQL - - 庫操作

MySQL - - 庫操作

目錄

  • 系統資料庫
  • 資料庫操作
  • 命名規範
  • 使用者許可權
  • 修改密碼
  • 忘記密碼
  • 中文亂碼問題

1, 系統資料庫

  • information_schema :虛擬庫,不佔用磁碟空間,儲存的是資料庫啟動後的一些引數,如使用者表資訊、列資訊、許可權資訊、字元資訊等
  • mysql:核心資料庫,裡面包含使用者、許可權、關鍵字等資訊。不可以刪除
  • performance_schema:mysql 5.5版本後新增的新庫,主要收集系統性能引數,記錄處理查詢請求時發生的各種事件、鎖等現象
  • sys : mysql5.7版本新增加的庫,通過這個庫可以快速的瞭解系統的元資料資訊,可以方便DBA發現數據庫的很多資訊,解決效能瓶頸都提供了巨大幫助

2, 資料庫操作

2.1 建立資料庫

  • 建立資料庫並指定字符集
mysql> create database db_name charset utf8;

2.2 檢視資料庫

  • 顯示資料庫版本
mysql> select version();
  • 顯示當前時間
mysql> select now();
  • 檢視所有資料庫
mysql> show databases;
  • 檢視當前使用的資料庫
mysql> select database();

2.3 選擇資料庫

mysql> USE db_name;

2.4 刪除資料庫

DROP DATABASE db_name;

3, 命名規範

  • 可以由字母、數字、下劃線、@、#、$
  • 區分大小寫
  • 唯一性
  • 不能使用關鍵字如: CREATE SELECT
  • 不能單獨使用數字
  • 最長128位

4, 使用者許可權

  • 建立使用者
# create user '使用者名稱'@'IP地址' identified by '密碼';
mysql> create user "test"@"localhost" identified by "test";
  • 刪除使用者
# drop user '使用者名稱'@'IP地址';
mysql> drop user "test"@"localhost";
  • 修改使用者
# rename user '使用者名稱'@'IP地址'; to '新使用者名稱'@'IP地址';
mysql> rename user "test"@"localhost"; to "test"@"%";
  • 檢視使用者
mysql> select user,host from mysql.user;
  • 授權
# grant  許可權 on 資料庫.表 to   '使用者'@'IP地址'
# db1資料庫下的所有表的 查詢.更新.修改許可權
mysql> grant select,update,delete on db1.* to "test"@"localhost";
# 所有庫的所有許可權(除grant許可權外)
mysql> grant all privileges on *.*  to 'test'@'localhost';
  • 重新整理使用者許可權
mysql> flush privileges;
  • 檢視許可權
# show grants for '使用者'@'IP地址'  
mysql> show grnats for "test"@"localhost";
  • 取消許可權
# revoke 許可權 on 資料庫.表 from '使用者'@'IP地址' 
  • 許可權列表
            all privileges  除grant外的所有許可權
            select          僅查許可權
            select,insert   查和插入許可權
            ...
            usage                   無訪問許可權
            alter                   使用alter table
            alter routine           使用alter procedure和drop procedure
            create                  使用create table
            create routine          使用create procedure
            create temporary tables 使用create temporary tables
            create user             使用create user、drop user、rename user和revoke  all privileges
            create view             使用create view
            delete                  使用delete
            drop                    使用drop table
            execute                 使用call和儲存過程
            file                    使用select into outfile 和 load data infile
            grant option            使用grant 和 revoke
            index                   使用index
            insert                  使用insert
            lock tables             使用lock table
            process                 使用show full processlist
            select                  使用select
            show databases          使用show databases
            show view               使用show view
            update                  使用update
            reload                  使用flush
            shutdown                使用mysqladmin shutdown(關閉MySQL)
            super                   使用change master、kill、logs、purge、master和set global。還允許mysqladmin除錯登陸
            replication client      伺服器位置的訪問
            replication slave       由複製從屬使用

5, 修改密碼

  • mysqladmin 命令
# mysqladmin -u使用者名稱 -p密碼 password 新密碼
  • 直接設定使用者密碼
mysql> set password for '使用者名稱'@'IP' = password('新密碼');
mysql> flush privileges;
  • 修改mysql庫下的user表
# 5.6 版本
mysql> update mysql.user set password = password('新密碼') where user= '使用者名稱';
mysql> flush privileges; 

# 5.7 版本
mysql> update mysql.user set authentication_string=password('新密碼') where user= '使用者名稱';
mysql> flush privileges; 

6, 忘記密碼

  • 在命令列跳過授權表命令
mysqld_safe --skip-grant-tables &
  • 在 my.cnf 檔案配置跳過授權表命令
[mysqld]
skip-grant-tables

7, 中文亂碼問題

  • 檢視字元編碼
mysql> SHOW VARIABLES LIKE 'char%';
  • 修改my.cnf 配置檔案
[client]

default-character-set=utf8

[mysql]

#設定mysql客戶端預設字符集
default-character-set=utf8

[mysqld]

#設定3306埠
port = 3306

#允許最大連線數
max_connections=200

#服務端使用的字符集預設為8位元編碼的latin1字符集
character-set-server=utf8

#建立新表時將使用的預設儲存引擎
default-storage-engine=INNODB

#解決mysql在執行sql語句後出現1055錯誤,sql_mode = only_full_group_by不相容
sql_mode='NO_ENGINE_SUBSTITUTION'
  • 轉自:http://www.cnblogs.com/wangfengming/articles/7875313.html