Linux上安裝二進制文件MySQL詳解


前言:昨天晚上搞了很久,終於搞清楚mysql的安裝配置了,我真是太low了。當我在雲服務器上登進Mysql時,真是高興哈哈,咱一步一步來,徹底搞懂Mysql的安裝配置。

我的安裝環境: 阿里雲服務器

[root@iz2zeezp920l8adg8sf0pz ~]# cat /etc/redhat-release 
CentOS Linux release 7.3.1611 (Core) 
[root@iz2zeezp920l8adg8sf0pz ~]# 

 

一、下載二進制包

訪問官網,無論學習啥工具,官方文檔是最有學習價值的。現在用谷哥翻譯英文網頁成中文,也基本能看懂的。

因為商用版本需要付費的,所以我下載的是MySQL社區版(免費)。點擊之后出現下圖,再點擊MySQL社區下載,下載MySQL服務器。

MySQL社區版是世界上最流行的開源數據庫的免費下載版本,由開源開發人員和愛好者的活躍社區支持。上方的Development Releases(發展版)意思是正在開發測試中的版本,就不要下載了。下載已經發布的版本。操作系統我是雲服務器,所以下載的的Linux-Generic(Linux-通用)版本。

我的服務器版本是64位的,點擊下載:

 

二、上傳安裝包至服務器

 下載成功后,進入/usr/local/目錄,將安裝包上傳至雲服務器:

[root@iz2zeezp920l8adg80sf0pz local]# rz
rz waiting to receive.
 zmodem trl+C ȡ

  100%  626100 KB  465 KB/s 00:22:24       0 Errorsbc2.12-x86_64.tar.gz...

[root@iz2zeezp920l8adg80sf0pz local]# ls
mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
[root@iz2zeezp920l8adg80sf0pz local]# 

借助securtCRT(我是用CRT,你也可以用xshell),使用linux命令sz可以很方便的將服務器上的文件下載到本地,使用rz命令則是把本地文件上傳到服務器

其中,對於sz和rz的理解與記憶我用了如下的方法(因為很多時候容易搞混):

  • sz中的s意為send(發送),告訴客戶端,我(服務器)要發送文件 send to cilent,就等同於客戶端在下載。
  • rz中的r意為received(接收),告訴客戶端,我(服務器)要接收文件 received by cilent,就等同於客戶端在上傳。

注意:sz和rz命令需要服務器支持,如果提示沒有個命令:則需要先安裝rzsz。如下:

[root@iz2zeezp920l8adg80sf0pz tools]# yum install lrzsz -y
Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile Resolving Dependencies --> Running transaction check ---> Package lrzsz.x86_64 0:0.12.20-36.el7 will be installed --> Finished Dependency Resolution

 

三、查看官方文檔

官方文檔: https://dev.mysql.com/doc/refman/5.7/en/binary-installation.html

Important(英文)
  • MySQL has a dependency on the libaio library. Data directory initialization and subsequent server startup steps will fail if this library is not installed locally. If necessary, install it using the appropriate package manager. For example, on Yum-based systems:

    shell> yum search libaio  # search for info
    shell> yum install libaio # install library

    Or, on APT-based systems:

    shell> apt-cache search libaio # search for info
    shell> apt-get install libaio1 # install library
  • For MySQL 5.7.19 and later: Support for Non-Uniform Memory Access (NUMA) has been added to the generic Linux build, which has a dependency now on the libnuma library; if the library has not been installed on your system, use you system's package manager to search for and install it (see the last bullet for some sample commands).

To install a compressed tar file binary distribution, unpack it at the installation location you choose (typically /usr/local/mysql). This creates the directories shown in the following table.

Table 2.3 MySQL Installation Layout for Generic Unix/Linux Binary Package

Directory Contents of Directory
bin mysqld server, client and utility programs
docs MySQL manual in Info format
man Unix manual pages
include Include (header) files
lib Libraries
share Error messages, dictionary, and SQL for database installation
support-files Miscellaneous support files
重要(中文)
  • MySQL依賴於libaio 庫。如果此庫未在本地安裝,則數據目錄初始化和后續服務器啟動步驟將失敗。如有必要,請使用相應的軟件包管理器進行安裝。例如,基於Yum的系統:

    shell> yum search libaio  # search for info
    shell> yum install libaio # install library

    或者,在基於APT的系統上:

    shell> apt-cache search libaio # search for info
    shell> apt-get install libaio1 # install library
  • 對於MySQL 5.7.19及更高版本支持非均勻內存訪問(NUMA)已​​被添加到通用Linux構建中,該構建現在依賴於libnuma 庫; 如果您的系統上沒有安裝庫,請使用系統的軟件包管理器來搜索並安裝它(有關示例命令,請參閱最后一個項目符號)。

要安裝壓縮的tar文件二進制版本,請在您選擇的安裝位置(通常/usr/local/mysql)將其解壓縮這將創建下表中顯示的目錄。

表2.3通用Unix / Linux二進制包的MySQL安裝布局

目錄 目錄內容
bin mysqld服務器,客戶端和實用程序
docs 信息格式的MySQL手冊
man Unix手冊頁
include 包含(標題)文件
lib 圖書館
share 用於數據庫安裝的錯誤消息,字典和SQL
support-files 其他支持文件

由於我下載的是最新版的mysql,根據官方文檔,我需要先安裝libaio。那就安裝唄,反正很簡單,官網也寫得很清楚。

 

四、解壓前配置

先看官方文檔是怎樣說的:

要安裝和使用MySQL二進制分發,命令序列如下所示:

shell> groupadd mysql
shell> useradd -r -g mysql -s /bin/false mysql
shell> cd /usr/local
shell> tar zxvf /path/to/mysql-VERSION-OS.tar.gz
shell> ln -s full-path-to-mysql-VERSION-OS mysql
shell> cd mysql
shell> mkdir mysql-files
shell> chown mysql:mysql mysql-files
shell> chmod 750 mysql-files
shell> bin/mysqld --initialize --user=mysql 
shell> bin/mysql_ssl_rsa_setup
shell> bin/mysqld_safe --user=mysql &
# Next command is optional
shell> cp support-files/mysql.server /etc/init.d/mysql.server
Note

This procedure assumes that you have root (administrator) access to your system.(此過程假定您具有root 訪問您的系統) Alternatively, you can prefix each command using the sudo (Linux) or pfexec (Solaris) command.

The mysql-files directory provides a convenient location to use as the value for the secure_file_priv system variable, which limits import and export operations to a specific directory.(將導入和導出操作限制到特定目錄) See Section 5.1.5, “Server System Variables”.

上面的知識點挺多的,如果你已經有安裝經驗,那對你很有用。對於我這種小白來說,並不是很友好,就算命令執行了,那命令執行的具體操作,又是否明白呢?接下來一步一步說明

 

五、步驟詳解

shell> groupadd mysql shell> useradd -r -g mysql -s /bin/false mysql
因為用戶僅僅是為了所有權需要而不是登錄目的,所以useradd命令使用 -r-s /bin/false選項來創建一個不具有對服務器主機的登錄權限的用戶。如果您的useradd不支持這些選項,請忽略這些選項。

1、接下來,創建一個到tar創建的安裝目錄的符號鏈接:

[root@iz2zeezp920l8adg80sf0pz local]# tar zxvf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
[root@iz2zeezp920l8adg80sf0pz local]# ln -s mysql-5.7.20-linux-glibc2.12-x86_64 mysql

ln命令創建一個到安裝目錄的符號鏈接。這使您能夠更容易地將其引用為/usr/local/mysql。為避免在使用MySQL時始終鍵入客戶端程序的路徑名,您可以將該/usr/local/mysql/bin 目錄添加到PATH變量中(當然,當你重新登入系統時,發現PATH變量的更改又變為原來的!! 如何永久更改PATH變量呢?后面再講):

shell> export PATH=$PATH:/usr/local/mysql/bin

 

2、在mysql目錄下創建mysql-files目錄:

[root@iz2zeezp920l8adg80sf0pz local]# cd mysql
[root@iz2zeezp920l8adg80sf0pz mysql]# mkdir mysql-files # 默認是沒有這個文件夾的,用來放數據,當然網上很多人是用data目錄,我這里為了保持與官網一致,用mysql-files
[root@iz2zeezp920l8adg80sf0pz mysql]# chown mysql:mysql mysql-files
[root@iz2zeezp920l8adg80sf0pz mysql]# chmod 750 mysql-files # 添加權限
[root@iz2zeezp920l8adg80sf0pz mysql]# ll | grep "mysql-files"
drwxr-x---  5 mysql mysql  4096 Oct 30 00:24 mysql-files [root@iz2zeezp920l8adg80sf0pz mysql]# 

3、初始化:

[root@iz2zeezp920l8adg80sf0pz mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/mysql-files
2017-10-29T16:05:31.856327Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2017-10-29T16:05:33.906980Z 0 [Warning] InnoDB: New log files created, LSN=45790
2017-10-29T16:05:34.133920Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2017-10-29T16:05:34.249408Z 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: fecb2929-bcc2-11e7-b26d-00163e062422. 2017-10-29T16:05:34.251580Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2017-10-29T16:05:34.252052Z 1 [Note] A temporary password is generated for root@localhost: y#cee=T_v6B9
[root@iz2zeezp920l8adg80sf0pz mysql]# 

此處需要注意記錄生成的臨時密碼,之后登陸數據庫會用到,如上文:y#cee=T_v6B9

 

4、修改系統配置文件

將support-files文件夾下的my-default.cnf,mysql.server復制到/etc和/etc/init.d下(最新版已經沒有my-default.cnf文件,可以自己按格式建立一個)

[root@iz2zeezp920l8adg80sf0pz mysql]# cd support-files/
[root@iz2zeezp920l8adg80sf0pz support-files]# ls
magic mysqld_multi.server mysql.server my-default.cnf  mysql-log-rotate

這個是時候我們自己創建一個(內部代碼如圖): 重點下面兩個socket(紅色)對應的值必須一致,不然啟動以后鏈接不上(參考自:http://www.cnblogs.com/fengyun1853/p/7441791.html)

#dvice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.

[mysqld]

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 

# 一般配置選項
basedir = /usr/local/mysql
datadir = /usr/local/mysql/mysql-files
port = 3306
#socket = /temp/mysqld.sock
socket = /var/run/mysqld/mysqld.sock
character-set-server=utf8


#下面是可選項,要不要都行,如果出現啟動錯誤,則全部注釋掉,保留最基本的配置選項,然后嘗試添加某些配置項后啟動,檢測配置項是否有誤
back_log = 300
max_connections = 3000
max_connect_errors = 50
table_open_cache = 4096
max_allowed_packet = 32M
#binlog_cache_size = 4M

max_heap_table_size = 128M
read_rnd_buffer_size = 16M
sort_buffer_size = 16M
join_buffer_size = 16M
thread_cache_size = 16
query_cache_size = 128M
query_cache_limit = 4M
ft_min_word_len = 8

thread_stack = 512K
transaction_isolation = REPEATABLE-READ
tmp_table_size = 128M
#log-bin=mysql-bin
long_query_time = 6


server_id=1

innodb_buffer_pool_size = 1G
innodb_thread_concurrency = 16
innodb_log_buffer_size = 16M


innodb_log_file_size = 512M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
innodb_file_per_table = on

[mysqldump]
quick

max_allowed_packet = 32M

[mysql]
no-auto-rehash
socket = /var/run/mysqld/mysqld.sock
default-character-set=utf8
safe-updates


[myisamchk]
key_buffer = 16M
sort_buffer_size = 16M
read_buffer = 8M
write_buffer = 8M

[mysqlhotcopy]
interactive-timeout

[mysqld_safe]
open-files-limit = 8192

[client]
loose-default-character-set = utf8

查看安裝路徑與數據路徑:

[root@iz2zeezp920l8adg80sf0pz support-files]# cat my-default.cnf | grep "basedir"
basedir = /usr/local/mysql [root@iz2zeezp920l8adg80sf0pz support-files]# cat my-default.cnf | grep "datadir"
datadir = /usr/local/mysql/mysql-files [root@iz2zeezp920l8adg80sf0pz support-files]# cp my-default.cnf /etc/my.cnf
[root@iz2zeezp920l8adg80sf0pz support-files]# [root@iz2zeezp920l8adg80sf0pz support-files]# cp mysql.server /etc/init.d/mysql

5、修改配置文件:

[root@iz2zeezp920l8adg80sf0pz support-files]# vim /etc/init.d/mysql

basedir='/usr/local/mysql'
datadir='/usr/local/mysql/mysql-files'

注意必須得加引號!!不然會出錯的。

 

6、啟動服務:

[root@iz2zeezp920l8adg80sf0pz support-files]# ../bin/mysqld_safe --user=mysql &
[1] 29215 [root@iz2zeezp920l8adg80sf0pz support-files]# Logging to '/usr/local/mysql/mysql-files/iz2zeezp920l8adg80sf0pz.err'.
2017-10-29T16:15:45.012169Z mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists.
 [1]+  Exit 1                  ../bin/mysqld_safe --user=mysql [root@iz2zeezp920l8adg80sf0pz support-files]# 

出現錯誤了,'/var/run/mysqld' for UNIX socket file don't exists.

這是因為我在my-default.cnf寫了以下配置:

socket = /var/run/mysqld/mysqld.sock

而/var/run/目錄下根本沒有mysqld目錄,解決方法如下:

[root@iz2zeezp920l8adg80sf0pz run]# ls
atd.pid crond.pid lock ntpd.pid setrans tuned auditd.pid cron.reboot log plymouth sshd.pid udev blkid dbus mount ppp syslogd.pid user cloud-init faillock netreport qga.state systemd utmp console initramfs nscd sepermit tmpfiles.d [root@iz2zeezp920l8adg80sf0pz run]# [root@iz2zeezp920l8adg80sf0pz ~]# mkdir -p /var/run/mysqld
[root@iz2zeezp920l8adg80sf0pz ~]# chown mysql:mysql /var/run/mysqld 

參考自: https://codedump.io/share/bND2n706rgU2/1/mysqldsafe-directory-39varrunmysqld39-for-unix-socket-file-don39t-exists

 

再次啟動服務:

[root@iz2zeezp920l8adg80sf0pz mysql]# bin/mysqld_safe --user=mysql &
[1] 29710 [root@iz2zeezp920l8adg80sf0pz mysql]# Logging to '/usr/local/mysql/mysql-files/iz2zeezp920l8adg80sf0pz.err'.
2017-10-29T16:23:39.587364Z mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/mysql-files [root@iz2zeezp920l8adg80sf0pz mysql]# 
[root@iz2zeezp920l8adg80sf0pz mysql]# netstat -ntlp
Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name tcp 0 0 0.0.0.0:22              0.0.0.0:*               LISTEN      2096/sshd tcp6 0 0 :::3306 :::* LISTEN 30188/mysqld  [root@iz2zeezp920l8adg80sf0pz mysql]# 

MySQL數據庫默認端口為3306,可以看到mysqld服務已啟動了。

mysql-files目錄下,可以看到生成了一些數據文件

[root@iz2zeezp920l8adg80sf0pz mysql-files]# ls
auto.cnf        ib_logfile1                  iz2zeezp920l8adg80sf0pz.pid
ib_buffer_pool  ib_logfile2                  mysql
ibdata1         ibtmp1                       performance_schema
ib_logfile0     iz2zeezp920l8adg80sf0pz.err  sys
[root@iz2zeezp920l8adg80sf0pz mysql-files]#

7、登陸數據庫,登陸密碼是之前生成的臨時密碼:

[root@iz2zeezp920l8adg80sf0pz bin]# mysql -uroot -p
-bash: mysql: command not found
[root@iz2zeezp920l8adg80sf0pz bin]# ./mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20

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> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

我本來想show databases查看數據庫的,發現無法查看。看到必須先改密碼才能執行操作

mysql> set password=password('aa1274828542')
    -> ;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> show databases;
mysql> set password=password('xxxx')    # xxxx設定的新密碼
    -> ;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+

 

六、直接登陸數據庫

現在你已經可以登際MySQL了,想必你肯定是和我一樣高興。執行以下命令,發現無法登陸,原因是無法找到mysql指令。

[root@iz2zeezp920l8adg80sf0pz ~]# mysql -uroot -p
-bash: mysql: command not found [root@iz2zeezp920l8adg80sf0pz ~]# 

這很正常,因為mysql命令的文件是在/usr/local/mysql/bin目錄下。

[root@iz2zeezp920l8adg80sf0pz ~]# ls /usr/local/mysql/bin | grep "mysql"
mysql

而系統的環境變量如下,根本無法找到mysql這個命令:

[root@iz2zeezp920l8adg80sf0pz ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin [root@iz2zeezp920l8adg80sf0pz ~]# 

要想直接執行mysql -uroot -p,然后輸入密碼就能登陸,這就得修改.bashrc文件:

加上 export PATH=$PATH:/usr/local/mysql/bin 當然,你得確保/usr/local/mysql/bin目錄有mysql。

[root@iz2zeezp920l8adg80sf0pz ~]# vim ~/.bashrc    

# .bashrc

###edit by chengliang at 2017/10/30###
# export PATH for user root can logging mysql-server quickly.
# after chage this file,you should source it.

export PATH=$PATH:/usr/local/mysql/bin

[root@iz2zeezp920l8adg80sf0pz ~]# source ~/.bashrc 
[root@iz2zeezp920l8adg80sf0pz ~]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@iz2zeezp920l8adg80sf0pz ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.

生效方法:(有以下兩種)

  • 關閉當前終端窗口,重新打開一個新終端窗口就能生效
  • 輸入“source ~/.bashrc”命令,立即生效

有效期限:永久有效
用戶局限:僅對當前用戶

參考博客: linux查看和修改PATH環境變量的方法

 

七、stop/start/restart

[root@iz2zeezp920l8adg80sf0pz ~]# /etc/init.d/mysql stop # 停止數據庫服務
Shutting down MySQL.. [ OK ] [root@iz2zeezp920l8adg80sf0pz ~]# [root@iz2zeezp920l8adg80sf0pz ~]# /etc/init.d/mysql start # 開啟數據庫服務
Starting MySQL. [ OK ] [root@iz2zeezp920l8adg80sf0pz ~]# [root@iz2zeezp920l8adg80sf0pz ~]# /etc/init.d/mysql restart # 重啟數據庫
Shutting down MySQL.. [ OK ] Starting MySQL. [ OK ] [root@iz2zeezp920l8adg80sf0pz ~]#

 

雖然知道上了首頁會被撤,但我還是要發首頁

段子:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM