centos7.9离线安装mysql5.7.40(本文使用mysql_install_db安装方法)
一、卸载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
[root@NIWAY-190 mysql]#vi /etc/security/limits.conf
在文件最后面增加两行( PS:大于50000即可)
* hard nofile 65535
* soft nofile 65535
保存退出,重启服务器
这样就不会报以下两条错误
2023-01-09T04:49:35.679438Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5000)
2023-01-09T04:49:35.679461Z 0 [Warning] Changed limits: table_open_cache: 431 (requested 2000)
# 进入mysql
[root@NIWAY-190 local]# cd /usr/local/mysql
# 安装mysql
[root@NIWAY-190 mysql]# bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
2023-01-09 12:49:35 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2023-01-09 12:49:42 [WARNING] The bootstrap log isn't empty:
2023-01-09 12:49:42 [WARNING] 2023-01-09T04:49:35.677733Z 0 [Warning] --bootstrap is deprecated. Please consider using --initialize instead
设置文件及目录权限:
[root@NIWAY-190 mysql]# cp ./support-files/mysql.server /etc/init.d/mysqld
[root@NIWAY-190 mysql]#
[root@NIWAY-190 mysql]# chmod +x /etc/init.d/mysqld
[root@NIWAY-190 mysql]#
九、启动mysql
#记得关闭防火墙,或者打开3306端口
# 查看进程是否有mysql已经启动
[root@NIWAY-190 mysql]# ps -ef|grep mysql
#已经启动的话,使用/etc/init.d/mysqld restart命令
#如果没启动的话,使用/etc/init.d/mysqld start命令
#关闭命令,使用/etc/init.d/mysqld stop命令
[root@localhost data]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/usr/local/mysql/data/localhost.localdomain.err'.
. SUCCESS!
#表示启动MYSQL,并把日志写入localhost.localdomain.err文件内
[root@NIWAY-190 mysql]# /etc/init.d/mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
十、设置开机启动
#设置开机启动
[root@NIWAY-190 mysql]# chkconfig --level 35 mysqld on
[root@NIWAY-190 mysql]# chkconfig --list mysqld
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@NIWAY-190 mysql]# chmod +x /etc/rc.d/init.d/mysqld
[root@NIWAY-190 mysql]# chkconfig --add mysqld
[root@NIWAY-190 mysql]# chkconfig --list mysqld
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@NIWAY-190 mysql]# service mysqld status
SUCCESS! MySQL running (1899)
[root@NIWAY-190 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
#做个软连接
ln -s /var/lib/mysql/mysql.sock /tmp
十二、获得mysql初始密码
1)获得mysql初始密码
[root@NIWAY-190 mysql]# cat /root/.mysql_secret
# Password set for user 'root@localhost' at 2023-01-09 12:49:35
o/XuKtQeAyDs
[root@NIWAY-190 mysql]#
GCdb3+4_#96a
2)修改密码
[root@NIWAY-190 mysql]# mysql -uroot -p
Enter password: #此处填写上边获取到的初始密码‘o/XuKtQeAyDs’
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
3)验证新密码是否登录成功:
[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]# /etc/init.d/mysqld restart
Shutting down MySQL..[ OK ]
Starting MySQL..[ OK ]
[root@NIWAY-190 mysql]#