本文記錄在 CentOS 6.5 上使用 YUM 快速安裝 MySQL 社區版的全流程,涵蓋服務管理、控制台操作、存儲引擎查看與切換、賬號授權、表結構變更等入門必備技能。
# 1. 安裝
yum install -y mysql-server mysql mysql-devel
# 2. 啟動服務
service mysqld start
# 首次啟動輸出初始化信息,提示設置 root 密碼:
# /usr/bin/mysqladmin -u root password 'new-password'
# /usr/bin/mysql_secure_installation # 推薦運行此向導
# 登錄
mysql -u root -p
# 查看存儲引擎
SHOW ENGINES;
-- Engine | Support | Comment | Transactions | XA | Savepoints
-- MyISAM | DEFAULT | Default engine ... | NO | NO | NO
-- InnoDB | YES | Supports transactions... | YES | YES | YES
# 查看數據庫
SHOW DATABASES;
-- information_schema, mysql, test
# 切換庫、查看表、表結構
USE test;
SHOW TABLES;
DESC db; -- 或 SHOW COLUMNS FROM db
-- 建表(默認 MyISAM)
CREATE TABLE user_test (id INT, sex CHAR(2));
-- 指定引擎建表
CREATE TABLE user_test_innodb (id INT, sex CHAR(2)) ENGINE=InnoDB;
-- 修改現有表引擎
ALTER TABLE user_test ENGINE=CSV;
-- ERROR 1178: CSV engine doesn't support nullable columns
-- 原因:CSV 引擎要求所有列 NOT NULL
ALTER TABLE user_test ENGINE=MEMORY;
-- Query OK, 0 rows affected
-- MEMORY 表:哈希索引、純內存、重啟數據丟失、適合臨時表
SHOW TABLE STATUS LIKE 'host'\G
-- Name: host
-- Engine: MyISAM
-- Version: 10
-- Row_format: Fixed
-- Rows: 0
-- Data_length: 0
-- Index_length: 2048
-- Collation: utf8_bin
-- Comment: Host privileges; Merged with database privileges
-- 創建用戶並授權(老版本語法,5.7+ 建議 CREATE USER + GRANT 分離)
GRANT ALL PRIVILEGES ON db_name.* TO 'user'@'host' IDENTIFIED BY 'pass';
FLUSH PRIVILEGES;
| 項目 | CentOS 6 | CentOS 7 |
|---|---|---|
| 服務管理 | service mysqld start/stop | systemctl start/stop mysqld |
| 默認 MySQL 版本 | 5.1/5.5 | 5.6/5.7 (MariaDB 替代) |
| 防火牆 | iptables | firewalld |
| SELinux | 同 | 同 |