Ubuntu 24.04 二進位制安裝 MySQL 8.0.20

klvchen發表於2024-11-08

相關資料

作業系統: Ubuntu 24.04
資料庫下載地址: https://cdn.mysql.com/archives/mysql-8.0/mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz

部署

# 安裝必要的依賴
apt -y install numactl libaio1t64 -y  

ln -sv /usr/lib/x86_64-linux-gnu/libtinfo.so.6.4 /usr/lib/x86_64-linux-gnu/libtinfo.so.5
ln -sv /usr/lib/x86_64-linux-gnu/libaio.so.1t64.0.2 /usr/lib/x86_64-linux-gnu/libaio.so.1

# 新增使用者和使用者組
groupadd -r mysql
useradd -M -g mysql -r -s /bin/false -c "MySQL Server" mysql

# 下載和解壓 mysql 二進位制安裝包 
mkdir /data/software -p
cd /data/software
wget https://cdn.mysql.com/archives/mysql-8.0/mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz 
tar -xvf mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz 
cp -rf mysql-8.0.20-linux-glibc2.12-x86_64 /usr/local/mysql8_3306

# 建立資料和日誌目錄
cd /data/
mkdir mysql8_3306/{data,log,log-bin} -p
touch mysql8_3306/log/mysql-err.log
chown -R mysql:mysql /data/mysql8_3306

# 建立配置
cat >> /usr/local/mysql8_3306/my.cnf << EOF
[mysqld]
port=3306
log-error=/data/mysql8_3306/log/mysql-err.log
basedir=/usr/local/mysql8_3306
datadir=/data/mysql8_3306/data
socket=/data/mysql8_3306/mysql.sock
server-id=1
log-bin=/data/mysql8_3306/log-bin/mysql-bin
character-set-server=utf8mb4
default-storage-engine=INNODB
user=mysql
lower_case_table_names=1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLE
max_connect_errors=2000
max_connections=1500
default-authentication-plugin=mysql_native_password
 
[client]
socket=/data/mysql8_3306/mysql.sock
default-character-set=utf8mb4
EOF

# 初始化資料庫
cd /usr/local/mysql8_3306
./bin/mysqld --defaults-file=/usr/local/mysql8_3306/my.cnf --initialize --console --user=mysql

# 建立 systemctl 配置
cat >/etc/systemd/system/mysqld8_3306.service <<EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql8_3306/bin/mysqld --defaults-file=/usr/local/mysql8_3306/my.cnf
LimitNOFILE = 5000
EOF

# 啟動資料庫和設定開機啟動
systemctl start mysqld8_3306
systemctl enable mysqld8_3306

# 獲取資料庫初始密碼
grep "password" /data/mysql8_3306/log/mysql-err.log

# 使用初始密碼登入資料庫
cd /usr/local/mysql8_3306
./bin/mysql --socket=/data/mysql8_3306/mysql.sock -uroot -p

# 修改密碼
alter user root@localhost identified by "KLVCHEN123456";
flush privileges;
use mysql;
update user set host = '%' where user = 'root';
flush privileges;
ALTER USER 'root'@'%' PASSWORD EXPIRE NEVER;
flush privileges;

相關文章