檢查資料庫大小
SELECT table_name, table_rows, ((index_length+data_length)/1024) AS size FROM information_schema.tables WHERE table_schema = 'database_name';
匯入表單
mysql db_name < xxx.sql -u root -p --default-character-set=utf8
匯出表單
mysqldump --default-character-set=utf8 -u root -p db_name > xxx.sql
mysqldump --default-character-set=utf8 -u root -p db_name --skip-extended-insert > xxx.sql
-- 上方的語法可以將 insert 的內容逐行分離
建立資料庫
CREATE DATABASE db_name DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
建立資料表
CREATE TABLE table_name (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
`name` VARCHAR(100) CHRACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT 'default' COMMENT 'your comment',
`fk` INT UNSIGNED NOT NULL DEFAULT 0 COMMENT 'foreign key',
`created_at` TIMESTAMP NULL DEFAULT NULL,
`updated_at` TIMESTAMP NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
INDEX(`fk`)
) ENGINE=MYISAM CHARACTER SET utf8 COLLATE utf8_unicode_ci COMMENT 'your comment';
修改資料表名稱
RENAME TABLE old-name TO new-name
使用者
SELECT user, host FROM mysql.user;
SHOW GRANTS;
SHOW GRANTS FOR 'user'@'%'; -- 檢視 user@% 權限
CREATE USER 'user'@'%' IDENTIFIED BY 'password'; -- 新增使用者
ALTER USER 'user'@'example.org' IDENTIFIED BY 'new_password'; -- 修改密碼
GRANT ALL ON database_name.* TO 'user'@'%'; -- 賦予權限,使用 *.* 表示套用全部資料庫
GRANT ALL ON database_name.* TO 'user'@'%' WITH GRANT OPTION; --讓自身擁有的權限賦予該會員
REVOKE ALL ON database_name.* FROM 'user'@'%'; -- 移除權限
DROP USER 'user'@'%'; -- 移除使用者
GRANT USAGE ON *.* TO 'user'@'%' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
設定密碼
mysqladmin -u root password 'new_password'
複製表格
CREATE TABLE new_table LIKE old_table;
INSERT new_table SELECT * FROM old_table;