標籤:
查看資料庫伺服器的版本
select version();
建立資料庫
create database dbname character_set=utf8 collate=utf8_general_ci
刪除資料庫
drop database dbname;
選擇使用的資料庫
mysql>use dbname;
察看資料庫中存在的表名
mysql>show tables;
mysql>show tables from schema_name;
建立資料庫表
create table tbname (column_name column_type contraints,......)
mysql中的表,是以檔案的形式儲存在檔案夾下的。
/var/lib/mysql/表名.frm
建立表時,可以為每個欄位指定不同的字元集和collation(定序)
察看建立表的ddl語句
show create table tbname \G;
刪除表
drop table tbname;
修改表
alter table tbname modify column_name newtype;
alter table tbname add column_name column_type;
alter table tbname drop column colname;
alter table tbname change col_name_old col_name_new;
alter table tbname add col_name col_type after col_name2;
alter table tbname add col_name col_type before col_name2;
alter table tbname modify age init(3) first;
alter table tbname rename tbname_new;
收回insert許可權
grant select, insert on test.* to ‘uname‘@‘localhost‘ identified by ‘pwd‘;
revoke insert on test.* from ‘uname‘@‘localhost‘;
察看系統變數
show variables like ‘time_zone‘;
修改mysql的時區
set time_zone=‘+9:00‘;
資料類型
如果表示年月日:date
年月日時分秒 :datetime
時分秒 : time
系統時間 :timestamp
只表示年 :year
MySQL的用戶端工具
1. mysql
沒有進入mysql的用戶端程式時,可以使用下面的方式執行sql文。
mysq < xxx.sql (初始化參數可以在c:/windows/my.ini裡進行配置)
使用重新導向的方式,當sql出錯時會中斷下面的執行。為了繼續執行檔案中 後面的內容。可以通過添加force選項來實現。
mysql --force < xxxx.sql
my.ini
----------------------
[client]
user=root
password=xxxxx
進入mysql的用戶端後,可以執行在.sql檔案的內容。如果sql中有錯誤的內容需要當發生錯誤的時候終止下面內容的執行.則啟動mysql用戶端的時候加上下面的參數。mysql --abort-source-on-error
mysql>source xxxx.sql;
mysql -e "show databases"; (不用登陸,直接存取)
離開mysql用戶端:exit quit \q
改變資料庫:use dbname | \u dbname (注意後面沒有任何符號)
1. mysqladmin
mysqladmin create dbname
mysqladmin drop dbname
mysqladmin extended-status
mysqladmin password pwd
mysqladmin ping --p --host=xxx.xxx.xxx.xx
mysqladmin status --sleep=1 --count=2
mysqladmin status --debug-info
字元集的設定
http://www.nowamagic.net/librarys/veda/detail/138
常見問題:
ERROR 1366 (HY000): Incorrect string value
保證資料庫字元集和建表的字元集一樣,並支援輸入的字元.
資料庫層級指定字元集
在my.cnf檔案中進行設點字元集,並且在建資料庫和表時保持一致。
my.cnf中設定以下幾個屬性值就好了。
[client] default-character-set=utf8 [mysql] default-character-set=utf8 [mysqld] character_set_server=utf8 character_set_filesystem=utf8 |
通過設定系統變數來設定預設的字元集。
set character_set_server=utf8
set collation_server=utf8_general_bi
個別資料庫指定字元集
建立資料庫的時候指定字元集和collation,這樣建立表的時候,如果不指定,就用建立資料庫指定的字元集和collation來建立表。
show variables 和 set 命令,預設是session層級的命令。
show session variables
show global variables
set global xxx=yyy
set @@global xxx=yyy
set session xxx=yyy
set @@session xxx=yyy
1)查看預設的字元集
SHOW VARIABLES LIKE ‘character%‘
SHOW VARIABLES LIKE ‘collation%‘
2)修改字元集
2.1>在伺服器層級修改字元集 (修改my.cnf檔案或者通過命令列進行設點)
2.2>在資料庫的層級修改字元集(建立資料庫的時候指定)
set global | session character_set_server=utf8
使用set命令設定的變數,當資料庫重新啟動後就失效了。
SHOW VARIABLES LIKE ‘character%‘
SHOW VARIABLES LIKE ‘collation%‘
Set character_set_system=utf8 (always set this value, because mysql server default)
set character_set_server=utf8
set character_set_results=utf8
set character_set_database=utf8
set character_set_connection=utf8 當前connect傳輸的字元集
set character_set_client=utf8 用戶端使用的字元集的設定
set character_set_results=utf8
set collation_connection=utf8_general_ci
set collation_database=utf8_general_ci
set collation_server=utf8_general_ci
SHOW VARIABLES LIKE ‘character%‘
SHOW VARIABLES LIKE ‘collation%‘
Set character_set_system=utf8 (always set this value, because mysql server default)
set character_set_server=utf8
set character_set_results=utf8
set character_set_database=utf8
set character_set_connection=utf8 當前connect傳輸的字元集
set character_set_client=utf8 用戶端使用的字元集的設定
set character_set_results=utf8
set collation_connection=utf8_general_ci
set collation_database=utf8_general_ci
set collation_server=utf8_general_ci
如果用戶端使用的是ASCII串連到資料庫,但想把所有的statement轉化為utf8的行式,
這樣character_set_client=ASCII
character_set_connection=utf8
character_set_result:返回給用戶端的字元集結果
通常情況下,character_set_client和character_set_result應該設成一致的。
SELECT collation_name, is_default FROM information_schema.collations WHERE character_set_name = ‘UTF8‘;
MySQL日常管理