centos7.9离线安装mysql5.7.40(本文使用initialize-insecure安装方法)
一、卸载CentOS7系统自带mariadb
# 查看系统自带的Mariadb
[root@NIWAY-190 ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64
# 卸载系统自带的Mariadb
[root@NIWAY-190 ~]# rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64
# 删除etc目录下的my.cnf
[root@NIWAY-190 ~]# rm /etc/my.cnf
二、检查mysql是否存在
# 检查mysql是否存在
[root@NIWAY-190 ~]# rpm -qa | grep mysql
[root@NIWAY-190 ~]#
三、查看用户和组是否存在
1)检查mysql组合用户是否存在
# 检查mysql组和用户是否存在,如无则创建
[root@NIWAY-190 ~]# cat /etc/group | grep mysql
[root@NIWAY-190 ~]# cat /etc/passwd | grep mysql
# 查询全部用户(只是做记录,没必要执行)
[root@NIWAY-190 ~]# cat /etc/passwd|grep -v nologin|grep -v halt|grep -v shutdown|awk -F ":" '{print $1 "|" $3 "1" $4}' | more
root|010
sync|510
[root@NIWAY-190 ~]#
2)若不存在,则创建mysql组和用户
# 创建mysql用户组
[root@NIWAY-190 ~]# groupadd mysql
# 创建一个用户名为mysql的用户,并加入mysql用户组
[root@NIWAY-190 ~]# useradd -g mysql mysql
# 制定password 为111111
[root@NIWAY-190 ~]# passwd mysql
Changing password for user mysql.
New password:
BAD PASSWORD: The password is a palindrome
Retype new password:
passwd: all authentication tokens updated successfully.
四、下载mysql离线安装包tar文件
官网下载地址:https://dev.mysql.com/downloads/mysql/5.7.html#downloads
MySQL Community Server 5.7.40
https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.40-el7-x86_64.tar.gz
版本选择,可以选择一下两种方式:
1)使用Red Hat Enterprise Linux
Select Version:5.7.40
Select Operating System:Red Hat Enterprise Linux / Oracle Linux
Select OS Version:Red Hat Enterprise Linux 7 / Oracle Linux 7 (x86, 64-bit)
列表中下载:
Compressed TAR Archive 5.7.40 711.3M
Download
(mysql-5.7.40-el7-x86_64.tar.gz) MD5: 9a35e45c4fe418b72b43fcc464044ea2
2)使用Linux - Generic
Select Version:5.7.40
Select Operating System:Linux - Generic
Select OS Version:Linux - Generic (glibc 2.12) (x86, 64-bit)
列表中下载:
Compressed TAR Archive 5.7.40 646.6M
Download
(mysql-5.7.40-linux-glibc2.12-x86_64.tar.gz) MD5: ce0ef7b9712597f44f4ce9b9d7414a24
注意:上边两种方式找mysql离线安装包的方式都可以。本文选择的是第一种压缩包
五、上传第四步下载的mysql TAR包
# 进入/usr/local/文件夹
[root@NIWAY-190 ~]# cd /usr/local/
# 上传mysql TAR包到/usr/local/下面
[root@NIWAY-190 local]# ls
bin full-path-to-mysql-VERSION-OS include lib64 mysql-5.7.40-el7-x86_64.tar.gz share
etc games lib libexec sbin src
[root@NIWAY-190 local]# tar -zxvf mysql-5.7.40-el7-x86_64.tar.gz
mysql-5.7.40-el7-x86_64/bin/myisam_ftdump
mysql-5.7.40-el7-x86_64/bin/myisamchk
mysql-5.7.40-el7-x86_64/bin/myisamlog
mysql-5.7.40-el7-x86_64/bin/myisampack
mysql-5.7.40-el7-x86_64/bin/mysql
...
mysql-5.7.40-el7-x86_64/support-files/magic
mysql-5.7.40-el7-x86_64/support-files/mysql.server
[root@NIWAY-190 local]# ls
bin full-path-to-mysql-VERSION-OS include lib64 mysql-5.7.40-el7-x86_64 share
etc games lib libexec mysql-5.7.40-el7-x86_64.tar.gz sbin src
# 进入/usr/local下,修改为mysql
[root@NIWAY-190 local]# mv mysql-5.7.40-el7-x86_64 mysql
[root@NIWAY-190 local]# ls
bin etc full-path-to-mysql-VERSION-OS games include lib lib64 libexec mysql mysql-5.7.40-el7-x86_64.tar.gz sbin share src
六、更改所属的组和用户
# 更改所属的组和用户
[root@NIWAY-190 ~]# cd /usr/local/
[root@NIWAY-190 local]# chown -R mysql mysql/
[root@NIWAY-190 local]# chgrp -R mysql mysql/
[root@NIWAY-190 local]# cd mysql/
[root@NIWAY-190 mysql]# mkdir data
[root@NIWAY-190 mysql]# chown -R mysql:mysql data
七、在/etc下创建my.cnf文件
# 进入/usr/local/mysql文件夹下
[root@NIWAY-190 ~]# cd /usr/local/mysql
# 创建my.cnf文件
[root@NIWAY-190 mysql]# touch my.cnf #或者cd ''>my.cnf
# 编辑my.cnf
[root@NIWAY-190 mysql]# vi my.cnf
[mysql]
socket=/var/lib/mysql/mysql.sock
# set mysql client default chararter
default-character-set=utf8
[mysqld]
socket=/var/lib/mysql/mysql.sock
# set mysql server port
port = 3306 #默认是3306,这里发现3306已经被占用,因此防止这种情况发生,可以避免使用3306mysql默认端口
# set mysql install base dir
# 设置mysql的安装目录
basedir=/usr/local/mysql
# set the data store dir
#设置 mysql数据库的数据的存放目录
datadir=/usr/local/mysql/data
# set the number of allow max connnection
#允许最大连接数
max_connections=500
# set server charactre default encoding
character-set-server=utf8
# the storage engine
#创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M
explicit_defaults_for_timestamp=true
[mysql.server]
user=mysql
basedir=/usr/local/mysql
[root@NIWAY-190 mysql]#
[root@NIWAY-190 mysql]# chown -R mysql:mysql my.cnf
[root@NIWAY-190 mysql]# chmod 644 my.cnf
[root@NIWAY-190 mysql]# mkdir -p /var/lib/mysql
[root@NIWAY-190 mysql]# cd /var/lib/
[root@NIWAY-190 mysql]# chown -R mysql:mysql mysql
八、进入mysql文件夹,并安装mysql
# 进入mysql
[root@NIWAY-190 local]# cd /usr/local/mysql
# 安装mysql
[root@NIWAY-190 mysql]# bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
2023-01-19T08:10:35.069928Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2023-01-19T08:10:36.524379Z 0 [Warning] InnoDB: New log files created, LSN=45790
2023-01-19T08:10:36.739392Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2023-01-19T08:10:36.819895Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: c12b7f3d-97d0-11ed-be35-005056b7267e.
2023-01-19T08:10:36.823294Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2023-01-19T08:10:37.484210Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2023-01-19T08:10:37.484265Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2023-01-19T08:10:37.486536Z 0 [Warning] CA certificate ca.pem is self signed.
2023-01-19T08:10:37.720516Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
九、设置开机启动
#设置开机启动
#创建systemctl 的对mysql服务的配置文件
vim /usr/lib/systemd/system/mysql.service
#下面为mysql.service文件内容
[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
PIDFile=/usr/local/mysql/data/mysqld.pid
# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=0
# Execute pre and post scripts as root
PermissionsStartOnly=true
# Needed to create system tables
#ExecStartPre=/usr/bin/mysqld_pre_systemd
# Start main service
ExecStart=/usr/local/mysql/bin/mysqld --daemonize --pid-file=/usr/local/mysql/data/mysqld.pid
#注意这里要加上 --daemonize
# Use this to switch malloc implementation
#EnvironmentFile=-/etc/sysconfig/mysql
# Sets open_files_limit
LimitNOFILE = 5000
Restart=on-failure
RestartPreventExitStatus=1
PrivateTmp=false
#上面为mysql.service文件内容
#重新加载开机服务
[root@NIWAY-190 mysql]# systemctl daemon-reload
#配置开机启动
[root@NIWAY-190 mysql]# systemctl enable mysql
Created symlink from /etc/systemd/system/multi-user.target.wants/mysql.service to /usr/lib/systemd/system/mysql.service.
#启动mysql服务
[root@NIWAY-190 mysql]# systemctl start mysql
#查看是否启动成功
[root@NIWAY-190 mysql]# systemctl status mysql
十、修改配置文件
# 进入/etc/profile文件夹
[root@NIWAY-190 mysql]# vi /etc/profile
修改/etc/profile,在最后添加如下内容
# 修改/etc/profile文件
#设置mysql的全局环境,这样直接输入mysql就行set mysql environment
export PATH=$PATH:/usr/local/mysql/bin
# 使文件生效
[root@NIWAY-190 mysql]# source /etc/profile
十一、修改mysql登陆密码
1)修改密码
[root@NIWAY-190 mysql]# mysql -uroot -p
Enter password: #直接回车进入
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.40
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> set PASSWORD = PASSWORD('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
2)验证新密码是否登录成功:
[root@NIWAY-190 mysql]# mysql -uroot -p
Enter password: #此处输入新密码‘123456’
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.40 MySQL Community Server (GPL)
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show tables;
ERROR 1046 (3D000): No database selected
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql>
十二、添加远程访问权限
# 添加远程访问权限
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
#查看表情况
mysql> select host,user from user;
+-----------+---------------+
| host | user |
+-----------+---------------+
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+---------------+
3 rows in set (0.00 sec)
#更新成远程可以登陆
mysql> update user set host='%' where user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
#再次查看字段是否已经修改
mysql> select host,user from user;
+-----------+---------------+
| host | user |
+-----------+---------------+
| % | root |
| localhost | mysql.session |
| localhost | mysql.sys |
+-----------+---------------+
3 rows in set (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
十三、重启mysql生效
# 重启mysql
[root@NIWAY-190 mysql]# systemctl restart mysql