MySQL 三种安装方式

本贴最后更新于 1298 天前,其中的信息可能已经物是人非

安装环境

虚拟机软件:VMware Workstation 15 Pro
虚拟机系统:CentOS Linux release 7.2.1511 (Core)
虚拟机内存:4G

MySQL 在 CentOS 上有三种安装方式:

  • 使用通用的二进制包进行安装
  • 使用 rpm 包安装
  • 使用源码自行编译安装

通用的二进制包

下载地址:https://dev.mysql.com/downloads/mysql/
操作系统选择:Linux - Generic

首先,将二进制包上传到/tmp 目录下,然后按照下面的步骤进行安装。其中 5.7 之前和之后的版本在初始化时稍有不同。

版本 5.7 之前

# 卸载系统自带的mariadb
rpm -qa | grep mariadb
rpm -evh --nodeps mariadb-libs-5.5.44-2.el7.centos.x86_64
# 或者使用yum
yum list installed | grep mariadb
yum -y remove mariadb-libs.x86_64

# 创建用户和组
groupadd mysql
useradd -r -g mysql -s /bin/false mysql

# 切换到/tmp目录,解压
cd /tmp
tar zxvf mysql-5.5.62-linux-glibc2.12-x86_64.tar.gz
#tar zxvf mysql-5.6.44-linux-glibc2.12-x86_64.tar.gz

# 将解压后的目录移动到/usr/local目录下
mv mysql-5.5.62-linux-glibc2.12-x86_64 /usr/local/mysql
#mv mysql-5.6.44-linux-glibc2.12-x86_64 /usr/local/mysql
chown mysql:mysql -R /usr/local/mysql

# 初始化数据目录
cd /usr/local/mysql/
scripts/mysql_install_db --user=mysql
chown -R root .
chown -R mysql data

# 启动和关闭
cp support-files/my-medium.cnf /etc/my.cnf ## 版本5.5
bin/mysqld_safe --user=mysql &
bin/mysqladmin shutdown
 
# 配置MySQL开机自动启动
cp support-files/mysql.server /etc/init.d/mysql.server
chmod 755 /etc/init.d/mysql.server
systemctl enable mysql.server

版本 5.7 之后

# 卸载系统自带的mariadb
rpm -qa | grep mariadb
rpm -evh --nodeps mariadb-libs-5.5.44-2.el7.centos.x86_64
# 或者使用yum
yum list installed | grep mariadb
yum -y remove mariadb-libs.x86_64

# 创建用户和组
groupadd mysql
useradd -r -g mysql -s /bin/false mysql

# 切换到/tmp目录,解压
cd /tmp
tar zxvf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz

# 将解压后的目录移动到/usr/local目录下
mv mysql-5.7.26-linux-glibc2.12-x86_64 /usr/local/mysql
chown mysql:mysql -R /usr/local/mysql

# 初始化数据目录
cd /usr/local/mysql/
mkdir mysql-files
chmod 750 mysql-files
bin/mysqld --initialize --user=mysql #初始化时会为root@localhost账号生成一个临时密码

# 启动和关闭(关闭时需要输入密码)
bin/mysqld_safe --user=mysql &
bin/mysqladmin shutdown -p

# 配置MySQL开机自动启动
cp support-files/mysql.server /etc/init.d/mysql.server
chmod 755 /etc/init.d/mysql.server
systemctl enable mysql.server

自定义配置

我们可以自己创建数据库和日志存放目录,配置文件等。在初始化时通过参数指定这些自定义的目录和文件。
首先,将安装包上传到 /tmp 目录,将准备好的配置文件(比如:my3306.cnf)上传到 /etc 目录下。然后,按照下面的步骤进行安装。

# 创建用户和组
groupadd mysql
useradd -r -g mysql -s /bin/false mysql

# 切换到/tmp目录,解压
cd /tmp
tar zxvf mysql-5.6.44-linux-glibc2.12-x86_64.tar.gz

# 将解压后的目录移动到/usr/local目录下
cp -rf mysql-5.6.44-linux-glibc2.12-x86_64 /usr/local/mysql
chown mysql:mysql -R /usr/local/mysql

# 创建数据库目录和日志目录
mkdir -p /data/mysql3306/mysql3306
mkdir -p /data/mysql3306/logs
chown mysql:mysql -R /data

# 初始化数据目录
/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql/ --datadir=/data/mysql3306/mysql3306 --defaults-file=/etc/my3306.cnf --user=mysql

# 启动服务
/usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my3306.cnf --user=mysql &
 
# 连接数据库
/usr/local/mysql/bin/mysql -S /tmp/mysql3306.sock

# 配置环境变量
vim /etc/profile
# 添加内容:
export PATH=$PATH:/usr/local/mysql/bin
 
# 配置MySQL开机自动启动
cp support-files/mysql.server /etc/init.d/mysql.server
chmod 755 /etc/init.d/mysql.server
systemctl enable mysql.server

my3306.cnf 文件的内容:

[client]
port= 3306
socket= /tmp/mysql3306.sock
secure_auth= false
 
[mysqld]
port= 3306
socket= /tmp/mysql3306.sock
datadir= /data/mysql3306/mysql3306
#read_only= on
 
##--- GLOBAL ---##
character_set_server    = utf8
lower_case_table_names  = 1
log-output              = FILE
log-error               = /data/mysql3306/logs/mysql-error.log
#general_log
general_log_file        = /data/mysql3306/logs/mysql.log
pid-file                = /data/mysql3306/mysql.pid
slow-query-log          = 1
slow_query_log_file     = /data/mysql3306/logs/mysql-slow.log
tmpdir                  = /tmp/
long_query_time         = 2
innodb_force_recovery   = 0
#innodb_buffer_pool_dump_at_shutdown = 1
#innodb_buffer_pool_load_at_startup = 1
 
##--------------##
 
#thread_concurrency      = 8
thread_cache_size       = 51
table_open_cache        = 16384
open_files_limit        = 65535
table_definition_cache  = 16384
sort_buffer_size        = 2M
join_buffer_size        = 2M
read_buffer_size        = 2M
read_rnd_buffer_size    = 8M
key_buffer_size         = 32M
bulk_insert_buffer_size = 16M
myisam_sort_buffer_size = 64M
tmp_table_size      = 32M
max_heap_table_size     = 16M
query_cache_size       = 32MB
 
#gtid_mode=on
#log_slave_updates=1
#enforce_gtid_consistency=1
 
##--- NETWORK ---##
back_log                = 103
max-connections         = 512
max_connect_errors   = 100000
max_allowed_packet      = 32M
interactive_timeout     = 600
wait_timeout            = 600
skip-external-locking
#max_user_connections    = 0
external-locking        = FALSE
#skip-name-resolve
 
##--- REPL ---##
server-id               = 28703306
sync_binlog             = 1
log-bin                 = mysql-bin
binlog_format           = row
expire_logs_days        = 10
relay-log               = relay-log
replicate-ignore-db     = test
log_slave_updates       =1
#skip-slave-start
binlog_cache_size       =4M
max_binlog_cache_size   =8M
max_binlog_size         =1024M
 
##--- INNODB ---##
default_storage_engine          = InnoDB
innodb_data_file_path           = ibdata1:1024M:autoextend
innodb_buffer_pool_size         = 1040M
innodb_buffer_pool_instances    = 1
innodb_additional_mem_pool_size = 16M
innodb_log_files_in_group       = 2
innodb_log_file_size            = 256MB
innodb_log_buffer_size          = 16M
innodb_flush_log_at_trx_commit  = 2
innodb_lock_wait_timeout        = 30
innodb_flush_method             = O_DIRECT
innodb_max_dirty_pages_pct      = 75
innodb_io_capacity              = 200
innodb_thread_concurrency       = 32
innodb_open_files               = 65535
innodb_file_per_table           = 1
transaction_isolation           = REPEATABLE-READ
innodb_locks_unsafe_for_binlog  = 0
#innodb_purge_thread             = 4
[mysqldump]
quick
max_allowed_packet = 32M
 
[mysql]
auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates
default_character_set=utf8
 
[mysqlhotcopy]
interactive-timeout

使用 RPM 包安装

既可以下载 RPM 安装包进行手动安装,也可以通过 yum 工具进行安装。

使用 yum 工具

前提:虚拟机能够访问互联网。
特点:不需要手动卸载系统自带的 MariaDB,yum 工具会自动帮我们卸载。

配置 Yum 仓库

首先,下载配置 yum 仓库的 rpm 包:mysql80-community-release-el7-3.noarch.rpm
下载地址:https://dev.mysql.com/downloads/repo/yum/

然后将其上传到虚拟机的 /tmp 目录进行安装。

yum localinstall mysql80-community-release-el7-3.noarch.rpm
# 确认
yum repolist enabled | grep "mysql.*-community.*"

安装成功后,可以在 /etc/yum.repos.d/ 目录下找到两个新增的的 repo 文件:mysql-community.repomysql-community-source.repo
默认启用的是最新版本 MySQL 的 Yum Repository,如下所示,是 mysql-community.repo 文件中的部分内容:

[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

国外的 yum 源可能会比较慢,最好可以修改为国内的。比如可以将上面 5.7 的 baseurl 修改为网易的地址:http://mirrors.163.com/mysql/Downloads/MySQL-5.7/

若要安装其它版本的 MySQL ,则需要需改上面的配置文件,也可以使用下面的命令:

yum-config-manager --disable mysql80-community
yum-config-manager --enable mysql57-community

安装并启动

# 用yum安装
yum -y install mysql-community-server
# 启动MySQL服务
systemctl start mysqld.service
# 查看服务状态
systemctl status mysqld.service

若查看服务状态时,是下面的结果,说明 MySQL 服务没有设置为开机启动。

[root@localhost ~]# systemctl status mysqld.service
● mysqld.service - MySQL Community Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)
......

可使用下面的命令将 MySQL 服务设置为开机启动。

systemctl enable mysqld.service

手动安装

下载安装包

下载地址:https://dev.mysql.com/downloads/mysql/
操作系统:Red Hat Enterprise Linux / Oracle Linux
系统版本:Red Hat Enterprise Linux 7 / Oracle Linux 7 (x86, 64-bit)

可下载的 RPM Bundle:

MySQL-5.5.62-1.el7.x86_64.rpm-bundle.tar
MySQL-5.6.44-1.el7.x86_64.rpm-bundle.tar
mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar
mysql-8.0.16-2.el7.x86_64.rpm-bundle.tar

安装并启动

首先卸载系统自带的 MariaDB:

# 使用rpm卸载
rpm -qa | grep mariadb
rpm -evh --nodeps mariadb-libs-5.5.44-2.el7.centos.x86_64

将下载的安装包上传到虚拟机,并安装

# 版本5.5
tar -xvf MySQL-5.5.62-1.el7.x86_64.rpm-bundle.tar
rpm -ivh MySQL-server-5.5.62-1.el7.x86_64.rpm
rpm -ivh MySQL-client-5.5.62-1.el7.x86_64.rpm

# 版本5.6
tar xvf MySQL-5.6.44-1.el7.x86_64.rpm-bundle.tar
rpm -ivh MySQL-server-5.6.44-1.el7.x86_64.rpm
rpm -ivh MySQL-client-5.6.44-1.el7.x86_64.rpm

# 版本5.7
tar xvf mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar
rpm -ivh mysql-community-common-5.7.26-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.26-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.7.26-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.7.26-1.el7.x86_64.rpm

# 版本5.8
tar xvf mysql-8.0.16-2.el7.x86_64.rpm-bundle.tar
rpm -ivh mysql-community-common-8.0.16-2.el7.x86_64.rpm
rpm -ivh mysql-community-libs-8.0.16-2.el7.x86_64.rpm
rpm -ivh mysql-community-client-8.0.16-2.el7.x86_64.rpm
rpm -ivh mysql-community-server-8.0.16-2.el7.x86_64.rpm

启动服务:

# 5.7之前的版本
mysqld_safe --user=mysql &
# 5.7版本,及之后的版本
systemctl start mysqld.service

通过源码安装

有两种方法来使用源码安装 MySQL:

  • 使用一种标准的 MySQL 源码安装。标准的可用源码文件被压缩成 tar 文件,zip 文件或 RPM 包。文件名的格式为 mysql-version.tar.gz,mysql-version.zip 或 mysql-version.rpm。其中 version 是版本号比例如 5.7.25。
  • 使用 MySQL 开发树。

使用源码安装所需的前提条件:

  • CMake,用来在所有平台上构建框架,可以从 http://www.cmake.org 来下载 CMake
  • 一个好的 make 程序,强烈建议使用 GNU make 3.75 或更高版本。可以从 http://www.gnu.org/software/make/ 下载 GNU make。
  • ANSI C++ 编译器。可以对 force_unsupported_compiler 的描述。
  • ncurses 库,可从 https://www.gnu.org/software/ncurses/ncurses.html 下载。
  • 为了构建 MySQL 需要 Boost C++ 库(但不使用它)。Boost 1.59.0 必须被安装。为了获得 Boost 与它的安装指令,可以访问 https://www.boost.org/ 网址。(MySQL 5.7)
  • 足够的空闲内存。
  • 如果要运行测试脚本那么就需要安装 perl。大多数类 Unix 的系统已经包含了 perl。在 windows 上可以使用 ActiveState Perl。

如果使用标准源码安装,还需要下面的工具来解压源码文件:

  • 对于一个 .tar.gz 的压缩 tar 文件,GNU gunzip 命令可以用来解压,tar 命令用来 unpack。如果你的 tar 命令支持 z 选项,它可以用来解压与 unpack 文件。
  • 对于一个 .zip 归档文件,winzip 或另外的工具可以读取.zip 文件。
  • 对于一个 .rpm 的 RPM 包,rpmbuild 命令用来对构建的发布版本进行 unpack 操作。

若使用开发源码树来安装 MySQL,需要安装以下额外的工具:

  • 需要 Git 版本控制系统来获得开发源代码。https://help.github.com/en 提供了指令在不同平台上下载与安装 Git。MySQL 官方组织已经于 2014 年 9 月加入了 GitHub。
  • bison 2.1 或更高版本,可以从 http://www.gnu.org/software/bison/ 网址进行下载(1 版本不再支持),尽可能的使用最新版本。
  • 在 Solaris 平台上,除了 bison 之外 m4 必须被安装。m4 可以从 http://www.gnu.org/software/m4/ 网址下载。

使用标准源码安装

将源码包 mysql-version.tar.gz 上传到虚拟机,然后按照下面的步骤进行安装。

5.7 之前的版本

# 卸载系统自带的mariadb
rpm -qa | grep mariadb
rpm -evh --nodeps mariadb-libs-5.5.44-2.el7.centos.x86_64

# 安装所需的工具包
yum -y install cmake* make* gcc* gcc-c++ ncurses* ncurses-devel* perl*

# 创建mysql用户
groupadd mysql
useradd -r -g mysql -s /bin/false mysql

# 解压并安装
tar zxvf mysql-5.5.62.tar.gz
cd mysql-5.5.62/
#tar zxvf mysql-5.6.45.tar.gz
#cd mysql-5.6.45/
mkdir bld
cd bld
cmake ..
make
make install

# 初始化
cd /usr/local/mysql

chown -R mysql:mysql .
scripts/mysql_install_db --user=mysql
chown -R root .
chown -R mysql data

# 启动服务
cp support-files/my-medium.cnf /etc/my.cnf ##5.5
bin/mysqld_safe --user=mysql &

# 配置开机启动
cp support-files/mysql.server /etc/init.d/mysql.server
chmod 755 /etc/init.d/mysql.server
systemctl enable mysql.server

5.7 版本

下载 boost_1_59_0.tar.gz 并上传到虚拟机的 /tmp 目录。
下载地址:https://jaist.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz

# 卸载系统自带的mariadb
rpm -qa | grep mariadb
rpm -evh --nodeps mariadb-libs-5.5.44-2.el7.centos.x86_64

# 安装所需的工具包
yum -y install cmake* make* gcc* gcc-c++ ncurses* ncurses-devel* perl*
cd /tmp
tar zxvf boost_1_59_0.tar.gz
mv boost_1_59_0 /usr/local/

# 创建mysql用户
groupadd mysql
useradd -r -g mysql -s /bin/false mysql

# 解压并安装
tar zxvf mysql-5.7.27.tar.gz
cd mysql-5.7.27/
mkdir bld
cd bld
cmake .. -DWITH_BOOST=/usr/local/boost_1_59_0
make
make install

# 初始化
cd /usr/local/mysql
mkdir mysql-files
chown mysql:mysql mysql-files
bin/mysqld --initialize --user=mysql
bin/mysql_ssl_rsa_setup

# 启动服务
bin/mysqld_safe --user=mysql &

# 配置开机启动
cp support-files/mysql.server /etc/init.d/mysql.server
chmod 755 /etc/init.d/mysql.server
systemctl enable mysql.server

使用开发树安装

从 GitHub 获取源码进行安装,与使用标准源码安装类似,只是获取源码的方式不一样。

# 安装Git
yum -y install git
# 克隆MySQL仓库到本地
git clone https://github.com/mysql/mysql-server.git
# 切换到mysql-server目录
cd mysql-server

连接 MySQL

启动成功之后就可以使用 mysql 命令进行连接了。
MySQL 5.7 之前的版本可以在本地直接连接,不需要输入密码。如下所示:

[root@localhost ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.62 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>

对于 5.7 及之后的版本:
a) 若 MySQL 是通过 rpm 包安装的,服务在启动时会创建超级账户 'root'@'localhost',并为其生成一个临时的密码,可通过下面的命令进行查看:

# 直接连接会报错
[root@localhost ~]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
# 查看临时密码
[root@localhost ~]# grep 'temporary password' /var/log/mysqld.log
# 用root账户连接,需输入密码
[root@localhost ~]# mysql -uroot -p

b) 若 MySQL 是通过通用的二进制包或者源码安装的,在初始化的时候会生成一个临时密码,在终端输出的日志中可以看到,如下所示:

[root@localhost mysql]# bin/mysqld --initialize --user=mysql
2019-07-22T08:51:10.366309Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-07-22T08:51:11.141264Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-07-22T08:51:11.245915Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-07-22T08:51:11.319143Z 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: dabbd8fc-ac5d-11e9-8579-000c29562563.
2019-07-22T08:51:11.324567Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-07-22T08:51:11.329523Z 1 [Note] A temporary password is generated for root@localhost: Zyreghpk3n_s

连接之后,需要将临时密码改掉之后才能进行其它操作:

mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';

参考文档

https://dev.mysql.com/doc/refman/5.5/en/
https://dev.mysql.com/doc/refman/5.6/en/
https://dev.mysql.com/doc/refman/5.7/en/
https://dev.mysql.com/doc/refman/8.0/en/
http://www.jydba.net/linux%E4%BD%BF%E7%94%A8%E6%BA%90%E7%A0%81%E6%9D%A5%E5%AE%89%E8%A3%85mysql-5-7/

  • MySQL

    MySQL 是一个关系型数据库管理系统,由瑞典 MySQL AB 公司开发,目前属于 Oracle 公司。MySQL 是最流行的关系型数据库管理系统之一。

    675 引用 • 535 回帖
2 操作
lingyundu 在 2020-09-29 18:35:41 更新了该帖
lingyundu 在 2020-09-29 16:03:32 更新了该帖

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...