linux(Ubuntu22.04)二进制安装mysql及redis
安裝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
將解壓的壓縮包移動(dòng)到/usr/local目錄下
2.創(chuàng)建數(shù)據(jù)存放目錄
cd /usr/local/mysql
mkdir -p date
3.創(chuàng)建用戶組和用戶
groupadd mysql
useradd g mysql mysql
4.修改目錄權(quán)限
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
命令執(zhí)行后會(huì)輸出密碼。將密碼復(fù)制下后
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
#如果要設(shè)置lower_case_table_names可以在初始化里面設(shè)置 ./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.設(shè)置mysql開機(jī)自啟動(dòng)
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.創(chuàng)建軟連接用于連接數(shù)據(jù)庫
ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
在ubuntu中登錄數(shù)據(jù)庫是為報(bào)錯(cuò)libncurses.so.5確實(shí)需要安裝
apt install -y libncurses.so5
9.修改mysql登錄密碼
mysql -u root -p
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '自己的密碼';
10 設(shè)置mysql遠(yuǎn)程登錄
use mysql;
update user set host='%' where user='root' limit 1;
flush privileges;
安裝redis
1.下載安裝包
https://download.redis.io/releases/
2.解壓壓縮包
3.移動(dòng)至/usr/local/redis
4.編譯安裝
make && make install
5.redis 啟動(dòng)
切換至redis根目錄,創(chuàng)建bin和etc文件,將redis.conf文件移動(dòng)到/usr/local/redis/etc/目錄,將mkreleasehdr.sh redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-server 文件移動(dòng)到/usr/local/redis/bin/目錄。
6.設(shè)置redis后臺(tái)啟動(dòng)
vim redis.conf
將daemonize no 改為yes
7.允許遠(yuǎn)程訪問
找到bind 127.0.0.1 注釋掉
8.啟動(dòng)redis
./redis-server
9.設(shè)置開機(jī)自啟動(dòng)
cat /etc/systemd/system/redis.service
[Unit]
Description=redis-server
After=network.target
[Service]
Type=forking
# 這行配置內(nèi)容要根據(jù)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
附帶自動(dòng)化安裝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
# 添加帶日志信息的分割線函數(shù)
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}如果不提供版本號,默認(rèn)安裝 Redis 6.0.5${NC}"
echo -e "${GREEN}如果不提供安裝目錄,默認(rèn)安裝在 /usr/local/${NC}"
echo -e "${GREEN}卸載 Redis: $0 --uninstall [安裝目錄]${NC}"
exit 0
}
uninstall_redis() {
read -p "請輸入安裝目錄 (默認(rèn) /usr/local/): " INSTALL_DIR
if [ -z "$INSTALL_DIR" ]; then
INSTALL_DIR="/usr/local/"
fi
REDIS_DIR="$INSTALL_DIR/redis-*"
# 卸載 Redis
print_log_separator "卸載現(xiàn)有的 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 "是否要卸載現(xiàn)有的 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 端口 (默認(rèn) 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 "請輸入編號 (默認(rèn) 6.0.5): " choice
# 檢查用戶輸入是否合法
if [[ "$choice" =~ ^[0-9]+$ ]] && [ "$choice" -lt "${#versions[@]}" ]; then
REDIS_VERSION=${versions[$choice]}
else
echo "無效的選擇或未輸入選擇,使用默認(rèn)版本 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}不支持的操作系統(tǒng): $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 "創(chuàng)建 Redis 用戶和數(shù)據(jù)目錄..." "info"
sudo useradd redis
}
create_redis_service() {
print_log_separator "創(chuàng)建 Redis Systemd 服務(wù)..." "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 "啟動(dòng)并啟用 Redis 服務(wù)..." "info"
sudo systemctl daemon-reload
sudo systemctl start redis
sudo systemctl enable redis
}
download_and_install_redis() {
print_log_separator "開始腳本執(zhí)行" "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 "請輸入安裝目錄 (默認(rèn) /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"
# 判斷系統(tǒng)類型
if [ -f /etc/os-release ]; then
. /etc/os-release
OS=$ID
else
echo "無法識(shí)別操作系統(tǒng)類型,請手動(dòng)安裝所需依賴。"
exit 1
fi
# 安裝必要的依賴
install_dependencies
# 創(chuàng)建源代碼目錄
sudo mkdir -p "$INSTALL_DIR"
cd "$INSTALL_DIR"
# 下載并解壓 Redis
download_and_extract_redis
# 編譯 Redis
compile_redis
# 配置 Redis
configure_redis
# 創(chuàng)建 Redis 用戶和數(shù)據(jù)目錄
create_redis_user_and_dirs
# 創(chuàng)建 Systemd 服務(wù)
create_redis_service
# 啟動(dòng)并啟用 Redis 服務(wù)
start_and_enable_redis
print_log_separator "安裝完成" "info"
echo -e "${GREEN}Redis $REDIS_VERSION 安裝完成并啟動(dòng)成功!${NC}"
}
# 調(diào)用函數(shù)
download_and_install_redis "$@"
總結(jié)
以上是生活随笔為你收集整理的linux(Ubuntu22.04)二进制安装mysql及redis的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 非豁免婴儿配方奶粉是指什么?
- 下一篇: Spring Boot 集成Mybati