linux安装部署mysql服务 tar.gz rpm两种安装方式教程

一、介绍

本教程为linux下安装部署mysql服务,安装方式分为两种,一种tar.gz包安装,另一种为rpm包安装。

二、准备工作

服务器:linxu CentOS 7

mysql服务tar.gz版本:mysql-5.7.36-el7-x86_64.tar.gz

mysql服务rpm版本: mysql-5.7.36-1.el7.x86_64.rpm-bundler.tar

首先连接linux服务器,将防火墙关闭:


[root@128 ~]# systemctl stop firewalld
[root@128 ~]# systemctl disable firewalld

三、mysql服务tar.gz包安装

1.下载mysql-5.7.36-el7-x86_64.tar.gz安装包到/opt/目录下,并解压,重命名


[root@128 ~]# cd /opt
[root@128 opt]# wget https://cdn.mysql.com/archives/mysql-5.7/mysql-test-5.7.36-el7-x86_64.tar.gz
[root@128 opt]# tar -zxvf mysql-test-5.7.36-el7-x86_64.tar.gz
[root@128 opt]# mv mysql-test-5.7.36-el7-x86_64 mysql5.7

2.创建data、log目录,创建mysql.log、mysql.pid、mysql.sock文件,并创建mysql用户组并赋予mysql5.7目录权限


[root@128 opt]# cd mysql5.7
[root@128 mysql5.7]# mkdir data log
[root@128 mysql5.7]# cd log
[root@128 log]# touch mysql.log mysql.pid mysql.sock
[root@128 log]# cd ../../ 
[root@128 opt]# groupadd mysql
[root@128 opt]# useradd -r -g mysql mysql
[root@128 opt]# chown -R mysql:mysql mysql的根目录

目录结构如下:

3.修改/etc/my.cnf配置文件,没有的话可自行创建,配置如下


[client]
#客户端连接端口
port=3306
#客户端连接sock
socket=/opt/mysql5.7/log/mysql.sock
#客户端编码
default-character-set=utf8
 
[mysqld]
#mysql服务端口
port=3306
#安装目录
basedir=/opt/mysql5.7
#数据存放目录
datadir=/opt/mysql5.7/data
#sock文件地址
socket=/opt/mysql5.7/log/mysql.sock
#错误日志存放地址
log-error=/opt/mysql5.7/log/mysql.log
#pid文件地址
pid-file=/opt/mysql5.7/log/mysql.pid
#服务端编码
character-set-server=utf8
 
!includedir /etc/my.cnf.d

4.初始化数据库,初始化完成后,初始密码再/opt/mysql5.7/log/mysql.log中存放


[root@128 opt]# cd mysql5.7
[root@128 mysql5.7]# bin/mysqld --initialize --user=mysql --basedir=/opt/mysql5.7 --datadir=/opt/mysql5.7/data
[root@128 mysql5.7]# cat log/mysql.log

5.并启动mysql,并设置开机自启,修改初始化密码,赋予外部连接mysql权限


[root@128 mysql5.7]# cp support-files/mysql.server /etc/init.d/mysqld
#开机自启
[root@128 mysql5.7]# chkconfig --add mysqld   或者  systemctl enable mysqld
[root@128 mysql5.7]# systemctl start mysqld
[root@128 mysql5.7]# bin/mysql -uroot -p
Enter password:
mysql: [Warning] Using a password on the command line interface can be insecure.
......
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
mysql> set password=password("root");
mysql> grant all privileges on *.* to root@'%' identified by 'root' with grant option;
mysql> flush privileges;
mysql> exit;

6.使用navicat工具测试连接mysql。

四、mysql服务rpm包安装

1.首先检查是否安装过mysql


[root@bogon mysql]# rpm -qa|grep mysql
[root@bogon mysql]# rpm -qa|grep mariadb
mariadb-libs-5.5.44-2.el7.centos.x86_64

2.如有安装mariadb,则卸载


[root@bogon mysql]# rpm -e --nodeps mariadb-libs-5.5.44-2.el7.centos.x86_64

3.下载mysql-5.7.36-1.el7.x86_64.rpm-bundler.tar安装包到/opt/目录下,创建mysql5.7目录,并解压


[root@bogon ~]# cd /opt
[root@bogon opt]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.36-1.el7.x86_64.rpm-bundle.tar
[root@bogon opt]# mkdir mysql5.7
[root@bogon opt]# tar -xvf mysql-5.7.36-1.el7.x86_64.rpm-bundle.tar -C /opt/mysql5.7/
[root@bogon opt]# cd mysql5.7
[root@bogon mysql5.7]# ll
总用量 533076
-rw-r--r--. 1 7155 31415  26664808 9月   8 2021 mysql-community-client-5.7.36-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415    317808 9月   8 2021 mysql-community-common-5.7.36-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415   4118740 9月   8 2021 mysql-community-devel-5.7.36-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415  47760636 9月   8 2021 mysql-community-embedded-5.7.36-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415  23316608 9月   8 2021 mysql-community-embedded-compat-5.7.36-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 132191192 9月   8 2021 mysql-community-embedded-devel-5.7.36-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415   2473272 9月   8 2021 mysql-community-libs-5.7.36-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415   1263988 9月   8 2021 mysql-community-libs-compat-5.7.36-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 182267476 9月   8 2021 mysql-community-server-5.7.36-1.el7.x86_64.rpm
-rw-r--r--. 1 7155 31415 125479900 9月   8 2021 mysql-community-test-5.7.36-1.el7.x86_64.rpm

4.安装按照common、libs、client、server顺序来,如出现下面错误,则安装perl


[root@bogon mysql5.7]# rpm -ivh mysql-community-common-5.7.36-1.el7.x86_64.rpm mysql-community-libs-5.7.36-1.el7.x86_64.rpm mysql-community-client-5.7.36-1.el7.x86_64.rpm mysql-community-server-5.7.36-1.el7.x86_64.rpm 
警告:mysql-community-common-5.7.36-1.el7.x86_64.rpm: 头V3 DSA/SHA256 Signature, 密钥 ID 5072e1f5: NOKEY
错误:依赖检测失败:
    /usr/bin/perl 被 mysql-community-server-5.7.36-1.el7.x86_64 需要
    perl(Getopt::Long) 被 mysql-community-server-5.7.36-1.el7.x86_64 需要
    perl(strict) 被 mysql-community-server-5.7.36-1.el7.x86_64 需要
[root@bogon mysql5.7]# yum -y install perl
[root@bogon mysql5.7]# rpm -ivh mysql-community-common-5.7.36-1.el7.x86_64.rpm mysql-community-libs-5.7.36-1.el7.x86_64.rpm mysql-community-client-5.7.36-1.el7.x86_64.rpm mysql-community-server-5.7.36-1.el7.x86_64.rpm
警告:mysql-community-common-5.7.36-1.el7.x86_64.rpm: 头V3 DSA/SHA256 Signature, 密钥 ID 5072e1f5: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:mysql-community-common-5.7.36-1.e################################# [ 25%]
   2:mysql-community-libs-5.7.36-1.el7################################# [ 50%]
   3:mysql-community-client-5.7.36-1.e################################# [ 75%]
   4:mysql-community-server-5.7.36-1.e################################# [100%]

5.启动mysql,查询初始化密码为:qaw.d0N_%(je


[root@bogon mysql5.7]# systemctl start mysqld
[root@bogon mysql5.7]# grep password /var/log/mysqld.log 
2023-03-01T03:22:32.919615Z 1 [Note] A temporary password is generated for root@localhost: qaw.d0N_%(je

6.修改初始化密码,设置密码出现ERROR 1819 (HY000): Your password does not satisfy the current policy requirements提示,说明密码过于简单,可先设置复杂密码,然后查看密码规则


[root@bogon mysql5.7]# mysql -uroot -p
Enter password: qaw.d0N_%(je
mysql> set password = password("root");
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql> set password=password('Abc123!@#');
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> show variables like 'validate_password%';
+--------------------------------------+--------+
| Variable_name                        | Value  |
+--------------------------------------+--------+
| validate_password_check_user_name    | OFF    |
| validate_password_dictionary_file    |        |
| validate_password_length             | 8      |
| validate_password_mixed_case_count   | 1      |
| validate_password_number_count       | 1      |
| validate_password_policy             | MEDIUM |
| validate_password_special_char_count | 1      |
+--------------------------------------+--------+
7 rows in set (0.01 sec)

7.修改密码策略,重新设置密码,并开启允许远程连接数据库


#修改密码策略等级为低
mysql> set global validate_password_policy=LOW;
Query OK, 0 rows affected (0.00 sec)
#修改密码长度至少为4
mysql> set global validate_password_length=4;
Query OK, 0 rows affected (0.00 sec)
#重新设置密码
mysql> set password=password('root');
Query OK, 0 rows affected, 1 warning (0.00 sec)
#开启远程连接数据库
mysql> grant all privileges on *.* to root@'%' identified by 'root' with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

8.再其他电脑使用连接工具测试,连接成功

9.设置编码字符集,修改/etc/my.cnf文件设置字符集


[root@bogon mysql5.7]# vi /etc/my.cnf
#添加如下编码配置
[mysqld]
character-set-server=utf8
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8

10.重启mysql,并登录mysql查看字符集


[root@bogon mysql5.7]# systemctl restart mysqld
[root@bogon mysql5.7]# mysql -uroot -p
Enter password: root
mysql> show variables like '%character%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

五.修改mysql数据目录(有问题未解决)


#先关闭mysql
[root@bogon mysql5.7] systemctl stop mysqld
#创建新目录
[root@bogon mysql5.7] mkdir data
#将mysql数据目录整体复制到data目录
[root@bogon mysql5.7] cp -R /var/lib/mysql/* /opt/mysql5.7/data/
#备份原始数据文件
[root@bogon mysql5.7] mv /var/lib/mysql /var/lib/mysql_bak
#设置数据权限
[root@bogon mysql5.7] chown -R mysql:mysql data
[root@bogon mysql5.7] chmod -R 777 data

然后修改/etc/my.cnf文件修改目录


datadir=/opt/mysql5.7/data
socket=/opt/mysql5.7/data/mysql.sock

修改完成,启动mysql启动不起来,错误信息如下,有知道的欢迎留言

/var/log/mysql.log日志如下:


2023-03-03T08:21:46.772613Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
2023-03-03T08:21:46.777358Z 0 [Note] InnoDB: Completed initialization of buffer pool
2023-03-03T08:21:46.778421Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
2023-03-03T08:21:46.789316Z 0 [ERROR] InnoDB: The innodb_system data file 'ibdata1' must be writable
2023-03-03T08:21:46.789339Z 0 [ERROR] InnoDB: The innodb_system data file 'ibdata1' must be writable
2023-03-03T08:21:46.789344Z 0 [ERROR] InnoDB: Plugin initialization aborted with error Generic error
2023-03-03T08:21:47.393465Z 0 [ERROR] Plugin 'InnoDB' init function returned error.
2023-03-03T08:21:47.393543Z 0 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
2023-03-03T08:21:47.393557Z 0 [ERROR] Failed to initialize builtin plugins.
2023-03-03T08:21:47.393564Z 0 [ERROR] Aborting

2023-03-03T08:21:47.393650Z 0 [Note] Binlog end
2023-03-03T08:21:47.393816Z 0 [Note] Shutting down plugin 'CSV'
2023-03-03T08:21:47.394635Z 0 [Note] /usr/sbin/mysqld: Shutdown complete
  • 5
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天道酬勤-L

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值