一、安装步骤
1. 选择需要下载版本
下载地址:https://downloads.mysql.com/archives/community/
选择版本:根据个人需要的版本和机器位数进行选择
2. 下载安装包
点击Download下载按钮,复制下载地址使用wget下载,或者本地下载sftp上传
代码语言:shell复制wget https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.9-linux-glibc2.5-x86_64.tar.gz
3. 解压安装包到安装目录
代码语言:shell复制#解压安装包到/usr/local
tar -zxvf mysql-5.7.9-linux-glibc2.5-x86_64.tar.gz -C /usr/local/
#重命名目录
mv mysql-5.7.9-linux-glibc2.5-x86_64/ mysql
4. 创建用户组和用户并授予安装目录权限
代码语言:shell复制groupadd mysql
useradd -r -g mysql mysql
chown mysql:mysql -R /usr/local/mysql
chmod 755 /usr/local/mysql/
5. 修改mysql配置文件/etc/my.cnf,目录可以根据自己情况选择,但一定要记得授权
代码语言:text复制[mysqld]
bind-address=0.0.0.0
port=3306
user=mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
log-error=/usr/local/mysql/data/mysql.err
pid-file=/usr/local/mysql/data/mysql.pid
#character config
#
character_set_server=utf8mb4
#
symbolic-links=0
#
#explicit_defaults_for_timestamp=true
#
#
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
#start mysql service error log
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
代码语言:shell复制mkdir -p /var/log/mariadb/
chown mysql:mysql -R /var/log/mariadb/
chmod 755 /var/log/mariadb/
[mysqld_safe]中设置的两个日志,在后面启动服务失败时会记录详细错误信息,作用非常大,但一定记得创建目录和配置的目录授权。
6. 初始化mysql
代码语言:shell复制cd /usr/local/mysql/bin/
./mysqld --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ --user=mysql --initialize
这里使用的目录路径要与配置文件/etc/my.cnf匹配起来
7. 复制mysql server 到init.d进行系统管理和建立mysql软链方便使用
代码语言:shell复制cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
ln -s /usr/local/mysql/bin/mysql /usr/bin
8. 启动mysql服务
代码语言:shell复制service mysql start
启动如果报错,解决方案参考下面常见问题,或者去启动错误日志中查看具体错误信息
代码语言:shell复制cat /var/log/mariadb/mariadb.log
9. 查看初始密码
代码语言:shell复制cat /usr/local/mysql/data/mysql.err
10. 登陆并重置密码
代码语言:sql复制#mysql -uroot -p
SET PASSWORD = PASSWORD('123456');
ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
FLUSH PRIVILEGES;
11. 如果你需要开启远程访问的话
代码语言:sql复制use mysql;
update user set host = '%' where user = 'root';
FLUSH PRIVILEGES;
收工!
二、常见问题
1. 报错信息:error while loading shared libraries: libaio.so.1: cannot open shared object file: No such
代码语言:shell复制yum install -y libaio
2. 报错信息:error while loading shared libraries: .so.5: cannot open shared object file: No such
代码语言:shell复制yum install libncurses*
3.报错信息:Starting MySQL. ERROR! The server quit without updating PID file (/usr/local/mysql/data/mysql.pid).
这个问题出现频率非常高,配置文件错误,目录不存在,目录无权限 等等原因都会出现该报错,排查方案优先看日志
log-error=/var/log/mariadb/mariadb.log,日志中我们可以找的具体的错误信息。
如果你遇到其他问题,可留言我们一起研究