MySQL数据库在线安装,更新升级

在linux系统安装mysql,其他版本的yum 库安装方法参考mysql官方文档 https://dev.mysql.com/doc/refman/8.0/en/installing.html

安装时需要连接网络,关闭防火墙以及selinux

mysql在线安装

方法一:下载mysql的yum 库文件

下载时选择合适自己的版本
http://dev.mysql.com/downloads/repo/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 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源
[root@localhost ~]# 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
[root@localhost ~]# yum clean all
[root@localhost ~]# 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的信息
[root@localhost yum.repos.d]# yum repolist all | grep mysql
mysql57-community/x86_64        MySQL 5.7 Community Server        enabled:   273

如果需要安装其他版本需要对yum源进行修改!

方法三:rpm包安装
该压缩包里面包含需要的rpm包,安装时根据需要选择性安装

[root@www lnmp]# ls
mysql-5.7.24-1.el7.x86_64.rpm-bundle.tar
[root@www lnmp]# tar -xf mysql-5.7.24-1.el7.x86_64.rpm-bundle.tar
[root@www mysql]# ls
mysql-community-client-5.7.24-1.el7.x86_64.rpm           mysql-community-libs-5.7.24-1.el7.x86_64.rpm
mysql-community-common-5.7.24-1.el7.x86_64.rpm           mysql-community-libs-compat-5.7.24-1.el7.x86_64.rpm
mysql-community-devel-5.7.24-1.el7.x86_64.rpm            mysql-community-minimal-debuginfo-5.7.24-1.el7.x86_64.rpm
mysql-community-embedded-5.7.24-1.el7.x86_64.rpm         mysql-community-server-5.7.24-1.el7.x86_64.rpm
mysql-community-embedded-compat-5.7.24-1.el7.x86_64.rpm  mysql-community-server-minimal-5.7.24-1.el7.x86_64.rpm
mysql-community-embedded-devel-5.7.24-1.el7.x86_64.rpm   mysql-community-test-5.7.24-1.el7.x86_64.rpm

安装时不建议mysql-community-minimal-debuginfo-5.7.24-1.el7.x86_64.rpm,mysql-community-server-minimal-5.7.24-1.el7.x86_64.rpm,mysql-community-test-5.7.24-1.el7.x86_64.rpm,可以删除该rpm包;
[root@www mysql]# yum install -y *.rpm
[root@www ~]# systemctl start mysqld
[root@www ~]# systemctl status mysqld
[root@www ~]# systemctl enable mysqld
#启动数据库是系统回设置一个初始密码,可以在一下文件查看
[root@www ~]# more /var/log/mysqld.log 
2018-12-08T01:46:08.452482Z 1 [Note] A temporary password is generated for root@
localhost: />y&cj2VQ0gF
#初始化修改数据库密码
[root@www ~]# mysql_secure_installation
ecuring the MySQL server deployment.
Enter password for user root: 
The existing password for the user account root has expired. Please set a new password.
New password: 
Re-enter new password: 

mysql的升级
使用mysql yum repositry进行升级,禁用mysql5.7版本子存储库,启用8.0版本的子存储库;升级版本需要一步一步进行升级,例如从5.6想升级到8.0,需要先升级到5.7再升级到8.0,

mysql数据库升级

注意:对所有数据库升级时需要花费很多时间

支持dnf工具使用以下命令升级
[root@localhost ~]# dnf upgrade mysql-serve
[root@localhost ~]# dnf upgrade 对所有mysql数据库进行升级
不支持dnf工具使用以下命令升级
[root@localhost ~]# yum update mysql-server
[root@localhost ~]# yum update 对所有mysql数据库进行升级

对于数据库特定组件升级

[root@localhost ~]# yum list installed | grep “^mysql” #列出安装的mysql组件
[root@localhost ~]# yum update package-name #支持dnf
[root@localhost ~]# dnf upgrade package-name

mysql数据库存储引擎

存储引擎介绍
mysql里面存在一种可替换存储引擎架构的特性,意味着mysql可以根据自己的需求来设置相应的存储引擎,也可以自己编写引擎。mysql数据库工作原理分为两部分,一层为语句分析层:主要负责与客户端完成连接并且事先分析sql语句的内容和功能;一层为存储引擎:主要负责接收来自语句分析层的分析结果,完成相应数据的输入输出和文件操作(如何存储数据,为存储建立索引和如何更新,查询数据等方法);
关系型数据库里面数据存储是以表的形式存储,所以存储引擎也可以成为表类型(即存储和操作表的类型)

存储引擎:MyISAM
mysql数据库的默认存储引擎,没有对数据库的事物支持,也不支持行级锁和外键,因此当insert或update数据时即写操作需要锁定整个表,效率会低些;独立与操作系统,可以在windos系统上使用,也可以比较简单的将数据转移到linux系统;
该存储引擎可以分为三种:

静态引擎:如果数据表中的各数据长度都是原先固定好的,服务器将自动选择这种表类型。因为每条记录占用空间相同,操作效率更高,数据受损是便于恢复;
动态引擎:如数据表中出现varchar等字段时,相对于静态引擎,该表存储空间较小,单存储长度不一,多次修改数据后会将表中的数据离散存储在内存中出现大量碎片导致效率下降。因此需要使用优化工具来进行优化;
压缩引擎:将静态引擎和动态引擎定义的表使用myisamchk工具进行压缩,压缩后不能被进行修改。因为是压缩数据,使用时需要进行解压;

使用MyISAM引擎建立数据库表时会额外创建三个格式的文件,示例:后缀名为frm的文件用于存储表的定义,后缀名为MYD的文件用于单独存放数据和优化数据库查询等操作,后缀名为MYI文件存储数据库的索引,有索引管理和字段管理作用;该方法可以将大文件的索引平均分配提升操作时间。

查询数据库容量大小

查询所有数据库容量大小
使用information_schema数据库进行查询

mysql> use information_schema;
mysql> select concat(round(sum(DATA_LENGTH/1024/1024),2),'MB') as size from TABLES;
+---------+
| size    |
+---------+
| 21.03MB |
+---------+
1 row in set (0.51 sec)

查询指定数据库的容量大小


mysql> select concat(round(sum(DATA_LENGTH/1024/1024),2),'MB') as size from TABLES where table_schema='zabbix';
+---------+
| size    |
+---------+
| 18.34MB |
+---------+

查询数据库某个表容量大小

mysql> use zabbix;
mysql> show tables;
+----------------------------+
| Tables_in_zabbix           |
+----------------------------+
| acknowledges               |
| actions                    |
| alerts                     |
| application_discovery      |
| application_prototype      |
| application_template       |
| applications               |
| auditlog                   |
| auditlog_details           |
---------------------------------------------

mysql> select concat(round(sum(DATA_LENGTH/1024/1024),2),'MB') as size from TABLES where table_schema='zabbix' and table_name='acknowledges';
+--------+
| size   |
+--------+
| 0.02MB |
+--------+
1 row in set (0.01 sec)

查询某个数据库的表个数

mysql> select count(*)tables,table_schema from TABLES where table_schema='zabbix';
+--------+--------------+
| tables | table_schema |
+--------+--------------+
|    146 | zabbix       |
+--------+--------------+
1 row in set (0.00 sec)

查询数据库表的统计数并且按降序排序
在排序后面可以指定需要查询的数据表

 mysql> select table_name,table_rows from tables where table_schema='zabbix' order by table_rows desc;
 +----------------------------+------------+
| table_name                 | table_rows |
+----------------------------+------------+
| history                    |     118345 |
| history_uint               |      65411 |
| trends                     |       3054 |
| history_str                |       2766 |
| items_applications         |       2006 |
| items                      |       1884 |
| trends_uint                |       1769 |
| functions                  |       1520 |
| triggers                   |       1050 |
| auditlog                   |        906 |
省略。。。。。。。。。。。。。。

你可能感兴趣的:(数据库)