1. 程式人生 > >MySQL資料庫線上安裝,更新升級,使用

MySQL資料庫線上安裝,更新升級,使用

安裝時需要連線網路,關閉防火牆以及selinux

mysql線上安裝

方法一:下載mysql的yum 庫檔案

[root@localhost ~]# wget https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm
#安裝yum庫
[root@localhost ~]# yum localinstall -y mysql80-community-release-el7-1.noarch.rpm
#檢查是否將yum庫增加到系統庫
[root@localhost ~]# yum repolist enabled | grep "mysql.*-community.*"
mysql-connectors-community/x86_64 MySQL Connectors Community 65 mysql-tools-community/x86_64 MySQL Tools Community 69 mysql80-community/x86_64 MySQL 8.0 Community Server 33

在MySQL Yum庫中,MySQL Community Server的不同版本系列儲存在不同的子庫中。預設情況下,最新的GA系列(目前為MySQL 8.0)的子儲存庫是啟用的,其他所有系列的子儲存庫是禁用的。使用此命令檢視MySQL Yum庫中的所有子儲存庫,並檢視其中哪些是啟用或禁用的

[root@localhost ~]# yum repolist all | grep mysql
mysql-cluster-7.5-community/x86_64 MySQL Cluster 7.5 Community    disabled
mysql-cluster-7.5-community-source MySQL Cluster 7.5 Community -  disabled
mysql-cluster-7.6-community/x86_64 MySQL Cluster 7.6 Community    disabled
mysql-cluster-7.6-community-source
MySQL Cluster 7.6 Community - disabled mysql-connectors-community/x86_64 MySQL Connectors Community enabled: 65 mysql-connectors-community-source MySQL Connectors Community - S disabled mysql-tools-community/x86_64 MySQL Tools Community enabled: 69 mysql-tools-community-source MySQL Tools Community - Source disabled mysql-tools-preview/x86_64 MySQL Tools Preview disabled mysql-tools-preview-source MySQL Tools Preview - Source disabled mysql55-community/x86_64 MySQL 5.5 Community Server disabled mysql55-community-source MySQL 5.5 Community Server - S disabled mysql56-community/x86_64 MySQL 5.6 Community Server disabled mysql56-community-source MySQL 5.6 Community Server - S disabled mysql57-community/x86_64 MySQL 5.7 Community Server disabled mysql57-community-source MySQL 5.7 Community Server - S disabled mysql80-community/x86_64 MySQL 8.0 Community Server enabled: 33 mysql80-community-source MySQL 8.0 Community Server - S disabled

開啟和禁用特定子庫,最好啟用一個版本,多個版本時預設啟用最新版本;

#如果系統不存在yum-config-manager命令,需要安裝一下軟體
[root@localhost ~]# yum install -y yum-utils.noarch
#開啟mysql5.7的版本子庫,關閉mysql8.0的版本子庫
[root@localhost ~]# yum-config-manager --enable mysql57-community
[root@localhost ~]# yum-config-manager --disable mysql80-community
[root@localhost ~]# yum repolist all | grep mysql
mysql-cluster-7.5-community/x86_64 MySQL Cluster 7.5 Community    disabled
mysql-cluster-7.5-community-source MySQL Cluster 7.5 Community -  disabled
mysql-cluster-7.6-community/x86_64 MySQL Cluster 7.6 Community    disabled
mysql-cluster-7.6-community-source MySQL Cluster 7.6 Community -  disabled
mysql-connectors-community/x86_64  MySQL Connectors Community     enabled:    65
mysql-connectors-community-source  MySQL Connectors Community - S disabled
mysql-tools-community/x86_64       MySQL Tools Community          enabled:    69
mysql-tools-community-source       MySQL Tools Community - Source disabled
mysql-tools-preview/x86_64         MySQL Tools Preview            disabled
mysql-tools-preview-source         MySQL Tools Preview - Source   disabled
mysql55-community/x86_64           MySQL 5.5 Community Server     disabled
mysql55-community-source           MySQL 5.5 Community Server - S disabled
mysql56-community/x86_64           MySQL 5.6 Community Server     disabled
mysql56-community-source           MySQL 5.6 Community Server - S disabled
mysql57-community/x86_64           MySQL 5.7 Community Server     enabled:   287
mysql57-community-source           MySQL 5.7 Community Server - S disabled
mysql80-community/x86_64           MySQL 8.0 Community Server     disabled
mysql80-community-source           MySQL 8.0 Community Server - S disabled
#檢測mysql開啟的狀態
[root@localhost ~]# yum repolist enabled | grep mysql
mysql-connectors-community/x86_64      MySQL Connectors Community            65
mysql-tools-community/x86_64           MySQL Tools Community                 69
mysql57-community/x86_64               MySQL 5.7 Community Server           287

安裝mysql5.7版本以及初始化

#mysql安裝時預設安裝client,connect等相關擴充套件軟體
[root@localhost ~]# yum install -y mysql-community-server

Installed:
  mysql-community-libs.x86_64 0:5.7.23-1.el7           mysql-community-libs-compat.x86_64 0:5.7.23-1.el7        
  mysql-community-server.x86_64 0:5.7.23-1.el7        
Dependency Installed:
  libaio.x86_64 0:0.3.109-13.el7                          mysql-community-client.x86_64 0:5.7.23-1.el7           
  mysql-community-common.x86_64 0:5.7.23-1.el7            net-tools.x86_64 0:2.0-0.22.20131004git.el7 

#啟動資料庫服務
[root@localhost ~]# systemctl start mysqld
[root@localhost ~]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2018-09-12 12:37:30 CST; 9s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 1747 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 1673 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 1750 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─1750 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

Sep 12 12:37:27 localhost.localdomain systemd[1]: Starting MySQL Server...
Sep 12 12:37:30 localhost.localdomain systemd[1]: Started MySQL Server.

#過濾初始化密碼
[root@localhost ~]# grep "password" /var/log/mysqld.log 
2018-09-12T04:37:27.902373Z 1 [Note] A temporary password is generated for root@localhost: (8<hgozGdwz8
#修改root使用者密碼
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';
Query OK, 0 rows affected (0.00 sec)

#從MySQL Yum庫中列出平臺上所有MySQL元件的包:
[root@localhost ~]# yum --disablerepo=\* --enablerepo='mysql*-community*' list available

方法二:配置mysql5.7的yum源 需要對系統的yum源進行修改如下:

#編輯配置檔案,在檔案後面新增如下yum源
[[email protected] ~]# vim /etc/yum.repo.d/CentOS-Base.repo
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/6/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[[email protected] ~]# yum clean all
[[email protected] ~]# yum makecache
Loaded plugins: fastestmirror
Determining fastest mirrors
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.cn99.com
base                                                                                      | 3.6 kB  00:00:00     
extras                                                                                    | 3.4 kB  00:00:00     
mysql57-community                                                                         | 2.5 kB  00:00:00     
updates                                                                                   | 3.4 kB  00:00:00     
(1/15): base/7/x86_64/group_gz                                                            | 166 kB  00:00:00     
(2/15): extras/7/x86_64/prestodelta                                                       |  84 kB  00:00:00     
(3/15): extras/7/x86_64/primary_db                                                        | 187 kB  00:00:00     
(4/15): extras/7/x86_64/filelists_db                                                      | 595 kB  00:00:00     
(5/15): extras/7/x86_64/other_db                                                          | 120 kB  00:00:00     
(6/15): base/7/x86_64/other_db                                                            | 2.5 MB  00:00:00     
(7/15): mysql57-community/x86_64/primary_db                                               | 156 kB  00:00:00     
(8/15): base/7/x86_64/primary_db                                                          | 5.9 MB  00:00:01     
(9/15): updates/7/x86_64/prestodelta                                                      | 538 kB  00:00:00     
(10/15): updates/7/x86_64/filelists_db                                                    | 2.9 MB  00:00:00     
(11/15): updates/7/x86_64/other_db                                                        | 478 kB  00:00:00     
(12/15): mysql57-community/x86_64/other_db                                                |  39 kB  00:00:00     
(13/15): base/7/x86_64/filelists_db                                                       | 6.9 MB  00:00:02     
(14/15): mysql57-community/x86_64/filelists_db                                            | 706 kB  00:00:02     
(15/15): updates/7/x86_64/primary_db                                                      | 5.2 MB  00:00:01     
Metadata Cache Created
#檢視mysql的資訊
[[email protected] yum.repos.d]# yum repolist all | grep mysql
mysql57-community/x86_64        MySQL 5.7 Community Server        enabled:   273

如果需要安裝其他版本需要對yum源進行修改!

mysql的升級

使用mysql yum repositry進行升級,禁用mysql5.7版本子儲存庫,啟用8.0版本的子儲存庫;升級版本需要一步一步進行升級,例如從5.6想升級到8.0,需要先升級到5.7再升級到8.0

mysql資料庫升級

注意:對所有資料庫升級時需要花費很多時間

支援dnf工具使用以下命令升級 [[email protected] ~]# dnf upgrade mysql-serve [[email protected] ~]# dnf upgrade 對所有mysql資料庫進行升級 不支援dnf工具使用以下命令升級 [[email protected] ~]# yum update mysql-server [[email protected] ~]# yum update 對所有mysql資料庫進行升級

對於資料庫特定元件升級

[[email protected] ~]# yum list installed | grep “^mysql” #列出安裝的mysql元件 [[email protected] ~]# yum update package-name #支援dnf [[email protected] ~]# dnf upgrade package-name

mysql資料庫的多表查詢