关于mysql:离线安装MySQL

2次阅读

共计 8418 个字符,预计需要花费 22 分钟才能阅读完成。

服务器环境:

CentOS-7.4-x86_64-DVD-1708

MySQL 版本:

mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz

1,首先卸载 Centos7 自带的 Mariadb

# 查看零碎自带的 Mariadb
[root@CDH-141 ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.44-2.el7.centos.x86_64
# 卸载零碎自带的 Mariadb(此时要依据理论状况去卸载)
[root@CDH-141 ~]# rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64

2,查看 Msyql 是否存在

# 查看 mysql 是否存在
[root@CDH-141 ~]# rpm -qa | grep mysql
[root@CDH-141 ~]# 

3,查看用户和组是否存在

(1), 先查看是否存在,如果不存在则创立

# 查看 mysql 组和用户是否存在,如无则创立
[root@CDH-141 ~]# cat /etc/group | grep mysql
[root@CDH-141 ~]# cat /etc/passwd | grep mysql 
# 创立 mysql 用户组
[root@CDH-141 ~]# groupadd mysql
# 创立一个用户名为 mysql 的用户,并退出 mysql 用户组
[root@CDH-141 ~]# useradd -g mysql mysql
# 制订 password 为 111111
[root@CDH-141 ~]# 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.

4,下载 mysql 离线安装包 tar 文件

官网下载地址:https://dev.mysql.com/downloa…

版本抉择,能够抉择一下两种形式:

1)应用 Red Hat Enterprise Linux
Select Version:5.7.25
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:(mysql-5.7.25-el7-x86_64.tar.gz)

2)应用 Linux – Generic
Select Version:5.7.25
Select Operating System:Linux – Generic
Select OS Version:Linux – Generic (glibc 2.12) (x86, 64-bit)
列表中下载:
Compressed TAR Archive:(mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz)【本文中应用的是这个版本】
留神:上边两种形式找 mysql 离线安装包的形式都能够。

5,上传第四步下载的 mysql TAR 包

# 上传 mysql TAR 包
# 或者间接应用 XFTP 进行装置
[root@CDH-141 ~]# rz
# 解压 mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz

[root@CDH-141 local]# ls
bin  full-path-to-mysql-VERSION-OS  include  lib64    mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz  share
etc  games                          lib      libexec  sbin                                 src
[root@CDH-141 local]#  tar -zxvf mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
mysql-5.7.25-lin
...
mysql-5.7.25-linux-glibc2.12-x86_64/share/install_rewriter.sql
mysql-5.7.25-linux-glibc2.12-x86_64/share/uninstall_rewriter.sql
mysql-5.7.25-linux-glibc2.12-x86_64/support-files/magic
mysql-5.7.25-linux-glibc2.12-x86_64/support-files/mysql.server
mysql-5.7.25-linux-glibc2.12-x86_64/docs/INFO_BIN
mysql-5.7.25-linux-glibc2.12-x86_64/docs/INFO_SRC
[root@localhost ~]# cd /usr/local/
[root@CDH-141 local]# ls
bin  full-path-to-mysql-VERSION-OS  include  lib64    mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz  share
etc  games                          lib      libexec  mysql-5.7.25-linux-glibc2.12-x86_64  sbin                                        src
# 进入 /usr/local 下,批改为 mysql
[root@CDH-141 local]# mv mysql-5.7.27-linux-glibc2.12-x86_64/ mysql
[root@CDH-141 local]# ls
bin  etc  full-path-to-mysql-VERSION-OS  games  include  lib  lib64  libexec  mysql  mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz  sbin  share  src

6,更改所属的组和用户

# 更改所属的组和用户
[root@CDH-141 ~]# cd /usr/local/
[root@CDH-141 local]# chown -R mysql mysql/
[root@CDH-141 local]# chgrp -R mysql mysql/
[root@CDH-141 local]# cd mysql/
[root@CDH-141 mysql]# mkdir data
[root@CDH-141 mysql]# chown -R mysql:mysql data

7,在 /etc 下创立 my.cnf 文件

# 编辑 /etc/my.cnf
[root@CDH-141 mysql]# vi /etc/my.cnf
[mysql]
socket=/tmp/mysql.sock
# set mysql client default chararter
default-character-set=utf8

[mysqld]
socket=/tmp/mysql.sock
# set mysql server port  
port = 3306 # 默认是 3306,如果这里发现 3306 曾经被占用,能够更改
# set mysql install base dir
basedir=/usr/local/mysql
# set the data store dir
datadir=/usr/local/mysql/data
# set the number of allow max connnection
max_connections=1024
# set server charactre default encoding
character-set-server=utf8
# the storage engine
default-storage-engine=INNODB
# 设置 MySQL 对表名等不辨别大小写
lower_case_table_names=1
max_allowed_packet=200M
explicit_defaults_for_timestamp=true
#阻止过多尝试失败的客户端以避免暴力破解明码的状况, 与性能并无太大的关系
max_connect_errors=30
#此参数确定数据日志文件的大小,以 M 为单位,依据数据更新频率调整。innodb_log_file_size=50
#指定大小的内存来缓冲数据和索引, 最大能够把该值设置成物理内存的 80%
innodb_buffer_pool_size=10G
key_buffer_size=16M
sql_mode=STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
# 配置 GROUP_CONCAT 拼接的字符串的长度字节
group_concat_max_len = 102400


[mysql.server]
user=mysql
basedir=/usr/local/mysql

# 能够防止出现插入中文报错;如果此时不失效,能够强制在创立表的时候指定应用 utf8 的编码集 
[client]
default-character-set = utf8

8, 进入 mysql 文件夹,并装置 mysql

# 进入 mysql
[root@CDH-141 local]# cd /usr/local/mysql
# 装置 mysql
[root@CDH-141 mysql]# bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
2019-03-08 18:11:07 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2019-03-08 18:11:24 [WARNING] The bootstrap log isn't empty:
2019-03-08 18:11:24 [WARNING] 2019-03-08T10:11:07.208602Z 0 [Warning] --bootstrap is deprecated. Please consider using --initialize instead

# 设置文件以及目录的权限
[root@CDH-141 mysql]# cp ./support-files/mysql.server /etc/init.d/mysqld

9,启动 MySQL

# 启动 mysql
[root@CDH-141 mysql]# /etc/init.d/mysqld restart
 ERROR! MySQL server PID file could not be found!
Starting MySQL.Logging to '/usr/local/mysql/data/localhost.localdomain.err'.
 SUCCESS! 

10,设置开机自启动

# 设置开机启动
[root@CDH-141 mysql]# chkconfig --level 35 mysqld on
[root@CDH-141 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@CDH-141 mysql]# chkconfig --add mysqld
[root@CDH-141 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@CDH-141 mysql]# service mysqld status
MySQL running (26122)[OK]
[root@CDH-141 mysql]#

11,批改环境变量配置文件

# 进入 /etc/profile 文件夹
[root@CDH-141 mysql]# vi /etc/profile
批改 /etc/profile,在最初增加如下内容
# 批改 /etc/profile 文件
export MYSQL_HOME=/usr/local/mysql
export PATH=$MYSQL_HOME/bin:$PATH
# 使文件失效
[root@CDH-141 mysql]# source /etc/profile

12,取得 MySQL 初始密码

# 每个人的明码是不一样的,随机生成的
[root@CDH-141 mysql]#  cat /root/.mysql_secret  
# Password set for user 'root@localhost' at 2019-03-08 17:40:42
poc3u0mO_luv
[root@CDH-141 mysql]# 

13,批改明码

[root@CDH-141 mysql]# mysql -uroot -p
Enter password: #此处填写上边获取到的初始密码‘poc3u0mO_luv’Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.25

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('111111@123');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

# 验证登录
[root@localhost mysql]# mysql -uroot -p111111@123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.27 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> 

14,增加近程拜访权限

留神:以后的拜访权限是间接给 root 用户增加的,如果在我的项目现场部署的时候,是不能应用 root 用户的;

此时是有两种计划的,别离做了整顿:

## 计划 1:间接给 root 用户减少近程拜访权限
# 增加近程拜访权限
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 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>
--- 计划 2:追加一个 demo 用户,而后给 demo 用户增加近程拜访的权限
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 host='%' where user='demo';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select host,user from user;
+-----------+---------------+
| host      | user          |
+-----------+---------------+
| %         | demo          |
| localhost | mysql.session |
| localhost | mysql.sys     |
+-----------+---------------+
3 rows in set (0.00 sec)

15,重启 MySQL 使其失效

做上述操作之后,是须要重新启动 MySQL,能力使其失效

# 重启 mysql
[root@CDH-141 mysql]# /etc/init.d/mysqld restart
Shutting down MySQL..[OK]
Starting MySQL..[OK]
[root@CDH-141 mysql]# 

## 重启命令 2
systemctl  start  mysql

16,如果连贯失败,须要查看防火墙

# 第一步:敞开防火墙
systemctl stop firewalld
#################
#############
####### 留神,在理论中先敞开,确定是应用开发端口的形式实现,还是间接移除,再执行对应的操作
##### 第二步:此时有两种解决方案
# 计划 1:移除防火墙
systemctl disable firewalld

# 计划 2:开发 3306 端口
#  凋谢端口请依据理论拜访规定配置,这里只是单纯开启 3306 端口的拜访,默认全放行,在生产环境禁止凋谢所有 IP 拜访
# (1) 向防火墙增加 mysql 端口: 
firewall-cmd --zone=public --add-port=3306/tcp --permanent
# (2) 刷新防火墙规定: 
firewall-cmd --reload
# (3) 验证端口, 查问防火墙凋谢端口: 
firewall-cmd --zone=public --list-port
正文完
 0