数据库 (二) – 数据库安装

一、安装 MySQL

  • 官方网址:
    https://www.mysql.com/
    http://mariadb.org/
  • 版本演变:
    MySQL:5.1 –> 5.5 –> 5.6 –> 5.7 –>8.0
    MariaDB:5.5 –>10.0–> 10.1 –> 10.2 –> 10.3 –> 10.4
  • 安装环境:
    • CentOS Linux release 7.7.1908 (Core)
    • Ubuntu 1804

1.1 Mariadb 安装

下载地址:
https://mariadb.org/download/
https://mirrors.tuna.tsinghua.edu.cn/mariadb/
https://github.com/MariaDB/server

1.1.1 yum 安装

参考:https://mariadb.org/download/#mariadb-repositories
# 修改 yum 源
vim /etc/yum.repos.d/MariaDB.repo
# MariaDB 10.4 CentOS repository list - created 2021-03-31 04:47 UTC
# https://mariadb.org/download/

[mariadb]

name = MariaDB baseurl = https://mirrors.tuna.tsinghua.edu.cn/mariadb/yum/10.4/centos7-amd64 gpgkey=https://mirrors.tuna.tsinghua.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB gpgcheck=1 yum clean all yum makecache sudo yum install MariaDB-server MariaDB-client

1.1.2 编译安装

Ubuntu 系统编译安装
# 安装相关命令
# 参考 https://mariadb.com/kb/en/building-mariadb-on-ubuntu/
apt purge ufw lxd lxd-client lxcfs lxc-common -y
apt install iproute2 ntpdate tcpdump telnet traceroute nfs-kernel-server nfs-common lrzsz tree openssl libssl-dev  libpcre3-dev zlib1g-dev binutils cpp fetchmail flex gcc libarchive-zip-perl libc6-dev libcompress-zlib-perl  libpcre3 libpopt-dev lynx m4 make ncftp nmap perl perl-modules unzip zip zlib1g-dev autoconf  libtool bison autotools-dev g++ build-essential cmake libncurses5-dev -y

# 创建 mysql 用户
groupadd -g 1306 mysql && useradd -g 1306 -u 1306 -m -d /app/mysql -s /bin/bash mysql

# 下载二进制包
wget https://mirrors.tuna.tsinghua.edu.cn/mariadb/mariadb-10.4.18/source/mariadb-10.4.18.tar.gz

# tar xvf
tar xvf mariadb-10.4.18.tar.gz

# 查看帮助
解压后查看INSTALL或README等文件相关内容

# cmake 编译安装
# 参考:https://mariadb.com/kb/en/compiling-mariadb-from-source/
cd mariadb-10.4.18/
cmake . -LH # 查看编译变量介绍
cmake . \
-DCMAKE_INSTALL_PREFIX=/app/mysql \
-DMYSQL_DATADIR=/data/mysql/ \
-DSYSCONFDIR=/etc/ \
-DMYSQL_USER=mysql \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITHOUT_MROONGA_STORAGE_ENGINE=1 \
-DWITH_DEBUG=0 \
-DWITH_READLINE=1 \
-DWITH_SSL=system \
-DWITH_ZLIB=system \
-DWITH_LIBWRAP=0 \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci

make && make install


# 说明
-DCMAKE_INSTALL_PREFIX=/app/mysql # 指向 mysql 安装目录
-DMYSQL_DATADIR=/data/mysql/  # 指向 mysql 数据文件目录
-DSYSCONFDIR=/etc/ # 指向 mysql 配置文件目录
-DMYSQL_USER=mysql # 用户
-DWITH_INNOBASE_STORAGE_ENGINE=1 # 安装 innodb 存储引擎
-DWITH_ARCHIVE_STORAGE_ENGINE=1
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 # 安装 blackhole 存储引擎
-DWITHOUT_MROONGA_STORAGE_ENGINE=1 \
-DWITH_DEBUG=0 \
-DWITH_READLINE=1 \
-DWITH_SSL=system \
-DWITH_ZLIB=system \
-DWITH_LIBWRAP=0 \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci

# PATH路径
echo 'PATH=/app/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh

# 生成数据库文件
cd /app/mysql/
scripts/mysql_install_db --datadir=/data/mysql/ --user=mysql

# 准备配置文件
vim /etc/my.cnf

[client]

port=3306 socket=/tmp/mysql.sock default-character-set=utf8

[mysqld]

port=3306 pid-file=/app/mysql/mysql.pid socket=/tmp/mysql.sock bind-address=0.0.0.0

[mysqld_safe]

log-error=/app/mysql/mysql.log # 准备启动脚本 cp /app/mysql/support-files/mysql.server /etc/init.d/mysqld chown mysql.mysql /etc/init.d/mysqld chown mysql:mysql /app/mysql/ -R # 启动mysql数据库 /etc/init.d/mysqld start service mysqld status # 查看

1.1.3 二进制安装

# 安装相关命令
yum install vim gcc gcc-c++ wget autoconf  net-tools lrzsz iotop lsof iotop bash-completion install curl policycoreutils openssh-server openssh-clients postfix -y

# 下载包
wget https://mirrors.tuna.tsinghua.edu.cn/mariadb//mariadb-10.4.18/bintar-linux-systemd-x86_64/mariadb-10.4.18-linux-systemd-x86_64.tar.gz

# 解包并创建软连接
tar xvf mariadb-10.4.18-linux-systemd-x86_64.tar.gz -C /usr/local/src
ln -sv /usr/local/src/mariadb-10.4.18-linux-systemd-x86_64 /usr/local/mysql

# 创建 mysql 用户
groupadd -g 1306 mysql && useradd -g 1306 -u 1306 -m -d /app/mysql -s /bin/bash mysql
chown  -R mysql.mysql  /usr/local/mysql/* -R

# 初始化
mkdir -pv /data/mysql && chown  -R mysql.mysql  /data   -R
/usr/local/mysql/scripts/mysql_install_db  --user=mysql --datadir=/data/mysql  --basedir=/usr/local/mysql/

# 准备配置文件
vim /etc/my.cnf

[mysqld]

socket=/app/mysql/mysql.sock user=mysql symbolic-links=0 datadir=/data/mysql innodb_file_per_table=1

[client]

port=3306 socket=/app/mysql/mysql.sock

[mysqld_safe]

log-error=/app/mysql/mysqld.log pid-file=/app/mysql/mysql.sock chown mysql.mysql /var/lib/mysql/ -R #准备启动文件 cp /usr/local/src/mariadb-10.4.18-linux-systemd-x86_64/support-files/mysql.server /etc/init.d/mysqld chmod a+x /etc/init.d/mysqld chown mysql.mysql /etc/init.d/mysqld ln -sv /usr/local/mysql/bin/mysql /usr/bin/mysql # 启动 /etc/init.d/mysqld start

1.2 mysql 安装

1.2.1 yum 安装

# 修改 yum 源
vim /etc/yum.repos.d/mysql-community.repo

[mysql57-community]

name = MySQL 5.7 Community Server baseurl = https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql-5.7-community-el7-x86_64/ enabled=1 gpgcheck=0 yum clean all yum makecache yum install mysql-community-server # 解决无法登陆 # 方案 1 [root@cent711 ~]#mysql ERROR 1045 (28000): Access denied for user ‘root’@’localhost’ (using password: NO) [root@cent722 ~]#cat /root/.mysql_secret # 查看初始秘密 # Password set for user ‘root@localhost’ at 2021-04-05 01:15:18 uYr975rPyd13 # 登录并修改初始密码 mysql -uroot -puYr975rPyd13 alter user user() identified by “123456”; # 方案 2 如果应用未生成初始密码文件,则我们以安全模式启动mysql,并跳过身份验证,匿名登入 nohup mysqld_safe –skip-grant-tables & alter user user() identified by “123456”; pkill -9 mysql service mysqld start

1.2.2 二进制安装脚本

#!/bin/bash
DIR=`pwd`
NAME="mysql-5.6.34-linux-glibc2.5-x86_64.tar.gz"
FULL_NAME={DIR}/{NAME}
DATA_DIR="/data/mysql"

yum install vim gcc gcc-c++ wget autoconf  net-tools lrzsz iotop lsof iotop bash-completion -y
yum install curl policycoreutils openssh-server openssh-clients postfix -y

if [ -f {FULL_NAME} ];then
    echo "安装文件存在"
else
    echo "安装文件不存在"
    exit 3
fi
if [ -h /usr/local/mysql ];then
    echo "Mysql 已经安装"
    exit 3 
else
    tar xvf{FULL_NAME}   -C /usr/local/src
    ln -sv /usr/local/src/mysql-5.6.34-linux-glibc2.5-x86_64  /usr/local/mysql
    if id  mysql;then
        echo "mysql 用户已经存在,跳过创建用户过程"
    fi
        useradd  mysql  -s /sbin/nologin
    if  id  mysql;then
        chown  -R mysql.mysql  /usr/local/mysql/* -R
        if [ ! -d  /data/mysql ];then
            mkdir -pv /data/mysql && chown  -R mysql.mysql  /data   -R
            /usr/local/mysql/scripts/mysql_install_db  --user=mysql --datadir=/data/mysql  --basedir=/usr/local/mysql/
        cp  /usr/local/src/mysql-5.6.34-linux-glibc2.5-x86_64/support-files/mysql.server /etc/init.d/mysqld
        chmod a+x /etc/init.d/mysqld
         cp ${DIR}/my.cnf   /etc/my.cnf
        ln -sv /usr/local/mysql/bin/mysql  /usr/bin/mysql
        /etc/init.d/mysqld start
    else
            echo "MySQL数据目录已经存在,"
            exit 3
    fi
    fi
fi
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇