MySQL入門——在Linux下安裝和卸載MySQL

MySQL入門——在Linux下安裝和卸載MySQL

摘要:本文主要學習瞭如何在Linux系統中安裝和卸載MySQL數據庫。mysql

查看有沒有安裝過MySQL

使用命令查看有沒有安裝過:git

1 [root@localhost ~]# yum list installed | grep mysql 2 [root@localhost ~]#

從CentOS版本6開始,官方版本的yum源中用MariaDB替換了MySQL,新的Linux系統中將不會默認安裝MySQL,安裝MySQL通常須要去官網下載rpm包或者源碼包等方式。sql

安裝MySQL源碼包

查看yum可用的軟件包

由於若是沒有源碼包,yum的倉庫裏是沒有MySQL的安裝包的,使用命令查看yum倉庫裏有關MySQL的軟件包:數據庫

1 [root@localhost ~]# yum repolist all | grep mysql 2 [root@localhost ~]#

找到源碼包下載連接

MySQL的官網地址以下:vim

https://www.mysql.com架構

登陸官網如圖:app

將頁面滑動到下面,點擊Red Hat Enterprise Linux 7對應的Download下載按鈕:dom

在新的頁面找到下方的地址連接,右鍵複製連接地址:post

下載源碼包

在Linux系統中使用wget命令下載rpm包,地址即爲剛剛在MySQL官網複製的連接地址:學習

 1 [root@localhost ~]# wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
 2 --2019-07-13 16:19:14--  https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
 3 正在解析主機 dev.mysql.com (dev.mysql.com)... 137.254.60.11
 4 正在鏈接 dev.mysql.com (dev.mysql.com)|137.254.60.11|:443... 已鏈接。  5 已發出 HTTP 請求,正在等待迴應... 302 Found  6 位置:https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm [跟隨至新的 URL]
 7 --2019-07-13 16:19:15--  https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm
 8 正在解析主機 repo.mysql.com (repo.mysql.com)... 184.26.157.95
 9 正在鏈接 repo.mysql.com (repo.mysql.com)|184.26.157.95|:443... 已鏈接。 10 已發出 HTTP 請求,正在等待迴應... 200 OK 11 長度:26024 (25K) [application/x-redhat-package-manager] 12 正在保存至: 「mysql80-community-release-el7-3.noarch.rpm.113 
14 100%[=====================================================================================================>] 26,024      88.9KB/s 用時 0.3s 15 
16 2019-07-13 16:19:17 (88.9 KB/s) - 已保存 「mysql80-community-release-el7-3.noarch.rpm.1」 [26024/26024]) 17 
18 [root@localhost ~]#

安裝源碼包

使用命令安裝MySQL的源碼包:

 1 [root@localhost ~]# yum install -y mysql80-community-release-el7-3.noarch.rpm  2 已加載插件:fastestmirror  3 正在檢查 mysql80-community-release-el7-3.noarch.rpm: mysql80-community-release-el7-3.noarch  4 mysql80-community-release-el7-3.noarch.rpm 將被安裝  5 正在解決依賴關係  6 --> 正在檢查事務  7 ---> 軟件包 mysql80-community-release.noarch.0.el7-3 將被 安裝  8 --> 解決依賴關係完成  9 
10 依賴關係解決 11 ... 12 完畢! 13 [root@localhost ~]#

再次查看yum倉庫中的源碼包

使用命令查看yum倉庫中有關MySQL的軟件包:

 1 [root@localhost ~]# yum repolist all | grep mysql  2 mysql-cluster-7.5-community/x86_64 MySQL Cluster 7.5 Community 禁用  3 mysql-cluster-7.5-community-source MySQL Cluster 7.5 Community - So 禁用  4 mysql-cluster-7.6-community/x86_64 MySQL Cluster 7.6 Community 禁用  5 mysql-cluster-7.6-community-source MySQL Cluster 7.6 Community - So 禁用  6 mysql-cluster-8.0-community/x86_64 MySQL Cluster 8.0 Community 禁用  7 mysql-cluster-8.0-community-source MySQL Cluster 8.0 Community - So 禁用  8 !mysql-connectors-community/x86_64 MySQL Connectors Community       啓用:    118
 9 mysql-connectors-community-source  MySQL Connectors Community - Sou 禁用 10 !mysql-tools-community/x86_64      MySQL Tools Community            啓用:     95
11 mysql-tools-community-source       MySQL Tools Community - Source 禁用 12 mysql-tools-preview/x86_64 MySQL Tools Preview 禁用 13 mysql-tools-preview-source         MySQL Tools Preview - Source 禁用 14 mysql55-community/x86_64           MySQL 5.5 Community Server 禁用 15 mysql55-community-source           MySQL 5.5 Community Server - Sou 禁用 16 mysql56-community/x86_64           MySQL 5.6 Community Server 禁用 17 mysql56-community-source           MySQL 5.6 Community Server - Sou 禁用 18 mysql57-community/x86_64           MySQL 5.7 Community Server 禁用 19 mysql57-community-source           MySQL 5.7 Community Server - Sou 禁用 20 !mysql80-community/x86_64          MySQL 8.0 Community Server       啓用:    129
21 mysql80-community-source           MySQL 8.0 Community Server - Sou 禁用 22 [root@localhost ~]#

能夠看到,目前默認安裝版本是8.0。

修改安裝版本

若是要修改MySQL的安裝版本,能夠經過修改repo配置文件或者經過執行修改命令來實現。

1)修改文件

編輯mysql-community.repo文件,將 [mysql80-community] 下面的enable設置爲0表示關閉8.0版本,而後將 [mysql56-community] 下面的enable設置爲1表示開啓5.6版本。

命令以下:

1 [root@localhost ~]# vim /etc/yum.repos.d/mysql-community.repo

2)修改命令

關閉8.0版本:

1 [root@localhost ~]# yum-config-manager --disable mysql80-community 2 已加載插件:fastestmirror 3 ====================================================== repo: mysql80-community =======================================================
4 [mysql80-community] 5 ... 6 enabled = 0
7 ... 8 [root@localhost ~]#

開啓5.6版本:

1 [root@localhost ~]# yum-config-manager --enable mysql56-community 2 已加載插件:fastestmirror 3 ====================================================== repo: mysql56-community =======================================================
4 [mysql56-community] 5 ... 6 enabled = 1
7 ... 8 [root@localhost ~]#

安裝MySQL

安裝

使用yum命令安裝:

 1 [root@localhost ~]# yum install -y mysql-community-server  2 ...  3 ======================================================================================================================================
 4  Package 架構 版本 源 大小  5 ======================================================================================================================================
 6 正在安裝:  7  mysql-community-server              x86_64              5.6.45-2.el7                            mysql56-community               60 M  8 爲依賴而安裝:  9  mysql-community-client              x86_64              5.6.45-2.el7                            mysql56-community               20 M 10  mysql-community-common              x86_64              5.6.45-2.el7                            mysql56-community              257 k 11  mysql-community-libs                x86_64              5.6.45-2.el7                            mysql56-community              2.0 M 12  net-tools                           x86_64              2.0-0.24.20131004git.el7                base                           306 k 13 ... 14 完畢! 15 [root@localhost ~]#

查看安裝版本

使用命令查看MySQL的安裝版本:

1 [root@localhost ~]# mysql -V 2 mysql  Ver 14.14 Distrib 5.6.45, for Linux (x86_64) using EditLine wrapper 3 [root@localhost ~]#

啓動服務

使用systemctl命令啓動並查看MySQL的服務狀態:

 1 [root@localhost ~]# systemctl start mysqld  2 [root@localhost ~]# systemctl status mysqld  3 ● mysqld.service - MySQL Community Server  4    Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)  5    Active: active (running) since 三 2019-08-14 17:18:54 CST; 23s ago  6   Process: 1851 ExecStartPost=/usr/bin/mysql-systemd-start post (code=exited, status=0/SUCCESS)  7   Process: 1791 ExecStartPre=/usr/bin/mysql-systemd-start pre (code=exited, status=0/SUCCESS)  8  Main PID: 1850 (mysqld_safe)  9    CGroup: /system.slice/mysqld.service 10            ├─1850 /bin/sh /usr/bin/mysqld_safe --basedir=/usr 11            └─2016 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mysqld.log... 12 
13 8月 14 17:18:53 localhost.localdomain mysql-systemd-start[1791]: PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
14 8月 14 17:18:53 localhost.localdomain mysql-systemd-start[1791]: To do so, start the server, then issue the following commands: 15 8月 14 17:18:53 localhost.localdomain mysql-systemd-start[1791]: /usr/bin/mysqladmin -u root password 'new-password'
16 8月 14 17:18:53 localhost.localdomain mysql-systemd-start[1791]: /usr/bin/mysqladmin -u root -h localhost.localdomain password 'new-password'
17 8月 14 17:18:53 localhost.localdomain mysql-systemd-start[1791]: Alternatively you can run: 18 8月 14 17:18:53 localhost.localdomain mysql-systemd-start[1791]: /usr/bin/mysql_secure_installation 19 8月 14 17:18:53 localhost.localdomain mysql-systemd-start[1791]: which will also give you the option of removing the test 20 8月 14 17:18:53 localhost.localdomain mysqld_safe[1850]: 190814 17:18:53 mysqld_safe Logging to '/var/log/mysqld.log'. 21 8月 14 17:18:53 localhost.localdomain mysqld_safe[1850]: 190814 17:18:53 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql 22 8月 14 17:18:54 localhost.localdomain systemd[1]: Started MySQL Community Server. 23 [root@localhost ~]#

鏈接數據庫

MySQL的5.6版本安裝完成後,默認root用戶是沒有密碼的,使用命令登陸:

 1 [root@localhost ~]# mysql -uroot  2 Welcome to the MySQL monitor. Commands end with ; or \g.  3 Your MySQL connection id is 4
 4 Server version: 5.6.45 MySQL Community Server (GPL)  5 
 6 Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.  7 
 8 Oracle is a registered trademark of Oracle Corporation and/or its  9 affiliates. Other names may be trademarks of their respective 10 owners. 11 
12 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. 13 
14 mysql>

查看版本號

使用 select version(); 命令能夠查詢MySQL的版本:

1 mysql> select version(); 2 +-----------+
3 | version() |
4 +-----------+
5 | 5.6.45    |
6 +-----------+
7 1 row in set (0.00 sec) 8 
9 mysql>

簡單配置

使用命令對MySQL進行簡單配置:

1 [root@localhost ~]# mysql_secure_installation 2 
3 NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL 4       SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

輸入當前的密碼,若是沒有設置就回車:

1 In order to log into MySQL to secure it, we'll need the current
2 password for the root user.  If you've just installed MySQL, and
3 you haven't set the root password yet, the password will be blank,
4 so you should just press enter here. 5 
6 Enter current password for root (enter for none): 7 OK, successfully used password, moving on...

是否爲root用戶設置密碼:

1 Setting the root password ensures that nobody can log into the MySQL 2 root user without the proper authorisation. 3 
4 Set root password? [Y/n] y 5 New password: 6 Re-enter new password: 7 Password updated successfully!
8 Reloading privilege tables.. 9  ... Success!

是否刪除匿名用戶:

1 By default, a MySQL installation has an anonymous user, allowing anyone 2 to log into MySQL without having to have a user account created for
3 them.  This is intended only for testing, and to make the installation 4 go a bit smoother. You should remove them before moving into a 5 production environment. 6 
7 Remove anonymous users? [Y/n] 8  ... Success!

是否容許root用戶遠程登陸:

1 Normally, root should only be allowed to connect from 'localhost'. This 2 ensures that someone cannot guess at the root password from the network. 3 
4 Disallow root login remotely? [Y/n] 5  ... Success!

是否刪除test數據庫:

 1 By default, MySQL comes with a database named 'test' that anyone can  2 access.  This is also intended only for testing, and should be removed  3 before moving into a production environment.  4 
 5 Remove test database and access to it? [Y/n]  6  - Dropping test database...  7 ERROR 1008 (HY000) at line 1: Can't drop database 'test'; database doesn't exist  8  ... Failed! Not critical, keep moving...  9  - Removing privileges on test database... 10  ... Success!

是否從新加載權限表:

1 Reloading the privilege tables will ensure that all changes made so far 2 will take effect immediately. 3 
4 Reload privilege tables now? [Y/n] 5  ... Success!

設置完成:

1 All done!  If you've completed all of the above steps, your MySQL
2 installation should now be secure. 3 
4 Thanks for using MySQL!
5 
6 
7 Cleaning up... 8 [root@localhost ~]#

卸載MySQL

查看MySQL已安裝的軟件包:

1 [root@localhost ~]# yum list installed | grep mysql 2 mysql-community-client.x86_64        5.6.45-2.el7                   @mysql56-community 3 mysql-community-common.x86_64        5.6.45-2.el7                   @mysql56-community 4 mysql-community-libs.x86_64          5.6.45-2.el7                   @mysql56-community 5 mysql-community-server.x86_64        5.6.45-2.el7                   @mysql56-community 6 mysql80-community-release.noarch     el7-3 installed 7 [root@localhost ~]#

使用yum命令卸載:

 1 [root@localhost ~]# yum remove -y mysql80-community-release.noarch mysql-community-client mysql-community-server mysql-community-common mysql-community-libs  2 ...  3 ======================================================================================================================================
 4  Package 架構 版本 源 大小  5 ======================================================================================================================================
 6 正在刪除:  7  mysql-community-client                   x86_64                5.6.45-2.el7                  @mysql56-community                 89 M  8  mysql-community-common                   x86_64                5.6.45-2.el7                  @mysql56-community                2.1 M  9  mysql-community-libs                     x86_64                5.6.45-2.el7                  @mysql56-community                9.2 M 10  mysql-community-server                   x86_64                5.6.45-2.el7                  @mysql56-community                252 M 11  mysql80-community-release                noarch                el7-3                         installed                          31 k 12 ... 13 完畢! 14 [root@localhost ~]#
相關文章
相關標籤/搜索