安裝MySQL5.7系列

機器環境: Centos 7 64位
安裝目錄: /usr/local/mysql
啟動腳本: /etc/init.d/mysqld
數據目錄: /data/mysql
安裝版本: 5.7.17
安裝過程: 源碼

1.安裝依賴

#yum install –y autoconf automake imake libxml2-devel expat-devel cmake gcc gcc-c++ libaio libaio-devel bzr bison libtool ncurses5-devel ncurses-devle

2.檢查你所用的Linux下有沒有安裝過mysql,沒有卸載干凈

#rpm -qa|grep -i mysql
mysql-5.7.13-linux-glibc2.5-x86_64

*可見已經安裝了庫文件,應該先卸載,不然會出現覆蓋錯誤。注意卸:載時使用了--nodeps選項,忽略了依賴關系:
#rpm -e mysql-5.7.13-linux-glibc2.5-x86_64 --nodeps

當然你的也可能不止這一個文件,也可能有多個,那么你就依次 rpm -e xx --nodeps 卸載,卸載完了再檢查一下,若確定刪除干凈后,在經行下面步驟。

  1. 創建mysql的用戶組/用戶, data目錄及其用戶目錄在這步之前一定要先確保你所建的用戶以及用戶組沒有存在,要不然在后面的過程中會報錯,刪除時候要先刪除用戶在刪除用戶組名。
# groupadd mysql                                          # 創建一個名為mysql的用戶組       
#useradd -r -g mysql -s /bin/false mysql       # 在用戶組下創建用戶
#mkdir -p /data/mysql                   #創建data目錄
#chown -R mysql.mysql /data/mysql       #給data目錄授權

4.安裝boost庫|檢查編譯環境|編譯安裝

將文件上傳到服務器上
#tar -zxvf mysql-boost-5.7.17.tar.gz   -C /usr/local/src  # 解壓boost庫文件(這里解壓出來的目錄名和mysql-5.7.17.tar.gz解壓出來的目錄名一樣)
#cp –a /usr/local/src/mysql-5.7.17/ /usr/local/boost
# cmake -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/boost             # 編譯boost庫

#tar -zxvf mysql-5.7.17.tar.gz #解壓數據庫文件
#cd  mysql-5.7.17 
執行cmake命令
#cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DENABLED_LOCAL_INFILE=ON \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DCOMPILATION_COMMENT='Zhaofx for mysqltest' \
-DWITH_READLINE=ON \
-DSYSCONFDIR=/data/mysql \
-DWITH_BOOST=/usr/local/boost \
-DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock

在cmake完成后,執行下面的命令進行編譯與安裝:
# make  && make install       ---開始編譯 && 編譯安裝
編譯安裝后,可以確認mysql軟件目錄:
# chown -R mysql:mysql /usr/local/mysql/
# ls -la /usr/local/mysql/
#編譯過程中出錯,清除臨時文件重新編譯:
#make clean
#rm CMakeCache.txt


安裝完后把可執行文件路徑加到系統環境變量:
#echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
#source /etc/profile
創建軟鏈接
# ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
#使用默認的配置文件(好像沒有默認配置文件,略過)
#cd /usr/local/mysql/support-files
#cp my-default.cnf ../my.cnf

5.初始化數據庫
#/usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql #執行初始化

[Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2016-04-08T01:47:59.945537Z 0
[Warning] InnoDB: New log files created, LSN=45790 2016-04-08T01:48:00.333528Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2016-04-08T01:48:00.434908Z 0
[Warning] No existing UUID has been found, so we assume that this is the first timethat this server has been started. Generating a new UUID: ece26421-fd2b-11e5-a1e3-00163e001e5c. 2016-04-08T01:48:00.440125Z 0
[Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2016-04-08T01:48:00.440904Z 1
[Note] A temporary password is generated for root@localhost: **mjT,#x_5sW

mysqld --initialize安裝的MySQL部署默認是安全的,以下更改已作為默認部署特征實現:
· 安裝過程中只創建一個root賬號,'root'@'localhost'自動為該賬戶生成隨機密碼,并標記密碼為已過期狀態,MySQL管理員必須用root的隨機密碼進行連接并設置新密碼。(服務器將隨機密碼寫入錯誤日志)
· 安裝不會創建匿名用戶賬號
· 安裝不會創建test數據庫
關于error log的位置,如果安裝的是RPM包,則默認是/var/log/mysqld.log。

6.檢測下是否能啟動mysql服務

復制啟動腳本:
#cd /usr/local/mysql/support-files
#cp mysql.server /etc/init.d/mysql
直接啟動
#mysqld_safe --user=mysql &
或
#/etc/init.d/mysqld start
或
#service mysql start
Starting MySQL.. OK!
這是正常的情況下。

如果安全腳本用不了則跳過,進入數據庫進行更改密碼
啟動后調用安全腳本mysql_secure_installation,實現:更改root密碼,刪除測試庫,禁止root遠程登錄。通過這幾項的設置能夠提高mysql庫的安全
運行mysql_secure_installation會執行幾個設置:
a)為root用戶設置密碼|Y,設置密碼
b)刪除匿名賬號|Enter
c)取消root用戶遠程登錄|Y
d)刪除test庫和對test庫的訪問權限|Enter
e)刷新授權表使修改生效|Enter

7.創建配置文件

#設置編碼,可按需修改新的配置文件選項, 不修改配置選項, mysql則按默認配置參數運行. 
如下是我修改配置文件/etc/my.cnf, 設置編碼為utf8以防亂碼
# vim /etc/my.cnf
 
[mysqld]
basedir = /usr/local/mysql
datadir = /data/mysql
character_set_server=utf8
init_connect='SET NAMES utf8'
max_connections=1000
  
[client]
default-character-set=utf8

參數說明:
character_set_server=utf8 //設置編碼為utf8以防亂碼
max_connections=16384 //設置最大連接數為16384(數據庫最大限制)

8.配置mysql服務開機自動啟動

#cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql # 拷貝啟動文件到/etc/init.d/下并重命令為mysqld
# chmod 755 /etc/init.d/mysqld                                      # 增加執行權限
# chkconfig --list mysqld                                           # 檢查自啟動項列表中沒有mysqld這個
# chkconfig --add mysqld                                            # 如果沒有就添加mysqld
# chkconfig mysqld on                                               # 用這個命令設置開機啟動

服務的啟動/重啟/停止

service mysqld start # 啟動服務
service mysqld restart # 重啟服務
service mysqld stop # 停止服務

9.初始化mysql用戶root的密碼

[root@linux mysql]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 27
Server version: 5.7.19 Zhaofx for mysqltest

Copyright (c) 2000, 2017, 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>use mysql;               ---會提示沒有權限
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.    
mysql> alter user 'root'@'localhost' identified by 'root';
Query OK, 0 rows affected (0.04 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
 
Database changed
mysql> update user set authentication_string = PASSWORD('123456') where user = 'root';
Query OK, 1 row affected, 1 warning (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 1

10.設置遠程登錄授權

[root@linux ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.13 MySQL Community Server (GPL)
 
Copyright (c) 2000, 2016, 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> grant all privileges on *.* to 'root'@'%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
 
mysql>
或者
grant all on *.* to 'root'@'%' identified by '123456'; 

如果不設置遠程連接,用mysql數據庫連接工具可是連接不上的

11.用數據庫連接工具來測試你剛剛的安裝是不是成功了
首先要知道,遠程連接的Linux系統的ip,確保本機已經能夠ping通的情況下再連接。
...
到這里說明mysql安裝成功!

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容