环境:
本地系统:Windows 10 专业版
工具:Xshell , Xftf
阿里云服务器:CentOS7.7
- 安装前先看看,系统是否有自带的MySQL
rpm -qa | grep mysql • 1
- 如果有可以选择卸载
rpm -e mysql-libs-5.1.52-1.el6_0.1.x86_64 // 普通删除模式 rpm -e --nodeps mysql-libs-5.1.52-1.el6_0.1.x86_64 // 强力删除模式,如果使用上面命令删除时,提示有依赖的其它文件,则用该命令可以对其进行强力删除
- 检查否存在 mariadb 数据库,如有,卸载之,卸载同上
rpm -qa | grep mariadb
- 接下来就可以开始正式的安装了。上传mysql安装包到/opt下解压
cd /opt/mysql tar -zxvf mysql-5.7.28-linux-glibc2.12-x86_64.tar.gz
- 重新命名
mv mysql-5.7.28-linux-glibc2.12-x86_64 mysql-5.7.28
- 检查mysql组和用户是否存在,如无创建
cat /etc/group | grep mysql #类似 mysql:x:490: cat /etc/passwd | grep mysql #类似 mysql:x:496:490::/home/mysql:/bin/bash
- 以上为存在的情况,如无,执行添加命令
groupadd mysql useradd -r -g mysql mysql #useradd -r参数表示mysql用户是系统用户,不可用于登录系统
安装数据库
- 创建data目录
cd mysql-5.7.28 mkdir data
将/opt/mysql-5.7.28的所有者及所属组改为mysql
chown -R mysql.mysql /opt/mysql-5.7.28
在/opt/mysql-5.7.28/support-files目录下创建my_default.cnf
# For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html # *** DO NOT EDIT THIS FILE. It's a template which will be copied to the # *** default location during install, and will be replaced if you # *** upgrade to a newer version of MySQL. [mysqld] sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES basedir = /opt/mysql-5.7.28 datadir = /opt/mysql-5.7.28/data port = 3306 character-set-server=utf8 log-error = /opt/mysql-5.7.28/data/mysqld.log pid-file = /opt/mysql-5.7.28/data/mysqld.pid
拷贝,如果提示是否覆盖,y
cp support-files/my_default.cnf /etc/my.cnf • 1
初始化 mysqld
cd /opt/mysql-5.7.28 ./bin/mysqld --initialize --user=mysql --basedir=/opt/mysql-5.7.28/ --datadir=/opt/mysql-5.7.28/data/
初始化完成之后,查看日志
cat -n /opt/mysql-5.7.28/data/mysqld.log
在日志的最下面可以看到临时密码
9 2021-06-15T04:23:33.319897Z 1 [Note] A temporary password is generated for root@localhost: byvhz?Hre0Qt
把启动脚本放到开机初始化目录
cp support-files/mysql.server /etc/init.d/mysql • 1
启动mysql服务
service mysql start
登录mysql,密码为初始密码
cd /opt/mysql-5.7.28 ./bin/mysql -u root -p
修改密码
set password=password('root'); grant all privileges on *.* to root@'%' identified by 'root'; flush privileges;
添加远程访问权限
use mysql; update user set host='%' where user = 'root'; flush privileges;
重启mysql生效
service mysql stop service mysql start 或 service mysql restart • 1 • 2 • 3 • 4 • 5 • 6