本次分享如何在Linux下安裝MySQL 5.7
操作系統版本:RedHat/CentOS 6.6 X64
MySQL版本:5.7.11
安裝方式:二進制
MySQL下載地址:http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.11-linux-glibc2.5-x86_64.tar.gz
安裝過程:
[root@gusha2 ~]# mkdir /data/dbwyzc -p
1、創建MySQL安裝目錄
2、創建一個不允許登錄的MySQL用戶及組
[root@gusha2 ~]# groupadd mysql
[root@gusha2 ~]# useradd -g mysql -s /sbin/nologin -d /data/mysql mysql
Creating mailbox file: File exists
[root@gusha2 ~]# id mysql
uid=500(mysql) gid=500(mysql) groups=500(mysql)
3、解壓下載下來的MySQL安裝包
[root@gusha2 ~]# mv /root/Desktop/mysql-5.7.11-linux-glibc2.5-x86_64.tar.gz /data/mysql/
[root@gusha2 ~]# cd /data/mysql/
[root@gusha2 mysql]# tar xf mysql-5.7.11-linux-glibc2.5-x86_64.tar.gz
[root@gusha2 mysql]# ls
mysql-5.7.11-linux-glibc2.5-x86_64 mysql-5.7.11-linux-glibc2.5-x86_64.tar.gz
做個軟連接到/usr/local/mysql
[root@gusha2 mysql]# cd /usr/local/
[root@gusha2 local]# ln -s /data/mysql/mysql-5.7.11-linux-glibc2.5-x86_64 mysql
[root@gusha2 local]# ls -l mysql
lrwxrwxrwx. 1 root root 46 Mar 24 02:43 mysql -> /data/mysql/mysql-5.7.11-linux-glibc2.5-x86_64
4、創建datadir及日誌存儲路徑
[root@gusha2 local]# cd /data/dbwyzc/
[root@gusha2 dbwyzc]# ls
[root@gusha2 dbwyzc]# mkdir {data,logs}
5、授權
[root@gusha2 dbwyzc]# chown -R mysql.mysql /data/dbwyzc/
[root@gusha2 dbwyzc]# chown -R mysql.mysql /usr/local/mysql/
6、因爲是選擇從MySQL壓縮包中初始化MySQL,不是直接安裝MySQL,需手動解決找不到mysql命令的問題
[root@gusha2 dbwyzc]# echo 'export PATH=$PATH:/usr/local/mysql/bin' >> /etc/profile
[root@gusha2 dbwyzc]# source !$
source /etc/profile
7、關閉防火牆及selinux
[root@gusha2 dbwyzc]# chkconfig iptables off
[root@gusha2 dbwyzc]# vi /etc/sysconfig/selinux
SELINUX=disabled
8、修改my.cnf
[root@gusha2 dbwyzc]# vi /etc/my.cnf
[client]
port=3306
socket=/tmp/mysql.sock
[mysqld]
user=mysql
basedir=/usr/local/mysql
datadir=/data/dbwyzc/data
socket=/tmp/mysql.sock
log-error=/data/dbwyzc/logs/mysqld.log
pid-file=/data/dbwyzc/mysqld.pid
9、初始化MySQL
在5.7中,推薦使用mysqld --initialize對數據庫進行初始化(mysql_install_db已經不再推薦使用),在初始化時如果加上--initialize-insecure,則會創建空密碼的 root@localhost 賬號,否則會創建帶密碼的 root@localhost 賬號,密碼直接寫在 log-error 日誌文件中
[root@gusha2 mysql]# cd bin/
[root@gusha2 bin]# mysqld --defaults-file=/etc/my.cnf --user=mysql --initialize --initialize-insecure
[root@gusha2 bin]# ls /data/dbwyzc/data/
auto.cnf ibdata1 ib_logfile1 performance_schema
ib_buffer_pool ib_logfile0 mysql sys
[root@gusha2 bin]# ls /data/dbwyzc/logs/
mysqld.log
[root@gusha2 bin]# cat /data/dbwyzc/logs/mysqld.log
2016-03-23T20:45:41.774933Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2016-03-23T20:45:43.665168Z 0 [Warning] InnoDB: New log files created, LSN=45790
2016-03-23T20:45:43.939168Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2016-03-23T20:45:44.025867Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 368bbcd5-f138-11e5-aad0-000c29f650ca.
2016-03-23T20:45:44.029664Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2016-03-23T20:45:44.031662Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
10、添加使用service快速啓動MySQL
[root@gusha2 bin]# cd ../
[root@gusha2 mysql]# ls support-files/
magic my-default.cnf mysqld_multi.server mysql-log-rotate mysql.server
[root@gusha2 mysql]# cp support-files/mysql.server /etc/init.d/mysql
[root@gusha2 mysql]# service mysql start
Starting MySQL. [ OK ]
之後給root用戶設置個密碼:
[root@gusha2 mysql]# mysql
mysql> set password=password('mysql123');
不要被上面繁瑣的操作步驟所迷惑,這種方法是我們搭建測試環境及MySQL多實例環境用到的最多的一種安裝方法,其操作對於熟練掌握Linux簡單命令的人來說已經非常簡單了,最重要的是,使用二進制的安裝方法,可以讓我們在安裝MySQL的過程中瞭解到MySQL各文件的位置、作用、MySQL初始化的時候都做了哪些工作等。
更多精彩MySQL內容 請關注我哦!