一、MySQL5.X安装(MySQL下载)
1. 下载并上传软件至/server/tools
[root@db01 /server/tools]# yum install -y lrzsz
[root@db01 ~]# mkdir -p /server/tools
[root@db01 ~]# cd /server/tools/
[root@db01 /server/tools]# ls
mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
2. 解压软件并放到/application/mysql
[root@db01 /server/tools]# tar xf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
[root@db01 ~]# mkdir /application
[root@db01 /server/tools]# mv mysql-5.7.26-linux-glibc2.12-x86_64 /application/mysql
3. 用户的创建处理原始环境
[root@db01 ~]# yum remove mariadb-libs-5.5.60-1.el7_5.x86_64 -y
[root@db01 ~]# rpm -qa |grep mariadb
[root@db01 ~]# useradd -s /sbin/nologin mysql
4. 设置环境变量
[root@db01 ~]# vim /etc/profile
export PATH=/application/mysql/bin:$PATH
[root@db01 ~]# source /etc/profile
[root@db01 ~]# mysql -V
mysql Ver 14.14 Distrib 5.7.26, for linux-glibc2.12 (x86_64) using EditLine wrapper
5. 创建数据路径并授权
[root@db01 ~]# mkfs.xfs /dev/sdc
[root@db01 ~]# mkdir /data
[root@db01 ~]# blkid
/dev/sda1: UUID="97df4d1c-8909-4dac-a1ef-660f90cd478c" TYPE="xfs"
/dev/sda2: UUID="dZFM3t-VSVZ-HHLk-wWif-S0Ud-mqpM-CqlnZE" TYPE="LVM2_member"
/dev/mapper/centos-root: UUID="c8889bbd-24cd-4674-9a49-206ff1e0ba0b" TYPE="xfs"
/dev/sdb: UUID="7f628485-47a6-4952-86e5-0b098184703c" TYPE="xfs"
/dev/mapper/centos-swap: UUID="f12a0b7c-312a-428c-ab43-d7ef88a7f981" TYPE="swap"
[root@db01 ~]# vim /etc/fstab
UUID=7f628485-47a6-4952-86e5-0b098184703c" /data xfs defaults 0 0
[root@db01 ~]# mount -a
[root@db01 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 475M 0 475M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.7M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/mapper/centos-root 28G 6.0G 22G 22% /
/dev/sdb 20G 166M 20G 1% /data
/dev/sda1 197M 115M 83M 59% /boot
tmpfs 98M 0 98M 0% /run/user/0
[root@db01 ~]# chown -R mysql.mysql /application/*
[root@db01 ~]# chown -R mysql.mysql /data
6. 初始化数据(创建系统数据)
# 5.6 版本
[root@db01 ~]# /application/mysql/scripts/mysql_install_db
# 5.7 版本
[root@db01 ~]# mkdir /data/mysql/data -p
[root@db01 ~]# chown -R mysql.mysql /data
[root@db01 ~]# mysqld --initialize --user=mysql --basedir=/application/mysql --datadir=/data/mysql/data
说明:
--initialize 参数:
1. 对于密码复杂度进行定制:12位,4种
2. 密码过期时间:180
3. 给root@localhost用户设置临时密码
[root@db01 ~]# mysqld --initialize-insecure --user=mysql --basedir=/application/mysql --datadir=/data/mysql/data
说明:
--initialize-insecure 参数:
无限制,无临时密码
7. 配置文件的准备
[root@db01 ~]# cat >/etc/my.cnf <<EOF
[mysqld]
user=mysql
basedir=/application/mysql
datadir=/data/mysql/data
socket=/tmp/mysql.sock
server_id=6
port=3306
[mysql]
socket=/tmp/mysql.sock
EOF
a. sys-v 方式启动
[root@db01 ~]# cp /application/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@db01 ~]# service mysqld restart
b. systemd方式启动
[root@db01 ~]# 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=/application/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
EOF
二、 报错解决方式
1. 错误处理
mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
解决:
[root@db01 ~]# yum install -y libaio-devel
2. without updating PID 类似错误
查看日志:
/data/mysql/data/主机名.err
可能情况:
/etc/my.cnf 路径不对等
/tmp/mysql.sock文件修改过 或 删除过
数据目录权限不是mysql
参数改错了
3. 管理员用户密码忘记
a. 关闭数据库
[root@db01 ~]# /etc/init.d/mysqld stop
Shutting down MySQL.. SUCCESS!
b. 启动数据库到维护模式
[root@db01 ~]# mysqld_safe --skip-grant-tables --skip-networking &
c. 登录并修改密码
mysql> alter user root@'localhost' identified by '1';
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
mysql> flush privileges;
mysql> alter user root@'localhost' identified by '1';
Query OK, 0 rows affected (0.01 sec)
d. 重新启动数据库即可
来源:CSDN
作者:South_Bay
链接:https://blog.csdn.net/South_Bay/article/details/104674236