系統版本:ubuntu 12.04 64位
安裝(測試時使用的版本是mysql-server-5.5):
apt-get install mysql-server
啟動和停止等:
/etc/init.d/mysql start
/etc/init.d/mysql restart
/etc/init.d/mysql stop
本地串連:
mysql -uname -p
更改mysql編碼(比如改為utf8)
進入mysql,查看當前編碼:
mysql> show variables like 'character%';
顯示如下:
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
可以看到建立一個表是latin1的格式。
退出mysql,修改設定檔:
/etc/mysql/my.cnf
添加:
[client]
default-character-set=utf8
[mysqld]
character-set-server=utf8
//default-character-set=utf8 //mysql版本5以前用這句
修改後需要重啟mysql:
/etc/init.d/mysql restart #啟動成功
再次進入mysql查看:
mysql> show variables like 'character%';
顯示如下:
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
修改成功。
*注意:需要重新建立資料庫和表才會生效,老資料庫中的資料編碼不會改變。
更改mysql資料庫位置:
資料庫檔案預設儲存位置在/var/lib/mysql。
MySQL資料庫的檔案結構是怎麼樣的呢,對於這樣子建立的資料庫命令:
CREATE DATABASE mysqldb
在預設的資料庫儲存位置下就會有個檔案夾mysqldb。要操作資料庫首先得停止資料庫進程:
$sudo /etc/init.d/mysql stop
本文以轉移到/home/mysql這個位置為例,下面命令將原有資料庫轉移到新位置:
$sudo cp –R –p /var/lib/mysql /home/mysql
編輯MySQL設定檔:
$sudo vim /etc/mysql/my.cnf
找到datadir這一行,將後面等號之後的內容更改為/home/mysql然後儲存退出。
自Ubuntu 7.10開始,Ubuntu就開始使用一種安全軟體叫做AppArmor,這個安全軟體會在你的檔案系統中建立一個允許應用程式訪問的地區(專業術語:應 用程式存取控制)。如果不為MySQL修改AppArmor設定檔,永遠也無法為新設定的資料庫儲存位置啟動資料庫服務。
配置AppArmor:
$sudo vim /etc/apparmor.d/usr.sbin.mysqld
找到/var/lib/mysql/這兩行,注釋掉這兩行(在這兩行前分別添加一個符號“#”即可注釋),在這兩行之前或之後加上下面內容:
/home/mysql/ r,
/home/mysql/** rwk,
儲存後退出,執行命令:
$sudo /etc/init.d/apparmor reload
等待顯示Reloading AppArmor profiles : done.即可以重啟MySQL服務:
$sudo /etc/init.d/mysql start
至此MySQL資料庫儲存位置就更改完畢了。
可能遇到的問題:
Q:當我運行$sudo /etc/init.d/apparmor reload卻返回資訊
Skipping profile /etc/apparmor.d/usr.sbin.mysqld~
: Warning.
並且也無法啟動MySQL服務,怎麼辦呢?
A:出現這個問題可能是因為你使用了$sudo gedit或$gksu gedit這樣子的命令來編輯usr.sbin.mysqld這個設定檔的,這兩個圖形介面文字編輯器會在相同位置產生一個 usr.sbin.mysqld~的檔案,從而影響了AppArmor讀取設定檔,出現這個問題先刪除這個usr.sbin.mysqld~檔案:
$sudo rm /etc/apparmor.d/usr.sbin.mysqld~
然後使用$sudo vim這個終端文字編輯器來編輯設定檔即可。
遠程連結mysql資料庫
進入mysql,執行如下命令:
格式:grant 許可權 on 資料庫名.表名 to 使用者@登入主機 identified by "使用者密碼";
例如:
grant select,update,insert,delete on *.* to root@192.168.1.12 identified by "passwd";
如果登入主機(host欄位)的值為%就表示可以在任何用戶端機器上能以root使用者登入到mysql伺服器。可用如下命令修改:
update user set host = ’%’ where user = ’root’;
授權許可權改為ALL PRIVILEGES表示對所有操作授權,也可在授權時使用如下命令:
grant all privileges on *.* to root@'%' identified by "passwd";
查看目前使用者許可權:
use mysql;
select host,user,password from user;
修改授權後,需要重新整理授權緩衝,以便授權生效。
FLUSH PRIVILEGES;
注意:遠程連結需要修改 /etc/mysql/my.cnf 檔案,
找到:bind-address = 127.0.0.1
修改為:bind-address = 0.0.0.0 或者 注釋掉
重啟mysql : sudo /etc/init.d/mysql restart
*使用用戶端連結時(standard(tcp/ip)連結),default schema項需要填寫資料庫名稱(必須在伺服器上已存在)
匯入匯出mysql資料
匯入mysql:
mysql> source name.sql(路徑);
匯出mysql:
使用mysqlworkbench用戶端-》data export。
mysql使用者管理
假設使用者名稱:xoneday 使用者資料庫:blog
1.建立使用者。
//登入MYSQL
@>mysql -u root -p
@>密碼
//建立使用者
mysql> insert into mysql.user(Host,User,Password) values("localhost”,”xoneday”,password("1234"));
//重新整理系統許可權表
mysql>flush privileges;
這樣就建立了一個名為:xoneday 密碼為:1234 的使用者。
然後登入一下。
mysql>exit;
@>mysql -u xoneday -p
@>輸入密碼
mysql>登入成功
2.為使用者授權。
//登入MYSQL(有ROOT許可權)。我裡我以ROOT身份登入.
@>mysql -u root -p
@>密碼
//首先為使用者建立一個資料庫(blog)
mysql>create database blog;
//授權xoneday使用者擁有blog資料庫的所有許可權。
>grant all privileges on blog.* to xoneday@localhost identified by '1234';
//重新整理系統許可權表
mysql>flush privileges;
mysql>其它操作
/*
如果想指定部分許可權給一使用者,可以這樣來寫:
mysql>grant select,update on blog.* to xoneday@localhost identified by '1234';
//重新整理系統許可權表。
mysql>flush privileges;
*/
3.刪除使用者。
@>mysql -u root -p
@>密碼
mysql>DELETE FROM user WHERE User=“xoneday” and Host="localhost";
mysql>flush privileges;
//刪除使用者的資料庫
mysql>drop database blog;
4.修改指定使用者密碼。
@>mysql -u root -p
@>密碼
mysql>update mysql.user set password=password('新密碼') where User="xoneday" and Host="localhost";
mysql>flush privileges;
查看mysql資料庫表大小(儲存空間):
假設我們要查詢的表格儲存體在:
資料庫名:zcdata
表名:zctable
1: 先進入MySQL內建管理庫:information_schema
mysql> use information_schema;
2: 查詢大小,顯示資料位元組數
mysql> select data_length,index_length
from tables where
table_schema='zcdata'
and table_name = 'zctable';
3: 查詢大小,已MB為單位顯示
mysql> select concat(round(sum(data_length/1024/1024),2),'MB') as data_length_MB,
oncat(round(sum(index_length/1024/1024),2),'MB') as index_length_MB
from tables where
table_schema='zcdata'
and table_name = 'zctable';
常見錯誤:
mysqldump: Got error: 1045:
解決(假設使用root使用者):
mysql -uroot -p
mysql> use mysql;
mysql> update user set password=password('new password') where user='root';
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4 Changed: 4 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
mysqldump: Got error: 1044:
解決:
加上-skip-lock-tables選項即可。即:
mysqldump -u dbuser -ppass db --skip-lock-tables > db.sql
使用mysqlworkbench用戶端時,
選擇-進階-其他-取消-lock