安装MySQL

1.下载mysql安装包

https://downloads.mysql.com/archives/community/

解压压缩包 tar xf mysql-8.0.30-linux-glibc2.17-x86_64-minimal.tar

再次解压需要的压缩包tar xf mysql-8.0.30-linux-glibc2.17-x86_64-minimal.tar.xz

将解压的压缩包移动到/usr/local目录下

2.创建数据存放目录

cd /usr/local/mysql

mkdir -p date

3.创建用户组和用户

groupadd mysql

useradd g mysql mysql

4.修改目录权限

chown -R mysql.mysql /usr/local/mysql

5.初始化mysql

cd /usr/local/mysql

./bin/mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --initialize

命令执行后会输出密码。将密码复制下后

6.修改配置文件

vim /etc/my.cnf

[client]
port = 3306
socket = /usr/local/mysql/data/mysql.sock
default-character-set = utf8mb4 [mysql]
default-character-set = utf8mb4 [mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci
init_connect = 'SET NAMES utf8mb4'
max_connections = 300 port = 3306
socket = /usr/local/mysql/data/mysql.sock
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1000M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
datadir = /usr/local/mysql/data
#lower_case_table_names=1
#如果要设置lower_case_table_names可以在初始化里面设置 ./mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql --lower_case_table_names=1 [mysqldump]
quick
max_allowed_packet = 16M [mysql]
no-auto-rehash [myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M [mysqlhotcopy]
interactive-timeout

7.设置mysql开机自启动

vim /etc/systemd/system/mysql.service

[Unit]
Description=MySQL Server
After=network.target [Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE=5000
Restart=always
PIDFile=/usr/local/mysql/data/mysqld.pid [Install]
WantedBy=multi-user.target

systemctl daemon-reload

systemctl enable --now mysql

8.创建软连接用于连接数据库

ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql

在ubuntu中登录数据库是为报错libncurses.so.5确实需要安装

apt install -y libncurses.so5

9.修改mysql登录密码

mysql -u root -p

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '自己的密码';

10 设置mysql远程登录

use mysql;
update user set host='%' where user='root' limit 1;
flush privileges;

安装redis

1.下载安装包

https://download.redis.io/releases/

2.解压压缩包

3.移动至/usr/local/redis

4.编译安装

make && make install

5.redis 启动

切换至redis根目录,创建bin和etc文件,将redis.conf文件移动到/usr/local/redis/etc/目录,将mkreleasehdr.sh redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-server 文件移动到/usr/local/redis/bin/目录。

6.设置redis后台启动

vim redis.conf

将daemonize no 改为yes

7.允许远程访问

找到bind 127.0.0.1 注释掉

8.启动redis

./redis-server

9.设置开机自启动

cat /etc/systemd/system/redis.service
[Unit]
Description=redis-server
After=network.target [Service]
Type=forking
# 这行配置内容要根据redis的安装目录自定义路径
ExecStart=/usr/local/redis-6.0.5/bin/redis-server /usr/local/redis-6.0.5/redis.conf
PrivateTmp=true [Install]
WantedBy=multi-user.target systemctl daemon-reload
systemctl enable --now redis

附带自动化安装redis脚本

#!/bin/bash
#author: Turbo
# -------------------------------------------------------------------
# Script Name: download_redis.sh
# Description: A script to install and uninstall Redis.
# Author: Turbo
# Version: 1.0
# Created: 2024-12-21
# Last Updated: 2025-02-17
# License: MIT License
# -------------------------------------------------------------------
# Copyright (c) 2023 Turbo
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# -------------------------------------------------------------------
# Contact:
# - GitHub: https://gitee.com/lian-haoxiong/
# ------------------------------------------------------------------- # 定义颜色常量
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color # 添加带日志信息的分割线函数
print_log_separator() {
local message="$1"
local level="$2"
local color="$NC"
local line_length=60
local message_length=${#message}
local padding_length=$(( (line_length - message_length - 4) / 2 ))
local padding=$(printf '%*s' "$padding_length" | tr ' ' '=') case "$level" in
info)
color="$GREEN"
;;
warning)
color="$YELLOW"
;;
error)
color="$RED"
;;
*)
color="$YELLOW"
;;
esac echo -e "${color}${padding} ${message} ${padding}${NC}"
} show_help() {
print_log_separator "显示帮助信息" "info"
echo -e "${GREEN}使用方法: $0 [版本号] [安装目录]${NC}"
echo -e "${GREEN}示例: $0 6.2.11 /opt${NC}"
echo -e "${GREEN}如果不提供版本号,默认安装 Redis 6.0.5${NC}"
echo -e "${GREEN}如果不提供安装目录,默认安装在 /usr/local/${NC}"
echo -e "${GREEN}卸载 Redis: $0 --uninstall [安装目录]${NC}"
exit 0
} uninstall_redis() {
read -p "请输入安装目录 (默认 /usr/local/): " INSTALL_DIR
if [ -z "$INSTALL_DIR" ]; then
INSTALL_DIR="/usr/local/"
fi REDIS_DIR="$INSTALL_DIR/redis-*" # 卸载 Redis
print_log_separator "卸载现有的 Redis 安装..." "warning"
sudo systemctl stop redis
sudo systemctl disable redis
sudo rm -f /etc/systemd/system/redis.service
sudo systemctl daemon-reload
sudo userdel -r redis
sudo rm -rf $REDIS_DIR
sudo rm -rf $INSTALL_DIR/src/redis-*
sudo rm -rf $INSTALL_DIR/redis-*.tar.gz echo -e "${GREEN}Redis 卸载完成!${NC}"
exit 0
} check_existing_redis() {
if systemctl is-active --quiet redis; then
print_log_separator "检测到已安装 Redis" "warning"
echo -e "${YELLOW}检测到已安装 Redis。${NC}"
read -p "是否要卸载现有的 Redis 安装? (y/n): " UNINSTALL
if [ "$UNINSTALL" == "y" ]; then
uninstall_redis
else
read -p "是否要修改 Redis 的端口? (y/n): " CHANGE_PORT
if [ "$CHANGE_PORT" == "y" ]; then
read -p "请输入新的 Redis 端口 (默认 6379): " NEW_PORT
if [ -z "$NEW_PORT" ]; then
NEW_PORT="6379"
fi
fi
fi
fi
} get_redis_versions() {
print_log_separator "正在获取 Redis 官方版本列表..." "info"
VERSIONS_URL="https://download.redis.io/releases/"
VERSIONS_PAGE=$(curl -s $VERSIONS_URL) # 解析版本列表
IFS=$'\n' read -rd '' -a versions <<< "$(echo "$VERSIONS_PAGE" | grep -oP 'redis-\K[\d]+\.[\d]+\.[\d]+' | sort -rV)"
echo "${versions[@]}"
} select_redis_version() {
local versions=("$@")
print_log_separator "展示 Redis 版本列表" "info"
echo "请选择要安装的 Redis 版本:"
for i in "${!versions[@]}"; do
echo "[$i] ${versions[$i]}"
done # 用户选择版本
read -p "请输入编号 (默认 6.0.5): " choice
# 检查用户输入是否合法
if [[ "$choice" =~ ^[0-9]+$ ]] && [ "$choice" -lt "${#versions[@]}" ]; then
REDIS_VERSION=${versions[$choice]}
else
echo "无效的选择或未输入选择,使用默认版本 6.0.5"
REDIS_VERSION="6.0.5"
fi
} install_dependencies() {
print_log_separator "更新软件包并安装必要的依赖..." "info"
if [[ "$OS" == "ubuntu" || "$OS" == "debian" ]]; then
sudo apt-get update
sudo apt-get install -y build-essential tcl wget curl
elif [[ "$OS" == "centos" || "$OS" == "rhel" ]]; then
sudo yum install -y gcc make tcl wget curl
else
echo -e "${RED}不支持的操作系统: $OS${NC}"
exit 1
fi
} download_and_extract_redis() {
print_log_separator "下载 Redis $REDIS_VERSION..." "info"
wget "$DOWNLOAD_URL"
if [ $? -ne 0 ]; then
echo -e "${RED}下载失败,请检查 Redis 版本号。${NC}"
exit 1
fi # 解压
print_log_separator "解压 Redis..." "info"
sudo tar xf "redis-$REDIS_VERSION.tar.gz"
} compile_redis() {
print_log_separator "编译 Redis..." "info"
cd "redis-$REDIS_VERSION"
sudo make
sudo make install
} configure_redis() {
print_log_separator "配置 Redis..." "info"
sudo sed -i 's/protected-mode yes/protected-mode no/' "$REDIS_DIR/redis.conf"
sudo sed -i 's/bind 127.0.0.1/#bind 127.0.0.1/' "$REDIS_DIR/redis.conf" # 修改端口
if [ -n "$NEW_PORT" ]; then
print_log_separator "修改 Redis 端口为 $NEW_PORT..." "info"
sudo sed -i "s/^port 6379/port $NEW_PORT/" "$REDIS_DIR/redis.conf"
fi
} create_redis_user_and_dirs() {
print_log_separator "创建 Redis 用户和数据目录..." "info"
sudo useradd redis
} create_redis_service() {
print_log_separator "创建 Redis Systemd 服务..." "info"
sudo tee /etc/systemd/system/redis.service > /dev/null <<EOF
[Unit]
Description=Redis In-Memory Data Store
After=network.target [Service]
User=redis
Group=redis
ExecStart=$REDIS_DIR/src/redis-server $REDIS_DIR/redis.conf
ExecStop=$REDIS_DIR/src/redis-cli shutdown
Restart=always [Install]
WantedBy=multi-user.target
EOF
} start_and_enable_redis() {
print_log_separator "启动并启用 Redis 服务..." "info"
sudo systemctl daemon-reload
sudo systemctl start redis
sudo systemctl enable redis
} download_and_install_redis() {
print_log_separator "开始脚本执行" "info"
# 检查是否需要帮助信息
if [ "$1" == "--help" ]; then
show_help
fi # 检查是否需要卸载 Redis
if [ "$1" == "--uninstall" ]; then
uninstall_redis
fi # 检查是否已安装 Redis
check_existing_redis # 获取 Redis 版本列表
versions=($(get_redis_versions)) # 用户选择版本
select_redis_version "${versions[@]}" # 用户输入安装目录
read -p "请输入安装目录 (默认 /usr/local/): " INSTALL_DIR
if [ -z "$INSTALL_DIR" ]; then
INSTALL_DIR="/usr/local/"
fi REDIS_DIR="$INSTALL_DIR/redis-$REDIS_VERSION"
DOWNLOAD_URL="http://download.redis.io/releases/redis-$REDIS_VERSION.tar.gz"
SOURCE_DIR="$INSTALL_DIR/src" # 判断系统类型
if [ -f /etc/os-release ]; then
. /etc/os-release
OS=$ID
else
echo "无法识别操作系统类型,请手动安装所需依赖。"
exit 1
fi # 安装必要的依赖
install_dependencies # 创建源代码目录
sudo mkdir -p "$INSTALL_DIR"
cd "$INSTALL_DIR" # 下载并解压 Redis
download_and_extract_redis # 编译 Redis
compile_redis # 配置 Redis
configure_redis # 创建 Redis 用户和数据目录
create_redis_user_and_dirs # 创建 Systemd 服务
create_redis_service # 启动并启用 Redis 服务
start_and_enable_redis print_log_separator "安装完成" "info"
echo -e "${GREEN}Redis $REDIS_VERSION 安装完成并启动成功!${NC}"
} # 调用函数
download_and_install_redis "$@"

linux(Ubuntu22.04)二进制安装mysql及redis的更多相关文章

  1. 使用docker安装mysql和redis

    本文介绍在linux下使用docker安装mysql和redis. 原文地址:代码汇个人博客 http://www.codehui.net/info/59.html 测试环境:centos7.6,do ...

  2. 记录CentOS 7.4 上安装MySQL&MariaDB&Redis&Mongodb

    记录CentOS 7.4 上安装MySQL&MariaDB&Redis&Mongodb 前段时间我个人Google服务器意外不能用,并且我犯了一件很低级的错误,直接在gcp讲服 ...

  3. Ubuntu18.04下安装MySQL

    Ubuntu上安装MySQL非常简单只需要几条命令就可以完成. 1. sudo apt-get install mysql-server 2. apt-get isntall mysql-client ...

  4. centOS Linux下用yum安装mysql

    centOS Linux下用yum安装mysql      第一篇:安装和配置MySQL   第一步:安装MySQL   [root@192 local]# yum -y install mysql- ...

  5. linux下使用yum安装mysql、tomcat、httpd

    一.linux下使用yum安装mysql   1.安装 查看有没有安装过:           yum list installed mysql*           rpm -qa | grep m ...

  6. Linux centos7环境下安装MySQL的步骤详解

    Linux centos7环境下安装MySQL的步骤详解 安装MySQL mysql 有两个跟windows不同的地方 1).my.ini 保存到/etc/my.ini 2).用户权限,单独用户执行 ...

  7. CentOS7攻克日记(四) —— 安装Mysql和Redis

    这一篇主要安装mysql,redis等数据库   在这篇开始之前,有一个坑,上一篇更改python软连接的时候,尽量都用名字是python3来软连接/usr/../bin/python3.6,把名字是 ...

  8. Ubuntu 12.04上安装MySQL并运行

    Ubuntu 12.04上安装MySQL并运行 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 安装MySQL数据库 sudo apt-get upda ...

  9. 二进制安装MySQL数据库

    今天安装的是二进制的mysql包5.7.21的包,在配置文件的时候采了好多坑,左后还是搞定了,来和大家分享一下 二进制msyql5.7.21版本的主从复制安装 新建/picclife目录 mkdir  ...

  10. [mysql] linux下使用yum安装mysql

    From: http://www.2cto.com/database/201207/141878.html linux下使用yum安装mysql   1.安装 查看有没有安装过:           ...

随机推荐

  1. PaddleOCR学习笔记1

    尝试使用PaddleOCR方法,如何使用自定义的模型方法,参数怎么配置,图片识别尝试简单提高识别率方法. 目前仅仅只是初步学习下如何使用PaddleOCR的方法. 一,测试识别图片: 1.png : ...

  2. http状态码413,并提示Request Entity Too Large的解决办法

    使用wordpress的用户经常遇到的问题,就是在后台上传多媒体文件的时候,发现文件大小是有限制的,通常是2M.如图: 如果上传的文件超过2M,服务端返回的状态码会是413,同时提示上传失败.实际上, ...

  3. Docker swarm集群增加节点

    docker swarm初始化 docker swarm init docker swarm 增加节点 在已经初始化的机器上执行:# docker swarm join-token manager T ...

  4. C# 多文件打包

    public HttpResponseMessage GetZip() { var response = Request.CreateResponse(HttpStatusCode.OK); try ...

  5. 【SpringCloud】OpenFeign服务接口调用

    OpenFeign服务接口调用 概述 我的理解: feign 为什么叫伪装? Feign可以把Rest的请求进行隐藏,伪装成类似SpringMVC的Controller一样.你不用再自己拼接url,拼 ...

  6. k8s集群创建之后coredns一直处于pending状态

    按照官网教程 master节点kubectl init, 每个从节点kubectl join之后, 在master节点执行 kubectl get pods -n kube-system,发现core ...

  7. 邮件自动回复助手(Rasa/SMTP)实现教程

    在现代办公场景中,处理大量邮件是一项既耗时又容易出错的任务.为了提升工作效率,我们可以利用自然语言处理(NLP)和邮件传输协议(SMTP)技术,构建一个智能的邮件自动回复助手.本文将详细介绍如何使用P ...

  8. asp.net里cookie、session进一步理解

    参照: session+cookie简单讲解以及持久化登录实现_session实现用户登录_AkagiSenpai的博客-CSDN博客 sessionID和cookie - 哈哈呵h - 博客园 (c ...

  9. 驾驭FastAPI多数据库:从读写分离到跨库事务的艺术

    title: 驾驭FastAPI多数据库:从读写分离到跨库事务的艺术 date: 2025/05/16 00:58:24 updated: 2025/05/16 00:58:24 author: cm ...

  10. C#交换方法指针

    被引用的dll是testDllFr.dll,其代码为: namespace testDLLFr { public class TestA { public static void TestAM() { ...