部署環(huán)境
操作系統(tǒng): CentOS-6.6-x86_64
MySQL 版本: mysql-5.6.26
操作用戶: root
系統(tǒng) IP: 192.168.175.200
主機(jī)名: huachao
服務(wù)器配置:
- 配置網(wǎng)絡(luò)
vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
UUID=1de4c8e4-15ed-4309-b2b6-2247c3d680ca
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
NAME="System eth0"
HWADDR=00:0C:29:AD:06:D2
IPADDR=192.168.175.200
GATEWAY=192.168.175.2
NETMASK=255.255.255.0
DNS1=192.168.175.2
- 設(shè)置主機(jī)名
# vi /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=huachao
- 設(shè)置 IP 與主機(jī)名的映射
# vi /etc/hosts
127.0.0.1 huachao
192.168.1.205 huachao
- 兩臺(tái)數(shù)據(jù)庫(kù)服務(wù)器的的 selinux 都要 disable
(永久關(guān)閉 selinux豺裆,請(qǐng)修改/etc/selinux/config进副,將 SELINUX 改為 disabled)
# vi /etc/selinux/config
SELINUX=disabled
- 重啟操作系統(tǒng)
# reboot
源碼安裝 MySQL5.6.26:
- 使用下面的命令檢查是否安裝有 MySQL Server:
# rpm -qa | grep mysql
mysql-libs-5.1.73-3.el6_5.x86_64
如果是 CentOS7 以上役拴, 請(qǐng)使用以下命令查看:
# rpm -qa | grep mariadb
mariadb-libs-5.5.41-2.el7_0.x86_64
(因?yàn)闆]有 MySQL 服務(wù)灿里,因此沒必要卸載翘地。 mysql-libs
是 MySQL 的必要包)
( 如果有的話可通過下面的命令來卸載掉, rpm -e mysql
//普通刪除模式)
- 改防火墻設(shè)置桶至,打開 3306 端口:
# vi /etc/sysconfig/iptables
增加如下行:
## MySQL
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
重啟防火墻:
# service iptables restart
- 新增 mysql 用戶組:
# groupadd mysql
- 新增 mysql 用戶具壮, 并添加到 mysql 用戶組:
# useradd -r -g mysql mysql
- 新建 MySQL 執(zhí)行文件目錄(后面會(huì)把編譯好的 mysql 程序安裝到這個(gè)目錄):
# mkdir -p /usr/local/mysql
( -p 參數(shù)的作用是: 如果最終目錄的父目錄不存在也會(huì)一并創(chuàng)建)
- 新建 MySQL 數(shù)據(jù)庫(kù)數(shù)據(jù)文件目錄:
# mkdir -p /home/mysql/data
# mkdir -p /home/mysql/logs
# mkdir -p /home/mysql/temp
( 注意:上面的 logs 及 temp 目錄是為了以后將 MySQL 的數(shù)據(jù)文件與執(zhí)行程序文件分離,如果你打算設(shè)置到不同的路徑梯刚,注意修改對(duì)應(yīng)的執(zhí)行命令和數(shù)據(jù)庫(kù)初始化腳本凉馆。 正式生產(chǎn)環(huán)境,建議數(shù)據(jù)目錄和日志目錄都使用單獨(dú)的分區(qū)來掛載亡资, 不同分區(qū)屬于不同的磁盤或磁盤組澜共。)
- 增加 PATH 環(huán)境變量搜索路徑:
# vi /etc/profile
在 profile 文件末尾增加兩行
# mysql env param
PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
export PATH
使 PATH 搜索路徑立即生效:
# source /etc/profile
安裝編譯 MySQL 需要的依賴包:
( mysql 從 5.5 版本開始,不再使用./configure 編譯锥腻,而是使用 cmake 編譯器嗦董,具體的
cmake 編譯參數(shù)可以參考 mysql 官網(wǎng)文檔http://dev.mysql.com/doc/refman/5.5/en/source-configuration-options.html
, 安裝基本依賴包瘦黑,先用 yum 安裝 cmake京革、 automake 奇唤、 autoconf ,另 MySQL 5.5.x 需要最少安裝的包有: bison,gcc匹摇、 gcc-c++咬扇、 ncurses-devel):
# yum install make cmake gcc gcc-c++ bison bison-devel ncurses ncurses-devel autoconf automake
進(jìn)入/usr/local/src 目錄,上傳 mysql-5.6.26.tar.gz 源代碼到/usr/local/src 目錄:
# cd /usr/local/src
- 開始編譯安裝 mysql-5.6.26:
解壓縮源碼包:
# tar -zxvf mysql-5.6.26.tar.gz
進(jìn)入解壓縮源碼目錄:
# cd mysql-5.6.26
使用 cmake 源碼安裝 mysql(如果你打算安裝到不同的路徑来惧,注意修改下面語句中/usr/local/mysql 和/home/mysql/data
路徑!)
[root@huachao mysql-5.6.26]# cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_DATADIR=/home/mysql/data \
-DMYSQL_USER=mysql \
-DMYSQL_TCP_PORT=3306 \
-DENABLE_DOWNLOADS=1
上面的這些復(fù)制完演顾,回車供搀,然后就開始 cmake 的過程,一般時(shí)間不會(huì)很長(zhǎng)钠至。
配置解釋:
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql 設(shè)置安裝目錄
-DMYSQL_DATADIR=/home/mysql/data 設(shè)置數(shù)據(jù)庫(kù)存放目錄
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock 設(shè)置 UNIX socket 目錄
-DMYSQL_USER=mysql 設(shè)置運(yùn)行用戶
-DDEFAULT_CHARSET=utf8 設(shè)置默認(rèn)字符集葛虐,默認(rèn) latin1
-DEFAULT_COLLATION=utf8_general_ci 設(shè)置默認(rèn)校對(duì)規(guī)則,默認(rèn) latin1_general_ci
-DWITH_INNOBASE_STORAGE_ENGINE=1 添加 InnoDB 引擎支持
-DENABLE_DOWNLOADS=1 自動(dòng)下載可選文件棉钧, 比如自動(dòng)下載谷歌的測(cè)試包
-DMYSQL_TCP_PORT=3306 設(shè)置服務(wù)器監(jiān)聽端口屿脐,默認(rèn) 3306
-DSYSCONFDIR=/etc 設(shè)置 my.cnf 所在目錄,默認(rèn)為安裝目錄)
執(zhí)行過程中會(huì)出現(xiàn):
CMake Error: Problem with tar_extract_all(): Invalid argument
CMake Error: Problem extracting tar: /usr/local/src/mysql-5.6.26/source_downloads/gmoc k-1.6.0.zip
- 解決方法:
??cd mysql 目錄下面會(huì)發(fā)現(xiàn)有一個(gè) source_downloads 目錄宪卿,需要解壓 unzip gmock-1.6.0.zip,然后再重新執(zhí)行上述配置過程的诵。當(dāng)然你也可以去掉-DENABLE_DOWNLOADS=1 這個(gè)選項(xiàng),不編譯谷歌的測(cè)試包也沒有什么問題佑钾,但是之前的某些版本會(huì)出現(xiàn)無法編譯的問題.
- cmake 結(jié)束后開始編譯源碼西疤,這一步時(shí)間會(huì)較長(zhǎng),請(qǐng)耐心等待:
# make
- 安裝編譯好的程序:
# make install
( 注意:如果需要重裝 mysql休溶,在/usr/local/src/mysql-5.6.26 在執(zhí)行下 make install 就可以了代赁,不需要再 cmake 和 make)
- 清除安裝臨時(shí)文件:
# make clean
- 修改 mysql 目錄擁有者為 mysql 用戶:
# chown -Rf mysql:mysql /usr/local/mysql
# chown -Rf mysql:mysql /home/mysql
- 進(jìn)入 mysql 執(zhí)行程序的安裝路徑:
# cd /usr/local/mysql
- 執(zhí)行初始化配置腳本,創(chuàng)建系統(tǒng)自帶的數(shù)據(jù)庫(kù)和表(注意:路徑/home/mysql/data 需要
換成你自定定義的數(shù)據(jù)庫(kù)存放路徑):
# scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/home/mysql/data
Installing MySQL system tables...2015-12-13 15:21:53 0 [Warning] TIMESTAMP with implicit
DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see
documentation for more details).
2015-12-13 15:21:53 0 [Note] /usr/local/mysql/bin/mysqld (mysqld 5.6.26) starting as process
17362 ...
2015-12-13 15:21:53 17362 [Note] InnoDB: Using atomics to ref count buffer pool pages
2015-12-13 15:21:53 17362 [Note] InnoDB: The InnoDB memory heap is disabled
2015-12-13 15:21:53 17362 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2015-12-13 15:21:53 17362 [Note] InnoDB: Memory barrier is not used
2015-12-13 15:21:53 17362 [Note] InnoDB: Compressed tables use zlib 1.2.3
2015-12-13 15:21:53 17362 [Note] InnoDB: Using CPU crc32 instructions
2015-12-13 15:21:53 17362 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2015-12-13 15:21:53 17362 [Note] InnoDB: Completed initialization of buffer pool
2015-12-13 15:21:53 17362 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a
new database to be created!
2015-12-13 15:21:53 17362 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB
2015-12-13 15:21:53 17362 [Note] InnoDB: Database physically writes the file full: wait...
2015-12-13 15:21:53 17362 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB
2015-12-13 15:21:53 17362 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB
2015-12-13 15:21:53 17362 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
2015-12-13 15:21:53 17362 [Warning] InnoDB: New log files created, LSN=45781
2015-12-13 15:21:53 17362 [Note] InnoDB: Doublewrite buffer not found: creating new
2015-12-13 15:21:53 17362 [Note] InnoDB: Doublewrite buffer created
2015-12-13 15:21:53 17362 [Note] InnoDB: 128 rollback segment(s) are active.
2015-12-13 15:21:53 17362 [Warning] InnoDB: Creating foreign key constraint system tables.
2015-12-13 15:21:53 17362 [Note] InnoDB: Foreign key constraint system tables created
2015-12-13 15:21:53 17362 [Note] InnoDB: Creating tablespace and datafile system tables.
2015-12-13 15:21:53 17362 [Note] InnoDB: Tablespace and datafile system tables created.
2015-12-13 15:21:53 17362 [Note] InnoDB: Waiting for purge to start
2015-12-13 15:21:53 17362 [Note] InnoDB: 5.6.26 started; log sequence number 0
2015-12-13 15:21:53 17362 [Note] Binlog end
2015-12-13 15:21:53 17362 [Note] InnoDB: FTS optimize thread exiting.
2015-12-13 15:21:53 17362 [Note] InnoDB: Starting shutdown...
2015-12-13 15:21:54 17362 [Note] InnoDB: Shutdown completed; log sequence number 1625977
OK
Filling help tables...2015-12-13 15:21:54 0 [Warning] TIMESTAMP with implicit DEFAULT value is
deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for
more details).
2015-12-13 15:21:54 0 [Note] /usr/local/mysql/bin/mysqld (mysqld 5.6.26) starting as process
17384 ...
2015-12-13 15:21:54 17384 [Note] InnoDB: Using atomics to ref count buffer pool pages
2015-12-13 15:21:54 17384 [Note] InnoDB: The InnoDB memory heap is disabled
2015-12-13 15:21:54 17384 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2015-12-13 15:21:54 17384 [Note] InnoDB: Memory barrier is not used
2015-12-13 15:21:54 17384 [Note] InnoDB: Compressed tables use zlib 1.2.3
2015-12-13 15:21:54 17384 [Note] InnoDB: Using CPU crc32 instructions
2015-12-13 15:21:54 17384 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2015-12-13 15:21:54 17384 [Note] InnoDB: Completed initialization of buffer pool
2015-12-13 15:21:54 17384 [Note] InnoDB: Highest supported file format is Barracuda.
2015-12-13 15:21:54 17384 [Note] InnoDB: 128 rollback segment(s) are active.
2015-12-13 15:21:54 17384 [Note] InnoDB: Waiting for purge to start
2015-12-13 15:21:54 17384 [Note] InnoDB: 5.6.26 started; log sequence number 1625977
2015-12-13 15:21:55 17384 [Note] Binlog end
2015-12-13 15:21:55 17384 [Note] InnoDB: FTS optimize thread exiting.
2015-12-13 15:21:55 17384 [Note] InnoDB: Starting shutdown...
2015-12-13 15:21:56 17384 [Note] InnoDB: Shutdown completed; log sequence number 1625987
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/local/mysql/bin/mysqladmin -u root password 'new-password'
/usr/local/mysql/bin/mysqladmin -u root -h edu-mysql-02 password 'new-password'
Alternatively you can run:
/usr/local/mysql/bin/mysql_secure_installation
which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the manual for more instructions.
You can start the MySQL daemon with:
cd . ; /usr/local/mysql/bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd mysql-test ; perl mysql-test-run.pl
Please report any problems at http://bugs.mysql.com/
The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
New default config file was created as /usr/local/mysql/my.cnf and
will be used by default by the server when you start it.
You may edit this file to change server settings
WARNING: Default config file /etc/my.cnf exists on the system
This file will be read by default by the MySQL server
If you do not want to use this, either remove it, or use the
--defaults-file argument to mysqld_safe when starting the server
- 初始化腳本在/usr/local/mysql/下生成了配置文件 my.cnf兽掰,需要更改該配置文件的所有者:
# ls -lah
[root@huachao mysql] # chown -Rf mysql:mysql /usr/local/mysql/my.cnf
- 注意:
( 1) Tips:在啟動(dòng) MySQL 服務(wù)時(shí)芭碍,會(huì)按照一定次序搜索 my.cnf,先在/etc 目錄下找孽尽,找不到則會(huì)搜索 mysql 程序目錄下是否有 my.cnf
( 2)需要注意 CentOS 6 版操作系統(tǒng)的最小安裝完成后窖壕,即使沒有安裝 mysql,在/etc 目錄下也會(huì)存在一個(gè) my.cnf 文件杉女,建議將此文件更名為其他的名字艇拍,否則該文件會(huì)干擾源碼安裝的 MySQL 的正確配置,造成無法啟動(dòng)宠纯。修改/etc/my.cnf 操作如下:
可以:mv /etc/my.cnf /etc/my.cnf.bak
也可以: 刪除掉/etc/my.cnf
這個(gè)文件:rm /etc/my.cnf
如果你需要用于生產(chǎn)環(huán)境卸夕,不要急著做下面的 mysql 啟動(dòng)操作。建議把上一步驟中 mysql 初
如果你需要用于生產(chǎn)環(huán)境婆瓜,不要急著做下面的 mysql 啟動(dòng)操作快集。建議把上一步驟中 mysql 初始化生成的/usr/local/mysql/my.cnf
刪除贡羔,然后把你優(yōu)化好的 mysql 配置文件 my.cnf 放到/etc下。(這是做 mysql 主從復(fù)制和 mysql 優(yōu)化的經(jīng)驗(yàn)8龀酢)
(我們這里使用/etc/my.cnf)
- 編輯
/etc/my.cnf:
# vi my.cnf
[client]
port = 3306
socket = /usr/local/mysql/mysql.sock
[mysqld]
character-set-server = utf8
collation-server = utf8_general_ci
skip-external-locking
skip-name-resolve
user = mysql
port = 3306
basedir = /usr/local/mysql
datadir = /home/mysql/data
tmpdir = /home/mysql/temp
# server_id = .....
socket = /usr/local/mysql/mysql.sock
log-error = /home/mysql/logs/mysql_error.log
pid-file = /home/mysql/mysql.pid
open_files_limit = 10240
back_log = 600
max_connections=500
max_connect_errors = 6000
wait_timeout=605800
#open_tables = 600
#table_cache = 650
#opened_tables = 630
max_allowed_packet = 32M
sort_buffer_size = 4M
join_buffer_size = 4M
thread_cache_size = 300
query_cache_type = 1
query_cache_size = 256M
query_cache_limit = 2M
query_cache_min_res_unit = 16k
tmp_table_size = 256M
max_heap_table_size = 256M
key_buffer_size = 256M
read_buffer_size = 1M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 64M
lower_case_table_names=1
default-storage-engine = INNODB
innodb_buffer_pool_size = 2G
innodb_log_buffer_size = 32M
innodb_log_file_size = 128M
innodb_flush_method = O_DIRECT
#####################
thread_concurrency = 32
long_query_time= 2
slow-query-log = on
slow-query-log-file = /home/mysql/logs/mysql-slow.log
[mysqldump]
quick
max_allowed_packet = 32M
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
- 復(fù)制服務(wù)啟動(dòng)腳本:
# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
- 啟動(dòng) MySQL 服務(wù):
# service mysql start
Starting MySQL.. SUCCESS!
(初次啟動(dòng)會(huì)在/usr/local/mysql
目錄下生成 mysql.sock
文件)
- 設(shè)置 MySQL 開機(jī)自動(dòng)啟動(dòng)服務(wù):
# chkconfig mysql on
設(shè)置 MySQL 數(shù)據(jù)庫(kù) root 用戶的本地登錄密碼(初始用戶沒有密碼):
# mysqladmin -u root password 'roncoo'
- 登錄并修改 MySQL 用戶 root 的密碼:
# mysql -uroot -p
update user set Password = password('要設(shè)置的密碼') where User='root';
use mysql;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'www.roncoo.com' WITH GRANT OPTION;
flush privileges;
exit;
- 運(yùn)行安全設(shè)置腳本乖寒, 強(qiáng)烈建議生產(chǎn)服務(wù)器使用(可選):
[root@huachao ~]# /usr/local/mysql/bin/mysql_secure_installation
- 重啟服務(wù)器,檢測(cè) mysql 是否能開機(jī)自動(dòng)啟動(dòng):
[root@huachao ~] # reboot