1. 程式人生 > >linux上安裝mysql5.6

linux上安裝mysql5.6

eps bison 自動 without memory oot 成了 into utf

CentOS-6.6+MySQL-5.6

部署環境
操作系統:CentOS-6.6-x86_64-bin-DVD1.iso
MySQL 版本:mysql-5.6.26.tar.gz
操作用戶:root
系統 IP:192.168.1.26
主機名:mysql-01
建議配置:4 核 + 、4G 內存 +

1、使用下面的命令檢查是否安裝有 MySQL Server:

# rpm -qa | grep mysql
mysql-libs-5.1.73-3.el6_5.x86_64


如果是 CentOS7 以上,請使用以下命令查看:
# rpm -qa | grep mariadb
mariadb-libs-5.5.41-2.el7_0.x86_64
(因為沒有 MySQL 服務,因此沒必要卸載。mysql-libs 是 MySQL 的必要包)


(如果有的話可通過下面的命令來卸載掉,例如:rpm -ev mariadb-libs-5.5.52-1.el7.x86_64 //需要按包的依賴關系依次刪除)

[root@DB-Server init.d]# service mysql status
MySQL running (25673)[ OK ]
[root@DB-Server init.d]# service mysql stop
Shutting down MySQL..[ OK ]
[root@DB-Server init.d]# service mysql status
MySQL is not running[FAILED]

2、改防火墻設置,打開 3306 端口:

# vi /etc/sysconfig/iptables
增加如下行:
## MySQL
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT

重啟防火墻:
# service iptables restart

3、新增 mysql 用戶組:

# groupadd mysql

4、新增 mysql 用戶,並添加到 mysql 用戶組:
# useradd -r -g mysql mysql

5、新建 MySQL 執行文件目錄(後面會把編譯好的 mysql 程序安裝到這個目錄):

# mkdir -p /usr/local/mysql
(-p 參數的作用是:如果最終目錄的父目錄不存在也會一並創建)

6、新建 MySQL 數據庫數據文件目錄:

# mkdir -p /home/mysql/data
# mkdir -p /home/mysql/logs
# mkdir -p /home/mysql/temp
(註意:上面的 logs 及 temp 目錄是為了以後將 MySQL 的數據文件與執行程序文件分離,
如果你打算設置到不同的路徑,註意修改對應的執行命令和數據庫初始化腳本。正式生產環
境,建議數據目錄和日誌目錄都使用單獨的分區來掛載,不同分區屬於不同的磁盤或磁盤
組。)

7、增加 PATH 環境變量搜索路徑:

# vi /etc/profile
在 profile 文件末尾增加兩行
## mysql env param
PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
export PATH

使 PATH 搜索路徑立即生效:
# source /etc/profile

8、安裝編譯 MySQL 需要的依賴包:

(mysql 從 5.5 版本開始,不再使用./configure 編譯,而是使用 cmake 編譯器,具體的
cmake 編譯參數可以參考 mysql 官網文檔
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 -y install make cmake gcc gcc-c++ bison bison-devel ncurses ncurses-devel autoconf automake

9、進入/usr/local/src 目錄,上傳 mysql-5.6.26.tar.gz 源代碼到/usr/local/src 目錄:
# cd /usr/local/src
# rz

10、開始編譯安裝 mysql-5.6.26:

解壓縮源碼包:
# tar -zxvf mysql-5.6.26.tar.gz
進入解壓縮源碼目錄:
# rm -rf mysql-5.6.26.tar.gz
# cd mysql-5.6.26

使用 cmake 源碼安裝 mysql(如果你打算安裝到不同的路徑,註意修改下面語句中/usr/local/mysql 和/home/mysql/data 路徑!)
創建編譯配置:
# 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

上面的這些復制完,回車,然後就開始 cmake 的過程。


配置解釋:
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql 設置安裝目錄
-DMYSQL_DATADIR=/home/mysql/data 設置數據庫存放目錄
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock 設置 UNIX socket 目錄
-DMYSQL_USER=mysql 設置運行用戶
-DDEFAULT_CHARSET=utf8 設置默認字符集,默認 latin1
-DEFAULT_COLLATION=utf8_general_ci 設置默認校對規則,默認 latin1_general_ci
-DWITH_INNOBASE_STORAGE_ENGINE=1 添加 InnoDB 引擎支持
-DENABLE_DOWNLOADS=1 自動下載可選文件,比如自動下載谷歌的測試包
-DMYSQL_TCP_PORT=3306 設置服務器監聽端口,默認 3306
-DSYSCONFDIR=/etc 設置 my.cnf 所在目錄,默認為安裝目錄)

執行過程中會出現:
CMake Error: Problem with tar_extract_all(): Invalid argument
CMake Error: Problem extracting tar: /usr/local/src/mysql-5.6.26/source_downloads/gmock-1.6.0.zip
解決方法:
cd mysql 目錄下面會發現有一個 source_downloads 目錄,需要解壓 unzip gmock-1.6.0.zip,然
後再重新執行上述配置過程。當然你也可以去掉-DENABLE_DOWNLOADS=1 這個選項,不編
譯谷歌的測試包也沒有什麽問題,但是之前的某些版本會出現無法編譯的問題.

11、cmake 結束後開始編譯源碼:

# make

12、安裝編譯好的程序:

# make install
(註意:如果需要重裝 mysql, 在 /usr/local/src/mysql-5.6.26 在執行下 make install 就可以了,
不需要再 cmake 和 make)

13、清除安裝臨時文件:

# make clean

14、修改 mysql 目錄擁有者為 mysql 用戶:

# chown -Rf mysql:mysql /usr/local/mysql
# chown -Rf mysql:mysql /home/mysql

15、進入 mysql 執行程序的安裝路徑:

# cd /usr/local/mysql

16、執行初始化配置腳本,創建系統自帶的數據庫和表

(註意:路徑/home/mysql/data 需要換成你自定定義的數據庫存放路徑):
# scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/home/mysql/data

17、初始化腳本在/usr/local/mysql/下生成了配置文件 my.cnf,需要更改該配置文件的所有者:
# ls -lah

# chown -Rf mysql:mysql /usr/local/mysql/my.cnf

18、註意:
(1)Tips:在啟動 MySQL 服務時,會按照一定次序搜索 my.cnf,先在/etc 目錄下找,找不
到則會搜索 mysql 程序目錄下是否有 my.cnf
(2)需要註意 CentOS 6 版操作系統的最小安裝完成後,即使沒有安裝 mysql,在/etc 目錄
下也會存在一個 my.cnf 文件,建議將此文件更名為其他的名字,否則該文件會幹擾源碼安
裝的 MySQL 的正確配置,造成無法啟動。修改/etc/my.cnf 操作如下:
可以:mv /etc/my.cnf /etc/my.cnf.bak
也可以:刪除掉/etc/my.cnf 這個文件:rm /etc/my.cnf

如果你需要用於生產環境,不要急著做下面的 mysql 啟動操作。建議把上一步驟中 mysql 初
始化生成的/usr/local/mysql/my.cnf 刪除,然後把你優化好的 mysql 配置文件 my.cnf 放到/etc
下。(這是做 mysql 主從復制和 mysql 優化的經驗!)

(我們這裏使用/etc/my.cnf)

19、編輯/etc/my.cnf:

# vi /etc/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=1000
max_connect_errors = 6000
wait_timeout=605800

#open_tables = 600
#table_cache = 650
#opened_tables = 630

max_allowed_packet = 64M

sort_buffer_size = 8M
join_buffer_size = 8M
thread_cache_size = 300

query_cache_type = 1
query_cache_size = 512M
query_cache_limit = 2M
query_cache_min_res_unit = 16k

tmp_table_size = 512M
max_heap_table_size = 512M

key_buffer_size = 512M
read_buffer_size = 2M
read_rnd_buffer_size = 32M
bulk_insert_buffer_size = 128M

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


20、復制服務啟動腳本:

# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
或者:(以下語句是跳過提示直接覆蓋)
# /bin/cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql

21、啟動 MySQL 服務:

# service mysql start
Starting MySQL.. SUCCESS!
(初次啟動會在/usr/local/mysql 目錄下生成 mysql.sock 文件)


22、設置 MySQL 開機自動啟動服務:

# chkconfig mysql on

設置 MySQL 數據庫 root 用戶的本地登錄密碼(初始用戶沒有密碼):
# mysqladmin -u root password ‘abc#$123‘

23、登錄並修改 MySQL 用戶 root 的密碼:

# mysql -uroot -p

Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.26-log Source distribution

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

mysql>

############## 特別註意:mysql> 命令模式下,後面的分號“;”不能少################
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

修改 root 用戶密碼:
mysql> update user set Password = password(‘abc#$123‘) where User=‘root‘;
mysql> flush privileges;
mysql>GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘%‘ IDENTIFIED BY ‘abc#$123‘ WITH GRANT OPTION;

all代表接受所有操作,比如 select,insert,delete....;
*.* 代表所有庫下面的所有表;
% 代表這個用戶允許從任何地方登錄,為了安全期間,這個%可以替換為你允許的ip地址;

mysql> flush privileges;
mysql> exit;
-------------允許 root 遠程登錄,設置遠程登錄密碼:abc#$123------------


###################註意:真實生產環境,應用操作不要使用 root 用戶,建議操作如下:###################

mysql> create user oxygen identified by ‘123456‘;
mysql> grant all privileges on *.* to ‘oxygen‘@‘%‘ identified by ‘123456‘ with grant option;
mysql> flush privileges;
mysql> exit;

重新登錄
[root@edu-mysql-01 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.6.26-log Source distribution

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

mysql>

24、運行安全設置腳本,強烈建議生產服務器使用(可選):
[root@edu-mysql-01 ~]# /usr/local/mysql/bin/mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!

In order to log into MySQL to secure it, we‘ll need the current
password for the root user. If you‘ve just installed MySQL, and
you haven‘t set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): ----->此處輸入 root 密碼
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

You already have a root password set, so you can safely answer ‘n‘.

Change the root password? [Y/n] n -----> 上已為 root 設置了密碼,此處可輸 n
... skipping.

By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] Y ------> 刪除匿名用戶
... Success!

Normally, root should only be allowed to connect from ‘localhost‘. This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n -----> 一般不允許 root 遠程登錄, 可 添加普通用戶,
然後設置允許遠程登錄
... skipping.

By default, MySQL comes with a database named ‘test‘ that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] Y -----> 刪除 test 庫及相應權限
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y -----> 重新加載權限表使設置生效
... Success!

All done! If you‘ve completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!
Cleaning up...

25、重啟服務器,檢測 mysql 是否能開機自動啟動:
[root@edu-mysql-01 ~] # reboot

linux上安裝mysql5.6