作者:北京运维

常见的 MySQL 安装方式有如下三种:

  1. RPM 包方式:这种方式安装适合对数据库要求不太高的场合,安装速度快;
  2. 通用二进制包方式:安装速度相较于源码方式快,可以自定义安装目录。
  3. 源码编译安装:安装过程比较慢,机器性能不好的情况下,大约需要30分钟左右,通常适用于mysql定制化的安装,比如需要加入一些第三方的插件及依赖库等

环境说明

OS 版本 MySQL 版本
CentOS 7.5.1804 5.7.25

一、RPM 包方式安装

1.1 获取 RPM 包

访问 MySQL 官网,下载最新版 mysql5.7 的 rpm 包。

  • 点击 DOWNLOADS --> 点击 Community 社区版 --> 选择 MySQL Community Server

  • 选择 MySQL Community Server 5.7 -> 而后选择对应的软件平台版本

  • 选择下载 RPM Bundle 这里包含了所有 MySQL 的 RPM 包。

1.2 安装 MySQL

下载 Bundle 包解压以后,可以看到包含了所有 MySQL 相关的 RPM 包:

其中 client、common、libs、server 四个程序包是必须安装的:

  1. mysql-community-client-5.7.25-1.el7.x86_64.rpm
  2. mysql-community-common-5.7.25-1.el7.x86_64.rpm
  3. mysql-community-libs-5.7.25-1.el7.x86_64.rpm
  4. mysql-community-server-5.7.25-1.el7.x86_64.rpm

在执行安装之前,先检查是否已经安装过(CentOS7 以后默认安装的 mariadb)

  1. $ rpm -qa|egrep "mariadb|mysql"
  2. mariadb-libs-5.5.60-1.el7_5.x86_64
  3. # 我这里存在 mariadb-libs 会造成冲突,所以卸载掉
  4. rpm -e --nodeps mariadb-libs-5.5.60-1.el7_5.x86_64
  5. # 卸载之后就可以进行安装使用 yum 或者 rpm -ivh
  6. $ yum -y install mysql-community-client-5.7.25-1.el7.x86_64.rpm mysql-community-common-5.7.25-1.el7.x86_64.rpm mysql-community-libs-5.7.25-1.el7.x86_64.rpm mysql-community-server-5.7.25-1.el7.x86_64.rpm

安装完成后 MySQL 的默认配置文件为 /etc/my.cnf 接下来我们就可以启动 MySQL 啦

  1. $ systemctl start mysqld.service
  2. $ systemctl enable mysqld.service
  3. $ systemctl status mysqld.service

1.3 修改 MySQL 默认密码

MySQL 5.7 以后,不在允许使用空密码进行登录,默认会初始化一个密码到 MySQL Error 日志中,配置参数 log-error= 指定的文件。

  1. $ cat /var/log/mysqld.log | grep password
  2. 2019-03-20T02:44:49.359004Z 1 [Note] A temporary password is generated for root@localhost: /qrsXHttL6Mr

连接实例并修改默认密码

  1. $ mysql -uroot -p
  2. mysql: [Warning] Using a password on the command line interface can be insecure.
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 2
  5. Server version: 5.7.25
  6. Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.
  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  11. mysql> set password for 'root'@'localhost'=password('MyNewPass4!');
  12. Query OK, 0 rows affected, 1 warning (0.00 sec)

以后通过 update set 语句修改密码:

  1. mysql> use mysql;
  2. mysql> update user set authentication_string=PASSWORD('NewPass@2019') where user='root';
  3. mysql> flush privileges;

注意:mysql 5.7 默认密码检查策略要求密码必须包含:大小写字母、数字和特殊符号,并且长度不能少于8位。否则会提示 ERROR 1819 (HY000): Your password does not satisfy the current policy requirements 错误。查看 MySQL 密码策略

二、通用二进制包方式安装

官方文档:https://dev.mysql.com/doc/refman/5.7/en/binary-installation.html

2.1 获取安装包

选择 Linux - generic 64 位安装包

2.2 安装 MySQL

MySQL 依赖于 libaio 库。 如果未在本地安装此库,则数据目录初始化和后续服务器启动步骤将失败。 如有必要,请使用适当的包管理器进行安装。 例如,在基于Yum 的系统上:

  1. $ yum -y install libaio

创建 MySQL 用户和组

  1. $ groupadd mysql
  2. $ useradd -r -g mysql -s /bin/false mysql

解压到指定目录

  1. $ tar xf mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
  2. $ cd /usr/local/
  3. $ ln -sv mysql-5.7.25-linux-glibc2.12-x86_64/ mysql

修改解压目录下所有文件属主及属组

  1. $ cd /usr/local/mysql
  2. $ chown -R root.mysql ./*

创建数据目录,以 /data/mysql/data 为例

  1. $ mkdir -pv /data/mysql/{data,log}
  2. $ chown -R mysql.mysql /data/mysql

准备 MySQL 配置文件,我这里用的是在线工具生成的 my.cnf 文件,工具链接

  1. $ cat /etc/my.cnf
  2. [client]
  3. port = 3306
  4. socket = /data/mysql/mysql.sock
  5. [mysqld]
  6. port = 3306
  7. socket = /data/mysql/mysql.sock
  8. pid_file = /var/run/mysql.pid
  9. datadir = /data/mysql/data
  10. basedir = /usr/local/mysql
  11. default_storage_engine = InnoDB
  12. max_allowed_packet = 128M
  13. max_connections = 2048
  14. open_files_limit = 65535
  15. skip-name-resolve
  16. lower_case_table_names=1
  17. character-set-server = utf8mb4
  18. collation-server = utf8mb4_unicode_ci
  19. init_connect='SET NAMES utf8mb4'
  20. innodb_buffer_pool_size = 128M
  21. innodb_log_file_size = 128M
  22. innodb_file_per_table = 1
  23. innodb_flush_log_at_trx_commit = 0
  24. key_buffer_size = 16M
  25. log-error = /data/mysql/log/mysql_error.log
  26. log-bin = /data/mysql/log/mysql_bin.log
  27. slow_query_log = 1
  28. slow_query_log_file = /data/mysql/log/mysql_slow_query.log
  29. long_query_time = 5
  30. tmp_table_size = 16M
  31. max_heap_table_size = 16M
  32. query_cache_type = 0
  33. query_cache_size = 0
  34. server-id=1

复制启动脚本

  1. $ cp support-files/mysql.server /etc/init.d/mysqld
  2. $ chmod +x /etc/init.d/mysqld
  3. $ chkconfig --add mysqld

初始化数据库

  1. $ ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/data

此时会生成一个临时密码,可以在 mysql_error.log 文件找到

  1. $ grep password /data/mysql/log/mysql_error.log
  2. 2019-03-20T05:37:28.267207Z 1 [Note] A temporary password is generated for root@localhost: H_wgkXR&f1=t

生成 SSL

  1. $ ./bin/mysql_ssl_rsa_setup --basedir=/usr/local/mysql --datadir=/data/mysql/data/

启动 MySQL 服务

  1. $ service mysqld start
  2. $ ss -tnlp | grep 3306

配置 MySQL 环境变量

  1. $ vim /etc/profile.d/mysql.sh
  2. export PATH=/usr/local/mysql/bin:$PATH
  3. $ source /etc/profile.d/mysql.sh

2.3 MySQL 用户初始化

  1. $ mysql_secure_installation
  2. Securing the MySQL server deployment.
  3. Enter password for user root: # 输入初始密码,在错误日志中
  4. The existing password for the user account root has expired. Please set a new password.
  5. New password: # 输入新密码
  6. Re-enter new password: # 输入新密码
  7. VALIDATE PASSWORD PLUGIN can be used to test passwords
  8. and improve security. It checks the strength of password
  9. and allows the users to set only those passwords which are
  10. secure enough. Would you like to setup VALIDATE PASSWORD plugin?
  11. Press y|Y for Yes, any other key for No: Y # 是否启用密码安全策略
  12. There are three levels of password validation policy:
  13. LOW Length >= 8
  14. MEDIUM Length >= 8, numeric, mixed case, and special characters
  15. STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
  16. Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2 # 设置密码复杂度
  17. Using existing password for root.
  18. Estimated strength of the password: 100
  19. Change the password for root ? ((Press y|Y for Yes, any other key for No) : N # 是否修改 root 密码,刚才已经新设置了,输入 N
  20. ... skipping.
  21. By default, a MySQL installation has an anonymous user,
  22. allowing anyone to log into MySQL without having to have
  23. a user account created for them. This is intended only for
  24. testing, and to make the installation go a bit smoother.
  25. You should remove them before moving into a production
  26. environment.
  27. Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y # 是否移除匿名用户
  28. Success.
  29. Normally, root should only be allowed to connect from
  30. 'localhost'. This ensures that someone cannot guess at
  31. the root password from the network.
  32. Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y # 是否禁止 root 用户远程登录
  33. Success.
  34. By default, MySQL comes with a database named 'test' that
  35. anyone can access. This is also intended only for testing,
  36. and should be removed before moving into a production
  37. environment.
  38. Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y # 是否删除 test 测试数据库
  39. - Dropping test database...
  40. Success.
  41. - Removing privileges on test database...
  42. Success.
  43. Reloading the privilege tables will ensure that all changes
  44. made so far will take effect immediately.
  45. Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y # 是否刷新权限表
  46. Success.
  47. All done!

验证 MySQL 安装

  1. mysqladmin version -u root -p

三、源码编译方式安装

3.1 安装依赖包

  1. $ yum -y install gcc gcc-c++ ncurses ncurses-devel cmake bison

安装 Boost 库,获取程序包请访问 Boost 官网

  1. $ cd /usr/local/src/
  2. $ wget https://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
  3. $ tar xf boost_1_59_0.tar.gz -C /usr/local/

3.2 获取 MySQL 源代码包

  1. $ cd /usr/local/src/
  2. $ wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.25.tar.gz

3.3 创建 MySQL 用户组

  1. $ groupadd mysql
  2. $ useradd -r -g mysql -s /bin/false mysql

3.4 预编译

  1. $ tar xf mysql-5.7.25.tar.gz
  2. $ cd mysql-5.7.25
  3. $ cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
  4. -DMYSQL_DATADIR=/data/mysql/data \
  5. -DENABLED_LOCAL_INFILE=1 \
  6. -DWITH_BOOST=/usr/local/boost_1_59_0 \
  7. -DDEFAULT_CHARSET=utf8 \
  8. -DDEFAULT_COLLATION=utf8_general_ci \
  9. -DEXTRA_CHARSETS=all \
  10. -DWITH_MYISAM_STORAGE_ENGINE=1 \
  11. -DWITH_INNOBASE_STORAGE_ENGINE=1 \
  12. -DWITH_ARCHIVE_STORAGE_ENGINE=1 \
  13. -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
  14. -DWITH_FEDERATED_STORAGE_ENGINE=1 \
  15. -DWITH_PARTITION_STORAGE_ENGINE=1 \
  16. -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
  17. -DWITH_CSV_STORAGE_ENGINE=1 \
  18. -DWITH_HEAP_STORAGE_ENGINE=1 \
  19. -DWITH_MYISAMMRG_STORAGE_ENGINE=1 \
  20. -DWITH_ZLIB=system \
  21. -DWITH_EMBEDDED_SERVER=1

更多 cmake 指令参考官方文档

3.5 编译安装

  1. $ make -j `grep processor /proc/cpuinfo | wc -l`
  2. $ make install

3.6 配置文件

准备 MySQL 配置文件,我这里用的是在线工具生成的 my.cnf 文件,工具链接

  1. $ cat /etc/my.cnf
  2. [client]
  3. port = 3306
  4. socket = /data/mysql/mysql.sock
  5. [mysqld]
  6. port = 3306
  7. socket = /data/mysql/mysql.sock
  8. pid_file = /var/run/mysql.pid
  9. datadir = /data/mysql/data
  10. basedir = /usr/local/mysql
  11. default_storage_engine = InnoDB
  12. max_allowed_packet = 128M
  13. max_connections = 2048
  14. open_files_limit = 65535
  15. skip-name-resolve
  16. lower_case_table_names=1
  17. character-set-server = utf8mb4
  18. collation-server = utf8mb4_unicode_ci
  19. init_connect='SET NAMES utf8mb4'
  20. innodb_buffer_pool_size = 128M
  21. innodb_log_file_size = 128M
  22. innodb_file_per_table = 1
  23. innodb_flush_log_at_trx_commit = 0
  24. key_buffer_size = 16M
  25. log-error = /data/mysql/log/mysql_error.log
  26. log-bin = /data/mysql/log/mysql_bin.log
  27. slow_query_log = 1
  28. slow_query_log_file = /data/mysql/log/mysql_slow_query.log
  29. long_query_time = 5
  30. tmp_table_size = 16M
  31. max_heap_table_size = 16M
  32. query_cache_type = 0
  33. query_cache_size = 0
  34. server-id=1

创建数据目录

  1. $ mkdir -pv /data/mysql/{log,data}
  2. $ chown -R mysql.mysql /data/mysql/

3.7 初始化

  1. $ cd /usr/local/mysql/
  2. $ ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/data/

3.8 设置启动脚本配置环境变量

  1. $ cp support-files/mysql.server /etc/init.d/mysqld
  2. $ chmod +x /etc/init.d/mysqld
  3. $ echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
  4. source /etc/profile.d/mysql.sh

3.9 启动数据库

  1. $ systemctl enable mysqld
  2. $ systemctl start mysqld
  3. $ systemctl status mysqld
  4. $ ss -tnlp|grep 3306
  5. $ ps aux|grep mysql

3.10 初始化用户

与二进制方式一样,初始密码在错误日志内。

  1. $ mysql_secure_installation

3.11 验证 MySQL

  1. $ mysqladmin version -uroot -p

以上就是 MySQL 5.7 版本的三种安装方式,欢迎大家多留言交流。

实战三种方式部署 MySQL5.7的更多相关文章

  1. hive学习(一)hive架构及hive3.1.1三种方式部署安装

    1.hive简介 logo 是一个身体像蜜蜂,头是大象的家伙,相当可爱. Hive是一个数据仓库基础工具在Hadoop中用来处理结构化数据.它架构在Hadoop之上,总归为大数据,并使得查询和分析方便 ...

  2. Spark部署三种方式介绍:YARN模式、Standalone模式、HA模式

    参考自:Spark部署三种方式介绍:YARN模式.Standalone模式.HA模式http://www.aboutyun.com/forum.php?mod=viewthread&tid=7 ...

  3. Apache Spark探秘:三种分布式部署方式比较

    转自:链接地址: http://dongxicheng.org/framework-on-yarn/apache-spark-comparing-three-deploying-ways/     目 ...

  4. 在Linux安装配置Tomcat 并部署web应用 ( 三种方式 )

    系统版本:centos6.5版本 java版本:1.7 一.准备工作 1.java -version 检查是否有java环境,没有则需要去安装并配置到环境变量中. 2.下载tomcat包,下载地址:h ...

  5. 关于tomcat部署应用的三种方式

    关于tomcat部署应用虽然不是一个经常的操作,因为一旦选择了一种部署方式,我们其他的应用就会不经大脑的使用这种既定模式, 如果不使用这种部署方式,但是对于其他的部署方式不是很清楚的话,很容易抓瞎,所 ...

  6. Tomcat,eclipse热部署的三种方式

    热部署是指在你修改项目BUG的时候对JSP或JAVA类进行了修改在不重启WEB服务器前提下能让修改生效.但是对配置文件的修改除外! 怎么说呢?热部署其实用的算少了,热部署怎么说都是个人部署的,大点的公 ...

  7. Tomcat热部署的三种方式

    原文地址:https://blog.csdn.net/nlwangxin/article/details/49734659热部署是指在你修改项目BUG的时候对JSP或JAVA类进行了修改在不重启WEB ...

  8. 在Tomcat中部署web项目的三种方式

    搬瓦工搭建SS教程 SSR免费节点:http://www.xiaokeli.me 在这里介绍在Tomcat中部署web项目的三种方式: 1.部署解包的webapp目录 2.打包的war文件 3.Man ...

  9. #Eclipse web工程 部署 三种方式 3

    Eclipse web工程 部署 三种方式 3.热部署 在Eclipse中,实现 修改一个.java文件或者.jsp文件甚至是配置文件,不需要重启WEB服务器的前提下让修改生效,实现tomcat自动加 ...

随机推荐

  1. GIT速成

    安装工具与使用工具: GIT工具 :https://www.git-scm.com/download/ WINGDOWS图形界面工具:https://download.tortoisegit.org/ ...

  2. Redis redis-trib集群配置

    redis文档:http://doc.redisfans.com/ 参考:https://www.cnblogs.com/wuxl360/p/5920330.html http://www.cnblo ...

  3. js判断字符串出现的次数

    // 判断substr字符串在str中出现的次数 isIgnore是否忽略大小写! function countSubstr(str, substr, isIgnore) { var count; v ...

  4. leetCode题解之Reshape the Matrix

    1.题目描述 2.分析 使用了一个队列. 3.代码 vector<vector<int>> matrixReshape(vector<vector<int>& ...

  5. web应用服务端cache策略初探

    一般来说,网站随着访问量以及数据库的增大,访问速度将会越来越慢,如何优化这个响应速度,增大用户支持容量是网站从小到中,到大的必经之路. 你也可能听说过对于大型web站点一般严重依赖于cache来弹性放 ...

  6. js获取元素显示隐藏的当前状态

    js获取元素显示隐藏的当前状态 // CSS var display = $("."+cls).css("display"); if(display == &q ...

  7. 用UITextView模拟UITextField的placeHolder

    用UITextView模拟UITextField的placeHolder 效果: 源码: // // ViewController.m // TextView // // Created by You ...

  8. [翻译] UIImageView-Letters

    UIImageView-Letters https://github.com/bachonk/UIImageView-Letters An easy, helpful UIImageView cate ...

  9. 17 汽车服务工程 李腾飞 MP4

  10. 解决由于显卡驱动BUG导致桌面右键卡顿的问题:bat文件源码

    @ ECHO OFF%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe&q ...