一、参考文献和环境
1.本文参考自:https://www.cnblogs.com/qixing/p/12271499.html
环境准备:
2.安装系统centos7
3.下载mysql5.7.31 下载地址:https://downloads.mysql.com/archives/community/ 文件名:mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
二、mysql安装
1.卸载自带的mariadb和mysql
rpm -qa | grep mariadb
rpm -qa | grep mysql
2.如果没有,就可以安装mysql,如果有,需要先卸载(remove后为上面命令查询到的内容,全文件名,我这里没有,没法展示)
yum remove mariadb-xxx
3.解压文件,修改目录名方便配置
tar -zxvf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz -C /opt/soft/ cd /opt/soft mv mysql-5.7.31-linux-glibc2.12-x86_64 mysql-5.7.31
4.在/usr/local/目录下创建到/opt/soft/mysql-5.7.31的软链接
5.添加mysql用户,修改mysql目录权限,并用此用户执行应用
useradd -s /bin/false -M mysql cd /opt/soft chown -R mysql:mysql mysql-5.7.31
6.拷贝配置文件,将mysql的配置文件拷贝为/etc/目录下的my.cnf,并修改配置文件
vim /etc/my.cnf ,内容为
[mysqld] # binlog 配置 log-bin=/usr/local/mysql/logs/mysql-bin.log expire-logs-days=14 max-binlog-size=500M server-id=1 # GENERAL basedir=/usr/local/mysql datadir=/usr/local/mysql/data socket=/usr/local/mysql/mysql.sock user=mysql default-storage-engine=InnoDB character-set-server=utf8 lower_case_table_names = 1 explicit_defaults_for_timestamp=true [mysqld_safe] log-error=/usr/local/mysql/mysql-error.log pid-file=/usr/local/mysql/mysqld.pid [client] socket=/usr/local/mysql/mysql.sock [mysql] default-character-set=utf8 socket=/usr/local/mysql/mysql.sock
7.安装mysql,进入mysql目录执行以下命令
cd /opt/soft/mysql-5.7.31 bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
*若报错
执行:
yum install -y libaio
*若报错 说明在my.cnf中指定的binlog配置文件的logs文件夹不存在:
在/usr/local/mysql/下创建logs文件夹就行了,并改为mysql用户
再次执行脚本
出现以下信息,代表成功,要保存一下密码,
8.拷贝启动程序,将mysql的启动程序拷贝到/etc/init.d/目录下
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
9.安装完,启动mysql服务
service mysqld start
若报错:
说明mysql-error.log不存在,手动去创建,并修改权限
cd /opt/soft/mysql-5.7.31 touch mysql-error.log chown mysql:mysql mysql-error.log
再试启动,出现SUCCESS,说明启动成功
10.配置环境变量,编辑/etc/profile,方便在任何地方用mysql命令
vim /etc/profile
#mysql export MYSQL_HOME=/usr/local/mysql export PATH=$PATH:$MYSQL_HOME/bin
重新编译 /etc/profile
source /etc/profile
11.登录mysql,修改密码
首次登录没有密码,提示输入密码时,输入第7步安装时生成的密码:p5j2jfX7am.h
mysql -uroot -p
这里要先使用alter user重置密码,不然会报错,我这里 修改mysql root用户密码 为 sa
mysql> alter user 'root'@'localhost' identified by 'sa'; mysql> flush privileges;
至此本机登录密码修改完成,若是想让其他机器访问,需要配置远程访问:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '111111' WITH GRANT OPTION;
至此搭建mysql-5.7.31版本就完成了
三、mysql常用命令
service mysqld start #启动
service mysqld stop #关闭
service mysqld restart #重启
service mysqld status #查看运行状态