1. 程式人生 > >MySQL菜鳥實錄(一):MySQL服務安裝實戰

MySQL菜鳥實錄(一):MySQL服務安裝實戰

# CentOS 7 ## 基本資訊 - 系統版本: CentOS 7.3 64bit - 系統配置: 4vCPUs | 8GB - 磁碟空間: ``` [root@ecs-ce5a-0001 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/vda1 40G 17G 22G 44% / devtmpfs 3.9G 0 3.9G 0% /dev tmpfs 3.9G 0 3.9G 0% /dev/shm tmpfs 3.9G 25M 3.8G 1% /run tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup tmpfs 783M 0 783M 0% /run/user/0 /dev/vdb1 493G 70M 467G 1% /data [root@ecs-ce5a-0001 ~]# ``` ## 檢查歷史殘留   使用rpm查詢功能,查詢本機是否有安裝過MySQL(因為安裝過後如果不徹底解除安裝的話,會導致後續安裝出現一些異常問題,所以為了保險起見還是先檢查一下為好): ``` # 沒有裝過MySQL的主機 [root@ecs-ce5a-0001 ~]# rpm -qa|grep -i mysql [root@ecs-ce5a-0001 ~]# # 之前裝過MySQL的主機 [root@mserver0002 img]# rpm -qa|grep -i mysql mysql-community-common-5.7.24-1.el6.x86_64 mysql-community-client-5.7.24-1.el6.x86_64 mysql-community-libs-5.7.24-1.el6.x86_64 mysql-community-server-5.7.24-1.el6.x86_64 [root@mserver0002 img]# ```   若發現有歷史安裝痕跡,那麼就要進行下一步的清理操作來清除歷史垃圾了,挨個將所有的安裝包清理完畢(若出現解除安裝不掉的可以嘗試使用`rpm -ev `): ``` [root@mserver0002 img]# yum -y remove mysql-community-common-5.7.24-1.el6.x86_64 [root@mserver0002 img]# yum -y remove xxx ``` ## 下載安裝源   MySQL有個安裝源倉庫站: http://repo.mysql.com/ ;裡面有一大排的安裝源連結,這時候可不能找錯了,對應於我們此次安裝的CentOS7.3版本我們選用帶el7的源: http://repo.mysql.com/mysql57-community-release-el7-9.noarch.rpm ;這一步切記千萬不能大意,下載的版本一旦和系統無法對應,那麼接下來的所有操作都是白費工夫,而且還有可能遇到一些意想不到的問題。 ``` [root@ecs-ce5a-0001 ~]# cd tmp/ [root@ecs-ce5a-0001 tmp]# ls [root@ecs-ce5a-0001 tmp]# wget http://repo.mysql.com/mysql57-community-release-el7-9.noarch.rpm --2018-11-14 10:29:03-- http://repo.mysql.com/mysql57-community-release-el7-9.noarch.rpm ...... 2018-11-14 10:29:04 (253 MB/s) - ‘mysql57-community-release-el7-9.noarch.rpm’ saved [9224/9224] [root@ecs-ce5a-0001 tmp]# ``` ## 安裝mysql-server   在經過上一步將安裝源下載到本地之後,接下來就是安裝了;先通過rpm將安裝源安裝後,就可以正式使用yum進行mysql-server的安裝了(據說也可以在安裝時指定installroot,沒有嘗試過,一般都是預設安裝的),有需要確認的地方之間y就可以了;直至出現“Complete!”意味著安裝順利完成了。 ``` [root@ecs-ce5a-0001 tmp]# rpm -ivh mysql57-community-release-el7-9.noarch.rpm warning: mysql57-community-release-el7-9.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY Preparing... ################################## [100%] Updating / installing... 1:mysql57-community-release-el7-9 ################################# [100%] [root@ecs-ce5a-0001 tmp]# [root@ecs-ce5a-0001 tmp]# yum -y install mysql-server Loaded plugins: fastestmirror Determining fastest mirrors ...... Replaced: mariadb-libs.x86_64 1:5.5.60-1.el7_5 Complete! [root@ecs-ce5a-0001 tmp]# ``` ## 預設配置檢視   預設情況下,安裝後的配置檔案是下面這樣子的(我們可以根據自己的需要將相應的配置補充或者進行修改,比如datadir一般都會設定到專屬的資料目錄下面去): ``` [root@ecs-ce5a-0001 tmp]# vim /etc/my.cnf [mysqld] # ... some comments datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid ``` ## 啟動服務   啟動MySQL服務,當前版本的MySQL在啟動時會預設生成一個root使用者並提供預設密碼,我們可以在`/var/log/mysqld.log `檔案中獲取到預設密碼,至此服務正常啟動完成,並獲取到了預設的root密碼。 ``` [root@ecs-ce5a-0001 tmp]# service mysqld restart Redirecting to /bin/systemctl restart mysqld.service [root@ecs-ce5a-0001 tmp]# [root@ecs-ce5a-0001 tmp]# grep password /var/log/mysqld.log 2018-11-14T03:07:28.036081Z 1 [Note] A temporary password is generated for root@localhost: 8-8br-HfR4J9 [root@ecs-ce5a-0001 tmp]# ``` ## 修改密碼和賦權   使用預設密碼登入後,發現啥也做不了,必須修改密碼,所以使用了alter來進行密碼修改(密碼要求比早版本的嚴格了很多,長度、特殊字元、字母和數字都有要求),關於賦權,後面的補充知識裡面會專門稍微詳細的說明一下。 ```sql [root@ecs-ce5a-0001 tmp]# mysql -uroot -p8-8br-HfR4J9 mysql: [Warning] Using a password on the command line interface can be insecure. ...... 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 'smart'; ERROR 1819 (HY000): Your password does not satisfy the current policy requirements mysql> alter user 'root'@'localhost' identified by 'Smart@123'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> ``` # CentOS 6 ## 無果的嘗試   理論上來說,CentOS6和CentOS7安裝MySQL的步驟應該是一致的,起碼大部分的步驟都應該是一致的,但是事實上卻並不如此。   按照傳統的安裝方式,即使在下載repo時選擇了正確的平臺版本,最後發現還是在`yum -y install mysql-server `這一步遇到了缺GLIBC_2.14的問題(類似下面這種),也曾經嘗試過自己編譯安裝這個庫,但是沒有成功,既然此路不通,那何不另覓新法呢! ``` Error: Package: mysql-community-libs-5.7.17-1.el7.x86_64 (mysql57-community-dmr) Requires: libc.so.6(GLIBC_2.14)(64bit) Error: Package: mysql-community-client-5.7.17-1.el7.x86_64 (mysql57-community-dmr) Requires: libc.so.6(GLIBC_2.14)(64bit) Error: Package: mysql-community-client-5.7.17-1.el7.x86_64 (mysql57-community-dmr) Requires: libstdc++.so.6(GLIBCXX_3.4.15)(64bit) You could try using --skip-broken to work around the problem ``` ## 另謀出路   既然線上安裝會出問題,那麼不妨試試傳統的安裝方式,把安裝包下載到本地再手動安裝呢?   於是就找到滿足我們系統要求的版本(下載地址: https://dev.mysql.com/downloads/file/?id=481078 ),下載到本地(檔名:mysql-5.7.24-1.el6.x86_64.rpm-bundle.tar,這裡的el6就是適配CentOS6的);壓縮包裡有如下一堆檔案: ``` Administrator@IVF21BBAA9XWLKD MINGW64 /e/暫時存放/mysql-5.7.24-1.el6.x86_64.rpm-bundle $ ls mysql-community-client-5.7.24-1.el6.x86_64.rpm mysql-community-common-5.7.24-1.el6.x86_64.rpm mysql-community-devel-5.7.24-1.el6.x86_64.rpm mysql-community-embedded-5.7.24-1.el6.x86_64.rpm mysql-community-embedded-devel-5.7.24-1.el6.x86_64.rpm mysql-community-libs-5.7.24-1.el6.x86_64.rpm mysql-community-libs-compat-5.7.24-1.el6.x86_64.rpm mysql-community-server-5.7.24-1.el6.x86_64.rpm mysql-community-test-5.7.24-1.el6.x86_64.rpm ```   接下來就分別按順序安裝common、libs、client、server四個包(其他工具包可以視情況決定是否安裝);這裡的四個包也是可以一次性安裝的(命令如:rpm -ivh pkg1 pkg2 pkg3 pkg4): ``` [root@mserver0002 img]# rpm -ivh mysql-community-common-5.7.24-1.el6.x86_64.rpm [root@mserver0002 img]# rpm -ivh mysql-community-libs-5.7.24-1.el6.x86_64.rpm [root@mserver0002 img]# rpm -ivh mysql-community-client-5.7.24-1.el6.x86_64.rpm [root@mserver0002 img]# rpm -ivh mysql-community-server-5.7.24-1.el6.x86_64.rpm ```   安裝完成後,後面的操作步驟就和CentOS7沒什麼差異了,無非是改改配置檔案,設定密碼和許可權之類的操作了。 # Ubuntu 16 ## 先說兩句   早幾年Ubuntu的系統使用的還是挺多的,但是如今很多應用預設都是使用CentOS了,其實孰優孰劣我還真沒詳細比較過,不過一個比較明顯的感覺呢,Ubuntu安裝軟體比起CentOS好像是比較簡便一些,更傻瓜式一點,所以都瞭解一下也沒什麼壞處。 ## 實際操作   鑑於下載安裝個系統還是要耗費不少精力,所以這次也就偷個小懶,沒有實際去驗證這個操作了,但是之前因為已經裝過很多次Ubuntu環境的MySQL了,結合網上的一些部落格說明,大致也就以下幾個步驟: ``` # 先通過如下3條命令完成安裝 sudo apt-get install mysql-server sudo apt install mysql-client sudo apt install libmysqlclient-dev # 然後檢視一下mysql的埠監聽確認服務是否正常啟動 sudo netstat -tap | grep mysql ```   安裝到這裡就已經算成功了,後面的步驟又是老調重彈了(配置、密碼、許可權等等),所以也就不多說了。 # Windows 10 ## 安裝版   貌似在5.5或之前的版本還挺流行安裝的,無非是一步步next,到最後設定個root密碼,然後就萬事大吉;到現在比較新的發行版好像都流行使用免安裝的方式了,而且安裝版的操作實際上也是小學生級的,只要稍微熟悉電腦操作的,估計裝起來都不會遇到啥問題,所以這裡就不多說了。   安裝版(MSI)的下載地址: https://dev.mysql.com/downloads/windows/installer/8.0.html 。 ## 免安裝版 ### 下載安裝源   參考地址: https://dev.mysql.com/downloads/file/?id=480557 ; 下載到本地:mysql-8.0.13-winx64.zip。 ### 解壓安裝   將壓縮包解壓到某個目錄下(這裡我是在虛擬機器裡面安裝的,所以直接放在C盤根目錄下了);然後以管理員許可權執行CMD,進入mysql解壓目錄的bin。   到這裡理論上來說是要配置一下my.ini檔案的(也就是Linux版的my.cnf檔案),鑑於這次只是做簡單的安裝實驗,所以就略去這一步操作。   接下來在dos視窗執行`mysqld --initialize-insecure`(注意:這裡有坑,參考文章最後的常見錯誤提供瞭解決方法)   然後就可以install和start了,出現下面的成功日誌即表示服務已經成功啟動了。 ``` Microsoft Windows [版本 10.0.10240] (c) 2015 Microsoft Corporation. All rights reserved. C:\Windows\system32>cd ../../ C:\>cd mysql-8.0.13-winx64 C:\mysql-8.0.13-winx64>cd bin C:\mysql-8.0.13-winx64\bin>mysqld --initialize-insecure C:\mysql-8.0.13-winx64\bin>mysqld -install Service successfully installed. C:\mysql-8.0.13-winx64\bin>net start mysql MySQL 服務正在啟動 .. MySQL 服務已經啟動成功。 C:\mysql-8.0.13-winx64\bin> ``` ### 安裝結果驗證   在前面的安裝操作中我們一直沒有設定root密碼,所以在驗證之前我們要設定一下root密碼,按照下面的步驟操作即可;密碼設定完成後即可登入進行常規操作,如下即表示安裝成功。 ``` C:\mysql-8.0.13-winx64\bin>mysqladmin -u root password Smart@123 mysqladmin: [Warning] Using a password on the command line interface can be insecure. Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety. C:\mysql-8.0.13-winx64\bin>mysql -uroot -p Enter password: ********* Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 10 Server version: 8.0.13 MySQL Community Server - GPL Copyright (c) 2000, 2018, 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 ; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.01 sec) mysql> ``` ### 補充內容   實際上這種免安裝模式是將MySQL註冊為Windows的一個服務了,通過檢視服務列表我們能發現這個MySQL服務,也可以控制服務的啟動方式、執行狀態等等。
### 怎麼解除安裝? - 首先要停止當前正在執行的服務,然後刪除mysql目錄 - 然後開啟登錄檔編輯器(regedit),找到並清除以下項: > HKEY_LOCAL_MACHINE/SYSTEM/ControlSet001/Services/Eventlog/Application/MySQLD Service HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/Eventlog/Application/MySQLD Service - 最後需要刪除已註冊到系統中的MySQL服務 ``` C:\Windows\system32>sc delete MySQL [SC] DeleteService 成功 C:\Windows\system32>
``` # 小葵花補充知識點 ## 關於密碼規則   MySQL的密碼設定有不同的規則等級,具體說來是跟validate_password_policy這個配置相關的(預設是1,即MEDIUM,所以剛開始設定的密碼必須符合長度,且必須含有數字,小寫或大寫字母,特殊字元。): | Policy | Tests Performed | | :----: | :-------------: | | 0 or LOW | Length | | 1 or MEDIUM | Length; numeric, lowercase/uppercase, and special characters | | 2 or STRONG | Length; numeric, lowercase/uppercase, and special characters; dictionary file | ## 忘記密碼了怎麼辦?   有時長時間不用某伺服器了,猛地有一天想去看看資料卻發現密碼忘了,很是尷尬吧?這時候就需要用到了skip-grant-tables 這個配置了,在my.cnf中將這個配置項開啟然後restart一下服務,就可以不用校驗密碼登入mysql伺服器了,登入進去之後再給自己的root重新設定一下密碼`update mysql.user set authentication_string=password('Smart@123') where user='root' ;`,大功告成! ``` [root@mserver0002 img]# cat /etc/my.cnf [mysqld] ####basic settings#### #skip-grant-tables default-storage-engine=INNODB ...... # 修改完配置檔案後可以使用空密碼登入(在Enter password:出現後直接敲enter鍵) [root@ecs-ce5a-0001 etc]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. ...... mysql>
use mysql Database changed mysql> SET PASSWORD = PASSWORD('Smart@123'); ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Smart@123'; ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement mysql> update mysql.user set password=password('Smart@123') where user= 'root'; ERROR 1054 (42S22): Unknown column 'password' in 'field list' mysql> desc mysql.user -> ; +------------------------+-----------------------------------+------+-----+-----------------------+-------+ | Field | Type | Null | Key | Default | Extra | +------------------------+-----------------------------------+------+-----+-----------------------+-------+ | Host | char(60) | NO | PRI | | | ...... | authentication_string | text | YES | | NULL | | | password_expired | enum('N','Y') | NO | | N | | | password_last_changed | timestamp | YES | | NULL | | | password_lifetime | smallint(5) unsigned | YES | | NULL | | | account_locked | enum('N','Y') | NO | | N | | +------------------------+-----------------------------------+------+-----+-----------------------+-------+ 45 rows in set (0.00 sec) mysql> update mysql.user set authentication_string=password('Smart@123') where user= 'root'; Query OK, 1 row affected, 1 warning (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 1 mysql> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.01 sec) mysql> exit Bye [root@ecs-ce5a-0001 etc]# mysql -uroot -p # 在這裡輸入新設定的密碼Smart@123 Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. ...... mysql> ``` ## 關於賦權的說明   首先說說改密碼,其實有下面兩種途徑都可以;而賦權則會涉及的比較多,可以參考下面的操作樣例(關於使用者許可權管理,這麼小小的一段是說不清楚的,後面會單獨開新的章節進行詳細說明): ``` ALTER USER 'root'@'localhost' IDENTIFIED BY 'Smart@123'; flush privileges; SET PASSWORD = PASSWORD('Smart@123'); ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER; flush privileges; -- root全部賦權 grant all privileges on *.* to root@"%" identified by "Smart@123"; flush privileges; -- 普通使用者部分賦權 grant select,insert,update,delete on db_test.* to test@"%" identified by "Smart@2018"; flush privileges; ``` ## 檢視MySQL版本   有時候難免會有需要檢視MySQL版本的需求(比如做庫同步時要檢視兩個服務的版本是不是一致能不能同步之類的),下面提供了4中常用的資料庫版本檢視的方法: ```bash # 方法1 [root@ecs-ce5a-0001 ~]# mysql -V mysql Ver 14.14 Distrib 5.7.24, for Linux (x86_64) using EditLine wrapper # 方法2 [root@ecs-ce5a-0001 ~]# mysql --help |grep Distrib mysql Ver 14.14 Distrib 5.7.24, for Linux (x86_64) using EditLine wrapper # 方法3 [root@ecs-ce5a-0001 ~]# mysql -uroot -pKoncendy@123 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 12 Server version: 5.7.24-log MySQL Community Server (GPL) Copyright (c) 2000, 2018, 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> select version(); +------------+ | version() | +------------+ | 5.7.24-log | +------------+ 1 row in set (0.00 sec) # 方法4 mysql> status -------------- mysql Ver 14.14 Distrib 5.7.24, for Linux (x86_64) using EditLine wrapper Connection id: 12 Current database: Current user: root@ SSL: Not in use Current pager: stdout Using outfile: '' Using delimiter: ; Server version: 5.7.24-log MySQL Community Server (GPL) Protocol version: 10 Connection: Localhost via UNIX socket Server characterset: utf8 Db characterset: utf8 Client characterset: utf8 Conn. characterset: utf8 UNIX socket: /data/mysql/mysql.sock Uptime: 5 min 21 sec Threads: 6 Questions: 293 Slow queries: 6 Opens: 195 Flush tables: 1 Open tables: 156 Queries per second avg: 0.912 -------------- mysql> ``` ## 常見的錯誤 ### 遠端無法訪問   通常情況下,有兩個可能性比較大的原因: - bind-address沒設定對,埠繫結到127.0.0.1上面去了 - 雲伺服器的3306埠沒有做安全組規則開放(這個在本地伺服器安裝不會出現) ### 目錄初始化問題   啟動時失敗通過`journalctl -xe`檢視發現如下的錯入日誌,一般出現這個錯誤表示my.cnf裡指定的datadir不是一個空目錄(有垃圾檔案或者有之前初始化殘留的檔案);已經初始化,但沒有成功,想再次初始化報錯;這時需要清理這個目錄之後重新嘗試啟動服務。 > ...: 2018-11-14T13:43:11.806347+08:00 0 [ERROR] --initialize specified but the data directory has files in it. Aborting. ### 日誌許可權的問題   啟動時失敗通過`journalctl -xe`檢視發現如下的錯入日誌,實際上就是寫日誌檔案沒有許可權導致的錯誤。但是我在執行了`[root@ecs-ce5a-0001 etc]# chmod -R 766 /var/log/mysql`之後發現啟動服務後還是會報下面的錯誤,很奇怪(因為766已經對應的是`-rwxrw-rw-`這個許可權級別,可以讀寫了);後來將766調整為777才可以正常啟動服務。(沒完全弄明白為什麼對日誌目錄要執行許可權?) > ...: 2018-11-14T13:44:17.230008+08:00 0 [ERROR] Could not open file '/var/log/mysql/error.log' for error logging: Permission denied ### MSVCP140.dll缺失   windows下出現的,在dos視窗執行`mysqld --initialize-insecure`,這時出現如下提示: ``` --------------------------- mysqld.exe - 系統錯誤 --------------------------- 無法啟動此程式,因為計算機中丟失 MSVCP140.dll。嘗試重新安裝該程式以解決此問題。 --------------------------- 確定 --------------------------- ```   實際上這個問題是因為沒有安裝VC++2015版執行庫導致的(Microsoft Visual C++ 2015 Redistributable),到如下下載地址 https://www.microsoft.com/en-us/download/details.aspx?id=53587 ,按照本機作業系統的位數確定下載的包,安裝後即可解決此問題。 ### 客戶端連線MySQL 8報1251   通過SQLyog或者Navicat連線MySQL發現報1251如下錯誤提示: ![](https://img2020.cnblogs.com/blog/753689/202102/753689-20210208091034898-1655428187.png)   百度了一下,原因是mysql8之前的版本中加密規則是mysql_native_password,在mysql8之後加密規則改成了caching_sha2_password;解決的方法有兩種,一種是升級客戶端驅動,一種是把mysql使用者登入密碼加密規則還原成mysql_native_password;因為咱們大多數都是自己想辦法搞得客戶端,所以就選擇第二條路了;操作如下: ``` # Login mysqladmin -u root -p ...... # Execute SQL ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER; ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'admin123'; FLUSH PRIVILE