目前官網(wǎng)僅存在 5.7 和 8.0 兩個版本的在線文檔,5.6 的PDF文檔。
https://dev.mysql.com/doc/refman/5.7/en/
https://dev.mysql.com/doc/refman/8.0/en/
https://downloads.mysql.com/docs/refman-5.6-en.pdf
一、軟件包準備
歷史版本下載地址 https://downloads.mysql.com/archives/community/
wget https://cdn.mysql.com/archives/mysql-5.6/mysql-5.6.51-linux-glibc2.12-x86_64.tar.gz
tar xzvf mysql-5.6.51-linux-glibc2.12-x86_64.tar.gz
| Directory | Contents of Directory |
|---|---|
| bin, scripts | mysqld server, client and utility programs |
| data | Log files, databases |
| docs | MySQL manual in Info format |
| include | Include (header) files |
| lib | Libraries |
| mysql-test | Test suite |
| man | Unix manual pages |
| share | Error messages, dictionary, and SQL for database installation |
| sql-bench | Benchmarks |
| support-files | Miscellaneous support files, including sample configuration files |
安裝依賴
yum install libaio
二、簡單運行
- 創(chuàng)建運行用戶
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
- 可選解壓路徑
cd /usr/local
tar zxvf /path/to/mysql-VERSION-OS.tar.gz
ln -s full-path-to-mysql-VERSION-OS mysql
- 創(chuàng)建表
# 執(zhí)行腳本需要安裝依賴
yum install perl-Data-Dumper.x86_64
# This scripts creates the MySQL Server system tables
cd mysql
scripts/mysql_install_db --user=mysql
mysql_install_db 在安裝目錄創(chuàng)建默認配置文件 my.cnf ,該文件源自安裝包的 my-default.cnf 模板。
mysql_install_db 安裝數(shù)據(jù)文件到 /var/lib/mysql/。
如果使用自定義配置文件,在創(chuàng)建表的時候,請增加
--defaults-file配置
- 啟動 mysql
bin/mysqld_safe --user=mysql &
--defaults-file argument to mysqld_safe when starting the server
You can test the MySQL daemon with
mysql-test-run.pl,執(zhí)行cd mysql-test ; perl mysql-test-run.pl
- 配置文件(可選)
# Next command is optional
cp support-files/mysql.server /etc/init.d/mysql.server
三、設置 root 用戶密碼
設置MySQL root用戶的密碼
./bin/mysqladmin -u root password 'new-password'
./bin/mysqladmin -u root -h localhost.localdomain password 'new-password'
生產(chǎn)環(huán)境建議
./bin/mysql_secure_installation
四、啟動 - 高級配置
# cd mysql_installation_directory
bin/mysqld_safe &
bin/mysqld_safe --user=mysql &
bin/mysqld_safe --defaults-file=/usr/local/mysql/my.cnf
默認配置下,需要手動創(chuàng)建
--log-error和--pid-file文件,并修改用戶權限為 mysql。
非默認配置,自行配置。
執(zhí)行 bin/mysqld_safe 啟動 mysql 服務 , 進程顯示如下
/root/mysql-5.6.51-linux-glibc2.12-x86_64/bin/mysqld \
--basedir=/root/mysql-5.6.51-linux-glibc2.12-x86_64 \
--datadir=/var/lib/mysql \
--plugin-dir=/root/mysql-5.6.51-linux-glibc2.12-x86_64/lib/plugin \
--user=mysql \
--log-error=/var/log/mariadb/mariadb.log \
--pid-file=/var/run/mariadb/mariadb.pid \
--socket=/var/lib/mysql/mysql.sock
如上默認配置都可以在 my.cnf 內(nèi)進行修改
- 參考PDF文檔 4.3.3 mysql.server — MySQL Server Startup Script
將support-files/mysql.server(MySQL daemon start/stop script)拷貝到對應位置
mysql.server start
mysql.server stop
cp mysql.server /etc/init.d/mysql
chmod +x /etc/init.d/mysql
chkconfig --add mysql
chkconfig --level 345 mysql on
五、配置文件說明
參考 http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
參考 4.2.2.2 Using Option Files
參考 MariaDB option-file-syntax
https://cloud.tencent.com/developer/article/1533528
范例
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
local-infile=1
query_cache_limit=4M
query_cache_size=256M
query_cache_type=1
max_connections=400
interactive_timeout=30
wait_timeout=30
connect_timeout=10
thread_cache_size=128
key_buffer=200M
join_buffer=1M
max_allowed_packet=16M
table_cache=26536
sort_buffer_size=1M
read_buffer_size=1M
read_rnd_buffer_size=1M
max_connect_errors=10
myisam_sort_buffer_size=64M
tmp_table_size=256M
max_heap_table_size=256M
innodb_buffer_pool_size=2000M
innodb_flush_log_at_trx_commit=2
innodb_file_per_table
[mysqldump]
quick
quote-names
max_allowed_packet = 16M
[mysql.server]
user=mysql
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
Option file Sections:
配置文件常見 Section - mysqld, mysql
| Section | Description | Other |
|---|---|---|
| [mysqld] | 服務器參數(shù) [server options] | |
| [mysqld_safe] | define server settings like MySQL user, pid file and error log | MySQL和MariaDB都接受 |
| [mysql.server] | define server settings like MySQL user, pid file and error log | 逐漸過時 |
| [mysqldump] | 備份參數(shù) [mysqldump options] | |
| [mysql] | 命令行參數(shù) [mysql and client options] | |
| [client] | 客戶端參數(shù),包含 mysql cli [mysql and client options] | |
| [mysqladmin] | mysqladmin | |
| [mysqlhotcopy] | mysqlhotcopy |
mysqld
mysqld是關于服務器端的程序,要想使用客戶端程序,該程序必須運行,因為客戶端通過連接服務器來訪問數(shù)據(jù)庫。
mysql_safe
mysql服務啟動腳本,它是mysqld的父進程,它調(diào)用mysqld啟動數(shù)據(jù)庫服務,并在啟動MySQL服務器后繼續(xù)監(jiān)控其運行情況,并在其死機時重新啟動它。
使用mysqld_safe命令可以防止mysql服務的意外終止。
mysql.server
服務器啟動的shell腳本,主要作用就是為了方便啟動和關閉mysql服務,它使用mysql_safe來啟動mysql服務器。
常用配置文件
[mysqld]
datadir = /mysql_data/data
user = mysql
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[mysqld_safe]
log-error = /mysql_data/mysql.log
pid-file = /mysql_data/mysql.pid
自定義配置,需要執(zhí)行
scripts/mysql_install_db --defaults-file=my.cnf安裝表文件到指定data路徑。
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
[mysqld]
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
# basedir = .....
# datadir = .....
# port = .....
# server_id = .....
# socket = .....
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES