【一、安装包下载】
归档地址:https://downloads.mysql.com/archives/
https://dev.mysql.com/downloads/archive/
https://dev.mysql.com/downloads/mysql/
社区版归档地址:https://downloads.mysql.com/archives/community/ ==》推荐下载地址
https://downloads.mysql.com/archives/community/
如果是二进制包安装方式,大体安装命令序列如下:
shell> groupadd mysql
shell> useradd -r -g mysql -s /bin/false mysql
shell> cd /usr/local
shell> tar zxvf /path/to/mysql-VERSION-OS.tar.gz
shell> ln -s full-path-to-mysql-VERSION-OS mysql
shell> cd mysql
shell> mkdir mysql-files
shell> chown mysql:mysql mysql-files
shell> chmod 750 mysql-files
shell> bin/mysqld --initialize --user=mysql
shell> bin/mysql_ssl_rsa_setup
shell> bin/mysqld_safe --user=mysql &
# Next command is optional
shell> cp support-files/mysql.server /etc/init.d/mysql.server
参考:https://dev.mysql.com/doc/refman/5.7/en/binary-installation.html
【二、安装前环境检查与准备】
1.系统版本信息确认:lsb_release -a 与 uname -a 或 cat /etc/issue
[root@centos-node2 mysql]# lsb_release -a -- 查看系统版本
LSB Version: :core-4.1-amd64:core-4.1-noarch:cxx-4.1-amd64:cxx-4.1-noarch:desktop-4.1-amd64:desktop-4.1-noarch:languages-4.1-amd64:languages-4.1-noarch:printing-4.1-amd64:printing-4.1-noarch
Distributor ID: CentOS
Description: CentOS Linux release 7.7.1908 (Core)
Release: 7.7.1908
Codename: Core
[root@centos-node2 mysql]#
[root@centos-node2 mysql]# uname -a -- 查看内核版本
Linux centos-node2 3.10.0-1062.el7.x86_64 #1 SMP Wed Aug 7 18:08:02 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
[root@izbp1c527y2uc9f7yez4g4z mysql]# cat /etc/issue
2.关闭防火墙
[root@centos-node1 /] systemctl stop firewalld
3.文件上传下载工具lrzsz安装
[root@centos-node1 /] yum -y install lrzsz -- 安装rz/sz工具 yum install lrzsz
4.卸载之前旧版本以及残余文件
# 检查系统是否已经安装过mysql 和 mariadb,如果安装了,卸载掉:
# rpm 检查: rpm -qa|grep -i mysql -- 安装过会报出软件名:如果什么都没有,表示没有安装过mysql
[root@centos-node1 /] rpm -qal | grep mysql
# ps 检查:ps -ef|grep mysql|grep -v grep
# 卸载现有旧版本MySQL:rpm -qa|grep -i mysql | xargs rpm -ev --nodeps
# 检查 mariadb:rpm -qa | grep mariadb
# 卸载 mariadb:rpm -e mariadb-libs-5.5.64-1.el7.x86_64 --nodeps
# 查看卸载残留目录:where is mysql
# 强制删除残留:rm -rf /usr/share/mysql/
# 再次检查:rpm -qa|grep -i mysql -- 若返回空,则清空下载完毕
# 检查目录残余:find / -name mysql
# 删除残余目录:rm -rf mysql/
# 删除之前的/etc/my.cnf文件 或者 /etc/mysql 文件夹
5.依赖库libaio与其他服务安装
[root@centos-node1 /]# sudo yum install gcc
[root@centos-node1 /]# sudo yum install glibc-headers
[root@centos-node1 /]# sudo yum install gcc-c++
[root@centos-node1 /]# sudo yum install -y cmake gcc-c++* make ncurses-devel
[root@centos-node1 /]# sudo yum install -y vim net-tools perl
# mysql 依赖的安装包安装:libaio 、libnuma
[root@centos-node1 /]# yum search libaio
[root@centos-node1 /]# yum install libaio libaio-devel
# MySQL 5.7.19以及以后版本还要添加 libnuma 库的依赖包
[root@centos-node1 /]# yum search libnuma
[root@centos-node1 /]# sudo yum install -y numactl-libs.x86_64
【三、正式开始安装】
1.解压安装包到指定的安装目录下,并将目录重命名为你自己指定的名称
[root@centos-node1 /]# tar -zxvf /path/to/mysql-VERSION-OS.tar.gz -C /usr/local/soft/mysql/
# 将解压后文件夹命名为自己指定的名字
[root@centos-node1 /]# mv mysql-5.7.22-linux-glibc2.12-x86_64 mysql-5.7.22-linux
2.添加用户和用户组
# 确认之前为创建过mysql用户
[root@centos-node1 /]# groups mysql
groups: mysql: no such user
# 如果之前存在mysql用户,删除之前的用户和用户组
[root@centos-node1 /]# userdel mysql
[root@centos-node1 /]# groupdel mysql
# 添加mysql用户组
# 由于该用户仅用于所有权目的,而不是登录目的,因此useradd命令使用 -r 和 -s/bin/false 选项创建一个对服务器主机没有登录权限的用户。
[root@centos-node1 /]# groupadd mysql
[root@centos-node1 /]# useradd -r -g mysql -s /bin/false mysql
#【可选】把当前目录及其下面的所有文件的所拥有者修改为:mysql
如:
[root@centos-node1 /] sudo chown -R mysql /usr/local/software/mysql
[root@centos-node1 /] sudo chgrp -R mysql /usr/local/software/mysql
3.[可选操作]创建到安装目录的符号软链接
命令格式为: ln -s full-path-to-mysql-VERSION-OS mysql ,如:
[root@centos-node1 /]# ln -s /usr/local/software/mysql/mysql-5.7.22-linux mysql
说明:解压后的mysql-5.7.22文件夹下有如下:
bin:mysqld server, client和实用的程序
docs:信息格式的MySQL手册
man:Unix手册页
include:包含(头)文件
lib:依赖库
share:数据库安装的错误消息、字典和可能需要用到的SQL
support-files:其他支持文件的文件夹,含有脚本和示例配置文件
4.将mysql的bin目录到PATH系统环境变量中
如:export PATH=$PATH:/usr/local/mysql/bin
或:
[root@centos-node1 /] vim /etc/profile
在/etc/profile最后添加:
export MYSQL_HOME=/usr/local/soft/mysql/mysql-5.7.22/
export PATH=$MYSQL_HOME/bin:$PATH
# 保存退出,然后:source /etc/profile
【四、安装后相关设置】
1.初始化数据目录文件夹
二进制包方式安装需要手动初始化数据目录
数据目录的创建方式有两种:
1.使用在根目录下创建默认的mysql-files目录,初始化时如果找不到,则使用--basedir=和--datadir=命令选项指出
2.自定义位置创建文件夹,在my.cnf配置文件中使用
# 使用mysql专用账户登录
[root@centos-node1 /]# su mysql
# 创建数据目录,默认数据目录位置:/usr/local/mysql/data
[root@centos-node1 /]# cd /usr/local/software/mysql/mysql-5.7.22-linux
[root@centos-node1 /]# mkdir mysql-files -- 这是官方文档中的写法,实际使用一般会在/usr/local/mysql/路径下创建一个data目录作为数据文件夹
# 将目录用户和组所有权授予mysql用户和mysql组,并适当设置目录权限
命令格式:chmod -R mysql:mysql /path-to-datadir
[root@centos-node1 /]# chown mysql:mysql mysql-files
[root@centos-node1 /]# chmod 750 mysql-files
举例:
[root@centos-node1 bin]# chown mysql:mysql /usr/local/software/mysql/data/
[root@centos-node1 bin]# chmod 750 /usr/local/software/mysql/data/
# 进入安装目录的顶级目录下,即包含bin等子目录的解压后得到的目录文件夹,创建mysql-files目录并修改其权限
# 创建一个能够被系统变量 secure_file_priv定位到的目录,如:mysql-files目录
# mysql-files目录提供了一个方便的位置,可以用作secure_file_priv系统变量的值,该变量将导入和导出操作限制到特定目录。
说明:
# bin/mysqld --initialize --user=mysql ,安全初始化,会生成一个随机的初始化root密码,临时初始密码保存在:/usr/local/soft/mysql/{datadir}/mysqld.log 文件中
# 在这种情况下,密码被标记为过期时,您需要选择一个新密码。
# bin/mysqld --initialize-insecure --user=mysql ,不安全的方式安装初始化,不会生成root密码。这是不安全的;在将服务器投入生产使用之前,必须及时为帐户分配密码。
方式1:在配置文件my.cnf中指定 (MySQL 5.7.17往后的版本support-files目录中不会再含有my-default.cnf文件,需要自己手动创建该配置文件)
# 创建my.cnf配置文件
[root@centos01 /]# touch support-files/my_default.cnf
配置好my_default.cnf文件之后:
[root@centos01 /]# cp support-files/my-default.cnf /etc/my.cnf
**********************************************************************************************************************************************
对于非Windows平台,在服务器安装或数据目录初始化过程中不会创建默认选项文件。
按照第4.2.2.2节“使用选项文件”中的说明创建选项文件。大多数选项文件是纯文本文件,使用任何文本编辑器都可以去创建它。
如果没有选项文件,则服务器仅以其默认设置启动- 有关如何检查这些设置的信息,请参见第5.1.2节“服务器配置默认值”。
有关选项文件格式和语法的更多信息,请参见第4.2.2.2节“使用选项文件”。
在Unix和类Unix系统上,MySQL程序按照指定的顺序从下表所示的文件中读取启动选项(首先列出的文件先读取,然后读取的文件优先)
/etc/my.cnf Global options
/etc/mysql/my.cnf Global options
SYSCONFDIR/my.cnf Global options
$MYSQL_HOME/my.cnf Server-specific options (server only)
defaults-extra-file The file specified with --defaults-extra-file, if any
~/.my.cnf User-specific options
~/.mylogin.cnf User-specific login path options (clients only)
# 生成所有mysqld选项和可配置系统变量的列表。它的输出包括默认选项和变量值。命令如下:
# mysqld --verbose --help
my.cnf 典型配置模板如下:
[client]
default-character-set = utf8
// ...
[mysql]
default-character-set = utf8
// ...
[mysqld]
port=3306
socket=/tmp/mysql.sock
basedir= ......
datadir= ......
// ...
[mysqldump]
quick
// ...
# 可以将配置好的文件复制到其他集群主机中去
[root@centos-node1 mysql-5.7.22]# scp -r support-files/my-default.cnf root@192.168.199.152:/usr/local/software/mysql/mysql-5.7.22/support-files
【注意】
如果没有任何选项文件,服务器将以其默认设置启动。
要显式指定MySQL服务器在启动时应该使用的选项,请将它们放在选项文件中,如/etc/my.cnf或/etc/MySQL/my.cnf。
例如,可以使用选项文件设置secure_file_priv系统变量.
5.7.17之后版本没有默认的这个文件,需要我们自己手动创建一个:touch my_default.cnf
在support-files目录下创建my_default.cnf,配置好之后
然后:cp support-files/my-default.cnf /etc/my.cnf -- 拷贝,如果提示是否覆盖,y
**********************************************************************************************************************************************
# 进行初始化
[root@centos-node1 mysql-5.7.22]# ./bin/mysqld --defaults-file=/etc/my.cnf --initialize --user=mysql
方式2:使用命令参数指定
使用服务器初始化数据目录,包括mysql数据库,其中包含初始的mysql授予表,这些表确定如何允许用户连接到服务器
[root@centos-node1 mysql-5.7.22]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/software/mysql/mysql-5.7.22 --datadir=/usr/local/software/mysql/mysql-5.7.22/mysql-files/
2. 安全连接设置
如果要部署自动支持安全连接的服务器,请使用mysql_ssl_rsa_setup实用程序创建默认ssl和rsa文件
[root@centos-node1 /] bin/mysql_ssl_rsa_setup
或者:
[root@centos-node1 mysql-5.7.22]# ./bin/mysql_ssl_rsa_setup --basedir=/usr/local/soft/mysql/mysql-5.7.22/ --datadir=/usr/local/soft/mysql/mysql-5.7.22/{datadir}/
或者:
[root@centos-node1 mysql-5.7.22]# ./bin/mysql_ssl_rsa_setup --defaults-file=/etc/my.cnf
3.[推荐操作]:把启动脚本放到开机初始化目录,添加进系统服务
如果要想使用service命令在任何地方启动mysql,而不是只能使用bin/mysqld_safe --user=mysql &启动的话则需要
将support-files目录下的启动脚本复制到/etc/init.d/目录下,否则报如下错误:
[root@centos01 bin]# service mysqld start
Redirecting to /bin/systemctl start mysqld.service
Failed to start mysqld.service: Unit not found.
# support-files文件夹中含有脚本和示例配置文件,您可以将其安装为/etc/init.d/mysql来自动启动和关闭mysql。
# 命令格式如下:shell> cp support-files/mysql.server /etc/init.d/mysql.server , 一般为了方便会复制为:/etc/init.d/mysql 文件
[root@centos-node1 /]# cp /usr/local/soft/mysql/mysql-5.7.22/support-files/mysql.server /etc/init.d/mysql
以后就可以使用如下方式启动mysql了:
# service mysql status
# service mysql start --- service mysql {start|stop|restart|status}
或者:
# systemctl start mysql --- systemctl {start|stop|restart|status} mysql
# 确认状态:service mysql status
[root@izbp1c527y2uc9f7yez4g4z support-files]# service mysql status
MySQL running (6966) [ OK ]
# 重启:service mysqld restart
# 关闭:service mysql stop
4.启动mysql服务
# 方反1:推荐
[root@centos-node1 mysql]# service mysql status
MySQL is not running [失败]
[root@centos-node1 mysql]# service mysql start
Starting MySQL..................... [ 确定 ]
[root@centos-node1 mysql]# service mysql status
MySQL running (43744) [ 确定 ]
# 方法2:如果安装包含mysqld_safe,可以如下方式启动:
[root@centos01 bin]# bin/mysqld_safe --user=mysql &
# 方法3:该方法会让窗口停住,必须开启另一个窗口进行其他操作,不建议使用
[root@centos01 bin]# ./mysqld --user=mysql
5.登录mysql,修改密码
密码为初始密码:临时初始密码保存在:/usr/local/soft/mysql/data/mysqld.log
如:# vim /usr/local/soft/mysql/data/mysqld.log
# 登录:mysql -u root -p ,输入mysqld.log中的临时密码
[root@centos-node1 bin]# mysql -u root -p
# 修改初始密码并使得密码永久有效
mysql> alter user 'root'@'localhost' identified by 'root-password';
mysql> alter user 'root'@'localhost' password expire never;
或者设置密码:
mysql> set password = password ('123456');
还可以:ALTER USER 'root'@'localhost' IDENTIFIED BY 'root123' PASSWORD EXPIRE NEVER;
# 授权root用户所有权限:grant all privileges on *.* to 'root'@'localhost' identified by '123456';
mysql> grant all privileges on *.* to 'root'@'localhost' identified by '123456';
mysql> flush privileges;
# 退出
mysql> exit
Bye
6.添加远程访问权限
如果不设置,navcat等远程工具连接会抛出编号为1130的错误!
[root@centos-node1 bin]# mysql -u root -p
mysql> use mysql
mysql> select host,user from user where user='root';
mysql> update user set host='%' where user='root';
mysql> flush privileges; -- 使修改生效
7.添加防火前开放端口
# 查看防火墙开启状态
[root@centos-node1 /]# sudo systemctl status firewalld
[root@centos-node1 /]# sudo systemctl stop firewalld
[root@centos-node1 /]# sudo systemctl start firewalld
或者:
# 查看防火墙状态:firewall-cmd --state
[root@centos-node1 /]# firewall-cmd --state
# 开启防火墙:systemctl start firewalld
# 关闭防火墙:systemctl stop firewalld
# 查看目前已经开放端口:firewall-cmd --list-all
[root@centos-node1 /]# firewall-cmd --list-all
# 添加要开放的端口:firewall-cmd --zone=public --add-port=3306/tcp --permanent
[root@centos-node1 /]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
success
# 重新载入防火墙配置:firewall-cmd --reload
[root@centos-node1 /]# firewall-cmd --reload
# 注:如果是阿里云服务器上安装,还需要添加安全组
# 重启mysql生效
# service mysql stop
# service mysql start 或者直接 service mysql restart
8.设置开机启动mysql
# 授权:chmod +x /etc/init.d/mysql
[root@centos-node1 /]# chmod +x /etc/init.d/mysql
# 设为开机启动:chkconfig --add mysql
[root@centos-node1 /]# chkconfig --add mysql
===========================================【常见安装报错】=========================================
1.为创建data目录
[root@izbp1c527y2uc9f7yez4g4z bin]# ./mysqld --initialize --user=mysql
mysqld: Can't create directory '/usr/local/mysql/data/' (Errcode: 2 - No such file or directory)
2019-10-20T08:51:15.948807Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-10-20T08:51:15.949148Z 0 [ERROR] Can't find error-message file '/usr/local/mysql/share/errmsg.sys'. Check error-message file location and 'lc-messages-dir' configuration directive.
2019-10-20T08:51:15.950442Z 0 [ERROR] Aborting
解决办法:
# bin/mysqld --initialize --user=dcuser --basedir=/u01/mysql/ --datadir=/u01/mysql_data/
【示例】
[root@izbp1c527y2uc9f7yez4g4z bin]# ./mysqld --user=mysql --initialize --basedir=/usr/local/soft/mysql/mysql-5.7.22/ --datadir=/usr/local/soft/mysql/data/
2019-10-20T09:17:38.359801Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-10-20T09:17:39.427994Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-10-20T09:17:39.547949Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-10-20T09:17:39.612842Z 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: 769bbdbb-f31a-11e9-91fb-00163e0a7a67.
2019-10-20T09:17:39.615466Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-10-20T09:17:39.616183Z 1 [Note] A temporary password is generated for root@localhost: ns(EX5?qeo1K
[root@izbp1c527y2uc9f7yez4g4z bin]#
2.安装好想去修改密码时,登录报错:Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
【示例】
[root@izbp1c527y2uc9f7yez4g4z tmp]# mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
解决办法:见一个文件的软连接,如下
# ln -s /usr/local/soft/mysql/tmp/mysql.sock /tmp
详细配置见官方文档:https://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
Section 5.1.6, “Server Command Options”
Section 5.1.7, “Server System Variables”.