欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

CentOS 7 离线安装MySQL 详细教程

程序员文章站 2022-07-08 10:26:04
前言:MySQL下载地址:https://dev.mysql.com//Downloads/MySQL-5.7/mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz正文:一、安装步骤:1.1、卸载旧的MySQL1.1.1、删除MySQL的安装文件查询MySQL的安装文件:[root@www ~]# find / -name mysql/usr/local/env/mysql/usr/local/env/mysql/mysql/usr/local/env...
前言:

MySQL下载地址:

https://dev.mysql.com//Downloads/MySQL-5.7/mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz

正文:

一、安装步骤:

1.1、卸载旧的MySQL

1.1.1、删除MySQL的安装文件

查询MySQL的安装文件:

[root@www ~]#  find / -name mysql
/usr/local/env/mysql
/usr/local/env/mysql/mysql
/usr/local/env/mysql/mysql/bin/mysql
/usr/local/env/mysql/mysql/include/mysql
/usr/local/env/mysql/mysql/data/mysql
[root@www ~]# 

删除:

[root@www ~]# rm -rf /usr/local/env/mysql
1.1.2、删除MySQL的配置文件

删除/etc/my.cnf文件:

[root@www ~]# rm -rf /etc/my.cnf

删除/etc/init.d/下跟MySQL有关的全部文件,通常包括mysql文件或者mysqld文件。/etc/init.d/目录下存放的一般都是加入系统的各个服务。

[root@www ~]# rm -rf /etc/init.d/mysql
[root@www ~]# rm -rf /etc/init.d/mysqld
1.1.3、删除MySQL用户和用户组
[root@www ~]# userdel mysql
userdel: user 'mysql' does not exist

至此,卸载完成!

1.2、安装MySQL

1.2.1、下载安装mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz

安装包下载地址:
https://dev.mysql.com/downloads/mysql/5.7.html#downloads

将下载好的mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz上传到Linux服务器并解压,笔者放到/usr/local/env/mysql目录下。

[root@www ~]# cd /usr/local/env/mysql/
[root@www mysql]# tar -zxvf mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz

重命名为mysql

[root@www mysql]# mv mysql-5.7.18-linux-glibc2.5-x86_64 mysql
1.2.2、添加用户组mysql和用户mysql,并将其添加到mysql用户组中
[root@www mysql]# groupadd mysql
[root@www mysql]# useradd -r -g mysql mysql

注:

  • useradd -r参数表示mysql用户是系统用户,不可用于登录系统。
  • useradd -g参数表示把mysql用户添加到mysql用户组中。

查看用户以及用户组信息:

cat /etc/passwd		# 查看所有用户信息
cat /etc/group		# 查看所有组信息
groups 用户名		# 查看当前用户所在组,以及组内其它成员的信息
1.2.3、检查是否安装了 libaio
[root@www ~]# rpm -qa | grep libaio

如果没有安装,使用如下命令安装:

[root@www ~]# yum search libaio
[root@www ~]# yum install libaio
1.2.4、配置my.cnf文件
[root@www ~]# touch /etc/my.cnf

注意: 使用默认的644权限和用户,不要做修改。

将如下内容拷贝到里面:

[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
# default-character-set=utf8mb4

socket=/var/lib/mysql/mysql.sock


[mysqld]
# 禁用DNS解析,一般不用
#skip-name-resolve

# 设置3306端口
port=3306

# 这里的socket要和[mysql]中的socket位置一致
socket=/var/lib/mysql/mysql.sock

# 设置mysql的安装目录
basedir=/usr/local/env/mysql/mysql

# 设置mysql数据库的数据的存放目录
datadir=/usr/local/env/mysql/mysql/data

# 允许最大连接数
max_connections=200

# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
collation-server=utf8_bin
# 如果使用utf8mb4字符集就做如下两个配置
# character-set-server=utf8mb4
# collation-server=utf8mb4_unicode_ci

# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB

# 数据库中的表名忽略大小写
# lower_case_table_name=1

max_allowed_packet=16M

# 慢查询设置
# 是指执行超过多久的sql会被log下来,这里是2秒
# long_query_time =2
# 将查询返回较慢的语句进行记录
# log-slow-queries=/usr/local/env/mysql/mysql/logs/slow_query.log
# 就是字面意思,log下来没有使用索引的query
# log-queries-not-using-indexes=/usr/local/env/mysql/mysql/logs/no_use_index.log
# 对所有执行语句进行记录
# log=/usr/local/env/mysql/mysql/logs/mylog.log

[mysqld_safe]
log-error=/usr/local/env/mysql/mysql/logs/error.log
pid-file=/usr/local/env/mysql/mysql/data/mysqld.pid
1.2.5、创建mysql、data、logs文件夹
[root@www ~]# mkdir /var/lib/mysql
[root@www ~]# mkdir /usr/local/env/mysql/mysql/logs
[root@www ~]# mkdir /usr/local/env/mysql/mysql/data

在logs目录下创建error.log文件,否则有可能在初始化MySQL的时候会报错。
如果没有mysqld.pid文件,在data目录下建一个mysqld.pid文件,方便起见,可将其权限设置为777:

[root@www ~]# cd /usr/local/env/mysql/mysql/
[root@www ~]# touch logs/error.log
[root@www ~]# touch data/mysqld.pid
[root@www ~]# chmod 777 mysqld.pid
1.2.6、将mysql目录的所属用户和组改为mysql
[root@www mysql]# chown -R mysql.mysql /var/lib/mysql
[root@www mysql]# chown -R mysql:mysql /usr/local/env/mysql/mysql

注:

  • -R 表示对目前目录下的所有档案与子目录进行相同的权限变更(即以递回的方式逐个变更)
  • mysql.mysql 和 mysql:mysql 这两种写法是一样的,都是将后面指定的目录修改为mysql用户和组
1.2.7、初始化 mysqld 生成初始化密码
[root@www ~]# cd /usr/local/env/mysql/mysql
[root@www mysql]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/env/mysql/mysql --datadir=/usr/local/env/mysql/mysql/data

此时,控制台打印如下:

2018-10-24T04:25:13.673365Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-10-24T04:25:15.808961Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-10-24T04:25:16.105505Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-10-24T04:25:16.184776Z 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: cec94f21-d744-11e8-a0b5-fa163ed8e403.
2018-10-24T04:25:16.188372Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-10-24T04:25:16.189074Z 1 [Note] A temporary password is generated for root@localhost: i;lknXwO;5,s
[root@www mysql]#

最后:i;lknXwO;5,s 就是初始化之后的密码。

二、MySQL的配置:

2.1、设置开机启动

2.1.1、复制mysql.server脚本到资源目录,并赋予执行权限:
[root@www mysql]# cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@www mysql]# chmod +x /etc/rc.d/init.d/mysqld
2.1.2、将 mysqld 服务加入到系统服务并检测是否生效:
[root@www mysql]# chkconfig --add mysqld
[root@www 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

得到如下结果说明生效。

注:

  • 查看启动项:chkconfig --list | grep -i mysql
  • 删除启动项:chkconfig --del mysql

启动mysqld:

[root@www mysql]# service mysqld start
Starting MySQL.Logging to '/usr/local/env/mysql/mysql/data/baidu64.err'.
                                                           [  OK  ]

通过命令查看MySQL进程,会看到MySQL已经开启了守护进程:

[root@www mysql]# ps -ef|grep mysql
root		10569		1	0	Jul15	?		00:00:00	/bin/sh /usr/local/env/mysql/mysql/bin/mysqld_safe --datadir=/usr/local/env/mysql/mysql/data --pid-file=/usr/local/env/mysql/mysql/data/mysqld.pid
mysql		10974	10569	4	Jul15	?		00:00:00	/usr/local/env/mysql/mysql/bin/mysqld --basedir=/usr/local/env/mysql/mysql --datadir=/usr/local/env/mysql/mysql/data --plugin-dir=/usr/local/env/mysql/mysql/lib/plugin --user=mysql --log-error=/usr/local/env/mysql/mysql/logs/error.log --pid-file=/usr/local/env/mysql/mysql/data/mysqld.pid --socket=/var/lib/mysql/mysql.sock --port=3306
root		21543	10404	0	11:56	pts/0	00:00:00	grep --color=auto mysql

其中,进程号为10569的就是守护进程,有了守护进程,即便mysql的进程被杀掉了,守护进程也会立刻将mysql进程再次开启。

若要关闭MySQL,执行如下命令:

[root@www mysql]# service mysql stop

2.2、配置环境变量

2.2.1、打开/etc/profile配置文件,添加如下内容:
#mysql环境变量
PATH=$PATH:/usr/local/env/mysql/mysql/bin
export PATH
或:
export PATH=$PATH:/usr/local/env/mysql/mysql/bin
2.2.2、执行命令,使其生效:
[root@www mysql]# source /etc/profile
2.2.3、校验是否成功:
[root@www mysql]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/home/env/jdk/jdk8/jdk1.8.0_171/bin:/root/bin:/home/env/jdk/jdk8/jdk1.8.0_171/bin:/usr/local/env/mysql/mysql/bin

出现了 /usr/local/env/mysql/mysql/bin 表示以已经配置成功。

2.3、初次登录 修改访问密码

2.3.1、首次登陆

第一次登陆的时候,使用初始化的密码:i;lknXwO;5,s

[root@www mysql]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.18
 
Copyright (c) 2000, 2017, 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>
2.3.1、修改登陆密码
mysql> SET PASSWORD = PASSWORD('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)

或者:

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> update user set authentication_string=PASSWORD('123456') where User='root';
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 1
 
mysql>

2.4、允许远程访问

mysql> grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)
 
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
 
mysql>
注意:

# 允许所有用户远程访问 修改用户名和密码为你自己的
mysql> grant all privileges on *.* to 'root'@'%' identified by 'password' with grant option;

# 允许单个ip访问 修改用户名和密码为你自己的
mysql> grant all privileges on *.* to 'root'@'1.2.3.4' identified by 'password' with grant option;

# 最后
mysql> FLUSH PRIVILEGES;

此时可以查看user表中多了一个 host 为 % 的用户:

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 user, host from user;
+---------------+-----------+
| user          | host      |
+---------------+-----------+
| root          | %         |
| mysql.session | localhost |
| mysql.sys     | localhost |
| root          | localhost |
+---------------+-----------+
4 rows in set (0.00 sec)

mysql>

此时,可以使用navicat进行远程连接,或者在另外一台Linux服务器上用命令远程连接,假设MySQL所在服务器地址为 192.168.137.10:

[root@www ~]# mysql -h 192.168.137.10 -u root -p
Enter password:

输入密码即可。

2.5、使用navicate远程连接报错1045

如果使用navicate进行远程连接访问,报如下错误:

1045 - Access denied for user 'root'@'192.168.137.10' (using password: YES)

解决方法如下:

2.5.1、关闭mysql服务
[root@www mysql]# service mysql stop
Redirecting to /bin/systemctl stop mysql.service
2.5.2、修改/etc/my.cnf修改为无密码登录

在my.cnf配置文件添加如下内容:

# mysql无密码登录
skip-grant-tables
2.5.3、重启mysql服务
[root@www mysql]# service mysql restart
Redirecting to /bin/systemctl restart mysql.service
2.5.4、无密码登录mysql

注意:此时登录不带p参数:

[root@www mysql]# mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.18 MySQL Community Server (GPL)
 
Copyright (c) 2000, 2017, 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>
2.5.5、再次修改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> update mysql.user set authentication_string=password('123456') where user='root' ;
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 2  Changed: 1  Warnings: 1
 
mysql>
2.5.6、然后将my.cnf无密码登录配置去掉(就是上面刚加的那句话)
2.5.7、退出mysql,并重启
mysql> quit
Bye
[root@www mysql]# service mysql restart
Redirecting to /bin/systemctl restart mysql.service
[root@www mysql]#

再次使用navicat连接即可。

三、开启binlog日志

3.1、mysql设置binlog日志

修改 /etc/my.cnf 配置文件,在 [mysqld] 节点下添加如下配置:

[mysqld]

# 开启binlog日志
# log_bin=ON
# binlog日志的基本文件名,后面会追加标识来表示每一个文件
# log_bin_basename=/usr/local/env/mysql/mysql/logs/mysql-bin
# binlog文件的索引文件,这个文件管理了所有的binlog文件的目录
# log_bin_index=/usr/local/env/mysql/mysql/mysql.index

# 设置日志路径,注意路径需要mysql用户有权限写。这一个参数和上面的三个参数是相同的,mysql会根据这个配置自动 设置 log_bin 为 ON 状态,自动设置 log_bin_index 文件名为你指定的文件名后跟 .index
log-bin=/usr/local/env/mysql/mysql/logs/mysql-binlog.log
# 指定当前mysql服务器的服务ID,为了区别集群内的其他mysql服务器。该参数5.7版本以下不需要设置,5.7以上的版本都要进行设置服务ID
server-id=002
# 设置日志格式,有三种:statement、row、mixed
binlog_format=mixed
# 设置binlog清理时间
expire_logs_days=7
# binlog每个日志文件的大小
max_binlog_size=100m
# binlog缓存大小
binlog_cache_size=4m
# 最大binlog缓存大小
max_binlog_cache_size=512m
# 无论是增量备份还是主从复制,都是需要开启mysql-binlog日志,最好跟数据目录设置到不同的>磁盘分区,可以降低io等待,提升性能;并且在磁盘故障的时候可以利用mysql-binlog恢复数据。

配置好之后,启动mysql,登陆。查看配置是否起作用:

show variables like '%log_bin%'; 

显示如下信息(log_bin的值为on),即表示设置成功。

+---------------------------------+----------------------------------------------------+
| Variable_name                   | Value                                              |
+---------------------------------+----------------------------------------------------+
| log_bin                         | ON                                                 |
| log_bin_basename                | /usr/local/env/mysql/mysql/logs/mysql-binlog       |
| log_bin_index                   | /usr/local/env/mysql/mysql/logs/mysql-binlog.index |
| log_bin_trust_function_creators | OFF                                                |
| log_bin_use_v1_row_events       | OFF                                                |
| sql_log_bin                     | ON                                                 |
+---------------------------------+----------------------------------------------------+
6 rows in set (0.00 sec)

十步杀一人,千里不留行
事了拂衣去,深藏身与名

–end–

本文地址:https://blog.csdn.net/weixin_44975741/article/details/107402518