原文链接:https://www.modb.pro/db/23103?cyn
摘要:mysql数据库的二进制安装方式
MYSQL 5.7.26 二进制版本安装
配置阿里云的yum环境
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOSBase.repo.backup
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum makecache
下载并上传软件至/data/soft
创建mysql包存放目录:
[root@linux26 ~]# mkdir -p /data/soft
[root@linux26 ~]# cd /data/soft/
[root@linux26 ~]# yum install -y lrzsz
使用rz命令上传安装包
[root@linux26 soft]# ls
mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
解压软件
解压包:
[root@linux26 soft]# tar xf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
移动到/data下,目录为mysql(此mysql目录要不存在)
[root@linux26 soft]# mv mysql-5.7.26-linux-glibc2.12-x86_64 /data/mysql
清除原始环境中mariadb
查询是否安装了mariadb
[root@linux26 soft]# rpm -qa | grep mariadb
如果有,则根据查询出来的结果yum remover删除
[root@linux26 soft]# yum remove mariadb-libs
创建mysql用户
创建mysql用户
[root@linux26 soft]# useradd -s /sbin/nologin mysql
设置环境变量
vim /etc/profile
export PATH=/data/mysql/bin:$PATH
[root@linux26 soft]# source /etc/profile
[root@linux26 soft]# mysqld -V
mysqld Ver 5.7.26 for linux-glibc2.12 on x86_64 (MySQL Community Server (GPL))
创建数据路径及授权
1.添加一块新磁盘模拟数据盘
2.格式化并挂载磁盘
格式化盘:
[root@linux26 ~]# mkfs.xfs /dev/sdb
创建mysql数据库的数据文件路径:
[root@linux26 ~]# mkdir /data/mysql/data
获取新增磁盘的uuid
[root@linux26 ~]# blkid
把挂载的磁盘记录写入/etc/fstab,开机启动
vim /etc/fstab
UUID=95d55a96-1ca6-4070-a22e-eb6eb1a7462a /data/mysql/data xfs defaults 0 0
挂载磁盘:
[root@linux26 ~]# mount -a
查询挂载结果:
[root@linux26 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 94G 12G 83G 12% /
devtmpfs 1.4G 0 1.4G 0% /dev
tmpfs 1.4G 0 1.4G 0% /dev/shm
tmpfs 1.4G 9.1M 1.4G 1% /run
tmpfs 1.4G 0 1.4G 0% /sys/fs/cgroup
/dev/sda1 197M 158M 39M 81% /boot
tmpfs 280M 12K 280M 1% /run/user/42
tmpfs 280M 0 280M 0% /run/user/0
/dev/sdb 20G 33M 20G 1% /data/mysql/data
3.目录授权
[root@linux26 ~]# chown -R mysql.mysql /data/mysql
初始化数据库(创建系统数据)
mysql5.6 版本
初始化命令:/data/mysql/scripts/mysql_install_db
mysql5.7 版本
–initialize参数
正常使用–initialize参数,初始化数据库后会生成临时密码给与登录
[root@linux26 data]# mysqld --initialize --user=mysql --basedir=/data/mysql --datadir=/data/mysql/data
说明:
–initialize参数
(1)对于密码复杂度进行定制:要求密码要12位,并且有4种密码格式(比如,大小写字母、数字、特殊字符)
(2)密码过期时间:180天
(3)给root@localhost用户设置临时密码
如果在启动的时候出现报错,一般是缺少相关的依赖包,则需要对缺少的依赖包进行安装即可
[root@linux26 data]# mysqld --initialize --user=mysql --basedir=/data/mysql --datadir=/data/mysql/data
执行完成初始化的命令,会出现如下的提示:
2020-02-29T08:56:15.521948Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-02-29T08:56:15.695632Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-02-29T08:56:15.727546Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-02-29T08:56:15.801784Z 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: 57ecb8b9-5ad1-11ea-b70b-000c29375cdc.
2020-02-29T08:56:15.802632Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed’ cannot be opened.
2020-02-29T08:56:15.804654Z 1 [Note] A temporary password is generated for root@localhost: riY_<sKl<0qr
其中加粗部分就是生成的临时密码,后续需要修改密码
–initialize-insecure参数
使用–initialize-insecure参数初始化数据库就不会生成密码,直接msyqld就可以登录数据库
如果已经用–initialize参数初始化过数据库,需要先删除数据文件的目录:
[root@linux26 data]# rm -rf /data/mysql/data/*
使用–initialize-insecure进行初始化数据库
[root@linux26 data]# mysqld --initialize-insecure --user=mysql --basedir=/data/mysql --datadir=/data/mysql/data
2020-02-29T09:06:30.390965Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-02-29T09:06:30.524295Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-02-29T09:06:30.553929Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-02-29T09:06:30.612936Z 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: c661754e-5ad2-11ea-8dcc-000c29375cdc.
2020-02-29T09:06:30.613884Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed’ cannot be opened.
2020-02-29T09:06:30.614951Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
此时无需密码也可以登录数据库
设置my.cnf配置文件
简单的模板(本次暂不扩展对my.cnf的详细配置):
cat >/etc/my.cnf <<EOF
[mysqld]
user=mysql
basedir=/data/mysql
datadir=/data/mysql/data
socket=/tmp/mysql.sock
server_id=4
[mysql]
socket=/tmp/mysql.sock
EOF
设置mysql启动方式
sys-v(linux6的方法)
[root@linux26 data]# cp /data/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@linux26 data]# service mysqld restart
systemd(linux7的方法)
注意:如果已经用sys-v方式启动过数据库的话,需要先用sys-v方式关闭,才可以用以下的方式登录数据库
sys-v方式停止数据库
[root@linux26 data]# /etc/init.d/mysqld stop
配置systemd的文件
cat >/etc/systemd/system/mysqld.service <<EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/data/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
EOF
配置完成就可以用systemctl命令来控制mysql的启动和关闭
[root@linux26 data]# systemctl start mysqld
mysql数据库启动报错
类似:without updating PID错误
思路:
(1) 查看日志,目录为/data/mysql/data/主机名.err,主要看该日志最后部分有[ERROR]的上下文
(2) 可能有的报错情况:
/et/my.cnf 路径不对
/tmp/mysql.sock 文件修改过或者删除过
数据目录权限不是mysql
参数改错了
更多MySQL精品文章、案例解析:https://www.modb.pro/tag/mysql?cyn