文章目录
CentOS7.5离线安装MySQL5.7
我系统是centos7.5,安装的mysql版本是mysql-5.7.38
1. 下载MySQL5.7
下载地址:https://dev.mysql.com/downloads/mysql/#downloads
2. 删除原来安装的mysql
- 查询原来的MySQL
rpm -qa | grep -i mysql
find / -name mysql
- 删除查询出来的(这是我自己的删除)
rm -rf /home/mysql
rm -rf /home/mysql/mysql-5.6.33-linux-glibc2.5-x86_64/bin/mysql
rm -rf /home/mysql/mysql-5.6.33-linux-glibc2.5-x86_64/include/mysql
rm -rf /etc/selinx/targeted/active/modules/100/mysql
rm -rf /var/spool/mail/mysql
rm -rf /usr/lib64/mysql
rm -rf /usr/share/mysql
rm -rf /usr/local/mysql
rm -rf /usr/local/mysql/data/mysql
rm -rf /usr/local/mysql/data/mysql/mysql
rm -rf /usr/local/mysql/bin/mysql
rm -rf /usr/localmysql/include/mysql
rm -rf /usr/local/mysql/mysql
3. 检查自带的mariadb并删除
#查看
rpm -qa|grep mariadb
#删除
yum remove mariadb-libs
4. 把下载的mysql上传到/opt并解压缩
tar -zxvf mysql-5.7.38-el7-x86_64.tar.gz
5. 移动并重命名
mv mysql-5.7.38-el7-x86_64/ /usr/local/mysql
注意:
这步会将mysql-5.7.38-el7-x86_64重命名为mysql并放到/usr/local目录下
6. 修改权限为mysql组、用户
#添加组
groupadd mysql
#添加用户
useradd -g mysql mysql
#将文件的所有属性改为mysql用户
chown -R mysql /usr/local/mysql
查看添加的mysql用户情况:
id mysql
7. 配置my.cnf文件
进入目录/usr/local/mysql/support-files/发现没有my_default.cnf文件,自己从网上找了个,改成自己需要的配置,放到**/etc/my.cnf**
my.cnf
[mysqld]
#设置mysql的安装目录
basedir =/usr/local/mysql
#设置mysql数据库的数据存放目录
datadir = /usr/local/mysql/data
#设置端口
port = 3306
socket = /tmp/mysql.sock
#设置字符集
character-set-server=utf8
#日志存放目录
log-error = /usr/local/mysql/data/mysqld.log
pid-file = /usr/local/mysql/data/mysqld.pid
#允许时间类型的数据为零(去掉NO_ZERO_IN_DATE,NO_ZERO_DATE)
sql_mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
default-time_zone = '+8:00'
max_allowed_packet = 20M
8. 初始化mysql
在/usr/local/mysql目录下初始化mysql
cd /usr/local/mysql
./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
9. 查看日志获得mysql初始化密码
cat /usr/local/mysql/data/mysqld.log
10. 登录服务、修改密码
- 先启动服务:
进入目录:/usr/local/mysql/support-files
./mysql.server start
- 登录服务,修改密码
#进入/usr/local/mysql/bin目录
cd /usr/local/mysql/bin
#登录mysql
./mysql -uroot -p
输入第9步得到的mysql密码
mysql>set password for root@localhost = password('root');
mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;
mysql>FLUSH PRIVILEGES;
mysql>exit;
11. 启动mysql
#复制mysql.server到/etc/init.d/重命名为mysqld
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
#将mysql注册为开启启动的服务
chkconfig --add mysqld
12. mysql启停常用命令
- 启动mysql
service mysql start
- 停止mysql
service mysql stop
- 重启mysql
service mysql restart
- 查看mysql状态
service mysql status
也可以使用:
#启动
systemctl start mysql
#停止
systemctl stop mysql
#重启
systemctl restart mysql
#查看状态
systemctl status mysql