安裝數據庫的時候,須要加擴展命令,添加全部字符集
# ./ configure --prefix=/usr/local/mysql --with-charset=gbk --with-extra-charset=all
測試是否支持gbk編碼:
set names gbk;
查看數據庫的編碼
mysql> show character set;
建立數據庫,使用gbk編碼
create database infosww default character set 'gbk';ide
導入數據,使UTF-8編碼方式鏈接測試
/usr/local/mysql/bin/mysql -u root -p ypk --default-character-set=utf8 < ypk.sql
查看修改字符集
(1)查看數據庫默認的字符集
Mysql > show variables like 'character\_set\_%';
顯示
+--------------------------+--------+
| Variable_name | Value |
+--------------------------+--------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
+--------------------------+--------+
(2)查看某個表的字符集
Mysql > show create table 表名;
(3)修改表的字符集
Mysql > ALTER TABLE tbl_name CONVERT TO CHARACTER SET charset_name
如
Mysql > alter table 表名 convert to CHARACTER set gbk;
(4)查看某個庫的字符集
Mysql> show create database 庫名;
Mysql> alter database 庫名 default character set gbk;
Mysql查看可用的字符串
Mysql > SHOW CHARACTER SET; //列出可用的字符串
+----------+-----------------------------+---------------------+--------+
| Charset | Description | Default collation | Maxlen |
+----------+-----------------------------+---------------------+--------+
| big5 | Big5 Traditional Chinese | big5_chinese_ci | 2 |
| dec8 | DEC West European | dec8_swedish_ci | 1 |
| cp850 | DOS West European | cp850_general_ci | 1 |
| hp8 | HP West European | hp8_english_ci | 1 |
| koi8r | KOI8-R Relcom Russian | koi8r_general_ci | 1 |
| latin1 | cp1252 West European | latin1_swedish_ci | 1 |
Comment: Users a
建立用戶並給其權限
grant all on 庫名.* to 用戶@localhost identified by '密碼';
建立遠程用戶
grant all privileges on *.* to sj@'%' identified by 'xywyclient123' with grant option;
example:
grant all privileges on ysq_xspace.* to clubuser@xxx.xxx.156.202 identified by "clubuser&(*";
刪除用戶
delete from user WHERE User="phplamp" and Host="localhost";
flush privileges;
設置用戶訪問權限和取消
grant all privileges on *.* to sum@192.168.0.97 identified by 'mima' with grant option;
use mysql;
update db set host = ''localhost' where user = ''用戶名'';
//若是寫成 host=localhost 那此用戶就不具備遠程訪問權限
flush privileges;
給多個數據庫相同的用戶權限
grant all on `cmsv53_%`.* to cmsdb_209@xxx.xxx.xxx209 identified by 'cmsv53_2010%^*'
// 這裏使用`` 而不是’’引號,對全部cmsv53_開頭的數據庫給同一個用戶權限。
設置用戶多權限
grant process,super on *.* to cacti@xxx.xxx.xxx.211 identified by 'cacti';
//將process和super權限賦予cacti
查詢遠程用戶的權限 見十三也可。
use mysql;
select host,user,password from user; 這裏列出來的用戶的全部列表
show grants for root@localhost;
附加:下面詳細介紹下mysql權限若是給用戶多個權限 +「,」 便可。
一.權限表
mysql數據庫中的3個權限表:user 、db、 host
權限表的存取過程是:
1)先從user表中的host、 user、 password這3個字段中判斷鏈接的IP、用戶名、密碼是否存在表中,存在則經過身份驗證;
2)經過權限驗證,進行權限分配時,按照userdbtables_privcolumns_priv的順序進行分配。即先檢查全局權限表
user,若是user中對應的權限爲Y,則此用戶對全部數據庫的權限都爲Y,將再也不檢查db,
tables_priv,columns_priv;若是爲N,則到db表中檢查此用戶對應的具體數據庫,並獲得db中爲Y的權限;若是db中爲N,則檢
查tables_priv中此數據庫對應的具體表,取得表中的權限Y,以此類推。
二.MySQL各類權限(共27個)
(如下操做都是以root身份登錄進行grant受權,以p1@localhost身份登錄執行各類命令。)
1. usage
鏈接(登錄)權限,創建一個用戶,就會自動授予其usage權限(默認授予)。
mysql> grant usage on *.* to ‘p1′@’localhost’ identified by ‘123′;
該權限只能用於數據庫登錄,不能執行任何操做;且usage權限不能被回收,也即REVOKE用戶並不能刪除用戶。
2. select
必須有select的權限,纔可使用select table
mysql> grant select on pyt.* to ‘p1′@’localhost’;
mysql> select * from shop;
3. create
必須有create的權限,纔可使用create table
mysql> grant create on pyt.* to ‘p1′@’localhost’;
4. create routine
必須具備create routine的權限,纔可使用{create |alter|drop} {procedure|function}
mysql> grant create routine on pyt.* to ‘p1′@’localhost’;
當授予create routine時,自動授予EXECUTE, ALTER ROUTINE權限給它的建立者:
mysql> show grants for ‘p1′@’localhost’;
+—————————————————————————+
Grants for p1@localhost
+————————————————————————–+
| GRANT USAGE ON *.* TO ‘p1′@’localhost’ IDENTIFIED BY PASSWORD ‘*23AE809DDACAF96AF0FD78ED04B6A265E05AA257′ |
| GRANT SELECT, CREATE, CREATE ROUTINE ON `pyt`.* TO ‘p1′@’localhost’|
| GRANT EXECUTE, ALTER ROUTINE ON PROCEDURE `pyt`.`pro_shop1` TO ‘p1′@’localhost’ |
+————————————————————————————-+
5. create temporary tables(注意這裏是tables,不是table)
必須有create temporary tables的權限,纔可使用create temporary tables.
mysql> grant create temporary tables on pyt.* to ‘p1′@’localhost’;
[mysql@mydev ~]$ mysql -h localhost -u p1 -p pyt
mysql> create temporary table tt1(id int);
6. create view
必須有create view的權限,纔可使用create view
mysql> grant create view on pyt.* to ‘p1′@’localhost’;
mysql> create view v_shop as select price from shop;
7. create user
要使用CREATE USER,必須擁有mysql數據庫的全局CREATE USER權限,或擁有INSERT權限。
mysql> grant create user on *.* to ‘p1′@’localhost’;
或:mysql> grant insert on *.* to p1@localhost;
8. insert
必須有insert的權限,纔可使用insert into ….. values….
9. alter
必須有alter的權限,纔可使用alter table
alter table shop modify dealer char(15);
10. alter routine
必須具備alter routine的權限,纔可使用{alter |drop} {procedure|function}
mysql>grant alter routine on pyt.* to ‘p1′@’ localhost ‘;
mysql> drop procedure pro_shop;
Query OK, 0 rows affected (0.00 sec)
mysql> revoke alter routine on pyt.* from ‘p1′@’localhost’;
[mysql@mydev ~]$ mysql -h localhost -u p1 -p pyt
mysql> drop procedure pro_shop;
ERROR 1370 (42000): alter routine command denied to user ‘p1′@’localhost’ for routine ‘pyt.pro_shop’
11. update
必須有update的權限,纔可使用update table
mysql> update shop set price=3.5 where article=0001 and dealer=’A';
12. delete
必須有delete的權限,纔可使用delete from ….where….(刪除表中的記錄)
13. drop
必須有drop的權限,纔可使用drop database db_name; drop table tab_name;
drop view vi_name; drop index in_name;
14. show database
經過show database只能看到你擁有的某些權限的數據庫,除非你擁有全局SHOW DATABASES權限。
對於p1@localhost用戶來講,沒有對mysql數據庫的權限,因此以此身份登錄查詢時,沒法看到mysql數據庫:
mysql> show databases;
+——————–+
| Database |
+——————–+
| information_schema|
| pyt |
| test |
+——————–+
15. show view
必須擁有show view權限,才能執行show create view。
mysql> grant show view on pyt.* to p1@localhost;
mysql> show create view v_shop;
16. index
必須擁有index權限,才能執行[create |drop] index
mysql> grant index on pyt.* to p1@localhost;
mysql> create index ix_shop on shop(article);
mysql> drop index ix_shop on shop;
17. excute
執行存在的Functions,Procedures
mysql> call pro_shop1(0001,@a);
+———+
| article |
+———+
| 0001 |
| 0001 |
+———+
mysql> select @a;
+——+
| @a |
+——+
| 2 |
+——+
18. lock tables
必須擁有lock tables權限,纔可使用lock tables
mysql> grant lock tables on pyt.* to p1@localhost;
mysql> lock tables a1 read;
mysql> unlock tables;
19. references
有了REFERENCES權限,用戶就能夠將其它表的一個字段做爲某一個表的外鍵約束。
20. reload
必須擁有reload權限,才能夠執行flush [tables | logs | privileges]
mysql> grant reload on pyt.* to p1@localhost;
ERROR 1221 (HY000): Incorrect usage of DB GRANT and GLOBAL PRIVILEGES
mysql> grant reload on *.* to ‘p1′@’localhost’;
Query OK, 0 rows affected (0.00 sec)
mysql> flush tables;
21. replication client
擁有此權限能夠查詢master server、slave server狀態。
mysql> show master status;
ERROR 1227 (42000): Access denied; you need the SUPER,REPLICATION CLIENT privilege for this operation
mysql> grant Replication client on *.* to p1@localhost;
或:mysql> grant super on *.* to p1@localhost;
mysql> show master status;
+——————+———-+————–+——————+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+——————+———-+————–+——————+
| mysql-bin.000006 | 2111 | | |
+——————+———-+————–+——————+
mysql> show slave status;
22. replication slave
擁有此權限能夠查看從服務器,從主服務器讀取二進制日誌。
mysql> show slave hosts;
ERROR 1227 (42000): Access denied; you need the REPLICATION SLAVE privilege for this operation
mysql> show binlog events;
ERROR 1227 (42000): Access denied; you need the REPLICATION SLAVE privilege for this operation
mysql> grant replication slave on *.* to p1@localhost;
mysql> show slave hosts;
Empty set (0.00 sec)
mysql>show binlog events;
+—————+——-+—————-+———–+————-+————–+
| Log_name | Pos | Event_type | Server_id| End_log_pos|Info |
+—————+——-+————–+———–+————-+—————+
| mysql-bin.000005 | 4 | Format_desc | 1 | 98 | Server ver: 5.0.77-log,
Binlog ver: 4 | |mysql-bin.000005|98|Query|1|197|use `mysql`; create
table a1(i int)engine=myisam|
……………………………………
23. Shutdown
關閉MySQL:
[mysql@mydev ~]$ mysqladmin shutdown
從新鏈接:
[mysql@mydev ~]$ mysql
ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/tmp/mysql.sock’ (2)
[mysql@mydev ~]$ cd /u01/mysql/bin
[mysql@mydev bin]$ ./mysqld_safe &
[mysql@mydev bin]$ mysql
24. grant option
擁有grant option,就能夠將本身擁有的權限授予其餘用戶(僅限於本身已經擁有的權限)
mysql> grant Grant option on pyt.* to p1@localhost;
mysql> grant select on pyt.* to p2@localhost;
25. file
擁有file權限才能夠執行 select ..into outfile和load data infile…操做,可是不要把file, process, super權限授予管理員之外的帳號,這樣存在嚴重的安全隱患。
mysql> grant file on *.* to p1@localhost;
mysql> load data infile ‘/home/mysql/pet.txt’ into table pet;
26. super
這個權限容許用戶終止任何查詢;修改全局變量的SET語句;使用CHANGE MASTER,PURGE MASTER LOGS。
mysql> grant super on *.* to p1@localhost;
mysql> purge master logs before ‘mysql-bin.000006′;
27. process
經過這個權限,用戶能夠執行SHOW PROCESSLIST和KILL命令。默認狀況下,每一個用戶均可以執行SHOW PROCESSLIST命令,可是隻能查詢本用戶的進程。
mysql> show processlist;
+—-+——+———–+——+———+——+——-+——————+
| Id | User | Host | db | Command | Time | State | Info |
+—-+——+———–+——+———+——+——-+——————+
| 12 | p1 | localhost | pyt | Query | 0 | NULL | show processlist |
+—-+——+———–+——+———+——+——-+——————+
另外,
管理權限(如 super, process, file等)不可以指定某個數據庫,on後面必須跟*.*
mysql> grant super on pyt.* to p1@localhost;
ERROR 1221 (HY000): Incorrect usage of DB GRANT and GLOBAL PRIVILEGES
mysql> grant super on *.* to p1@localhost;
Query OK, 0 rows affected (0.01 sec)