1.下载数据库,地址https://downloads.mysql.com/archives/community/,选择8.0.32版本,下载安装包mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz;
2.将安装包解压放在/usr/local目录下:
tar xvf -C mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz /usr/local/
mv /usr/local/ mysql-8.0.32-linux-glibc2.12-x86_64 /usr/local/mysql-8.0.32
3.创建用户和用户组
[root@localhost local]# groupadd mysql
[root@localhost local]# useradd -M -g mysql -s /sbin/nologin mysql
4.创建MySQL目录并授权
4.1创建MySQL数据目录
[root@localhost local]# mkdir -p /data/mysql
[root@localhost local]# chown mysql:mysql -R /data/mysql
4.2创建MySQL日志目录
[root@localhost local]# mkdir -p /var/log/mysql
[root@localhost local]# chown mysql:mysql -R /data/mysql
5.环境变量
5.1创建mysql目录软连接
[root@localhost local]# ln -s /usr/local/mysql-8.0.32/ /usr/local/mysql
[root@localhost local]# chown -R mysql:mysql mysql-8.0.32/
[root@localhost local]# chown -R mysql:mysql mysql
5.2创建MySQL环境变量配置文件
[root@localhost local]# echo 'MYSQL_HOME=/usr/local/mysql-8.0.32' >> /etc/profile.d/mysql.sh
[root@localhost local]# echo 'export PATH=$PATH:$MYSQL_HOME/bin' >> /etc/profile.d/mysql.sh
[root@localhost local]# cat /etc/profile.d/mysql.sh
MYSQL_HOME=/usr/local/mysql-8.0.32
export PATH=$PATH:$MYSQL_HOME/bin
[root@localhost local]# source /etc/profile.d/mysql.sh
6.创建编辑/etc/my.cnf配置文件
vim /etc/my.cnf
[server]
[mysqld]
lower_case_table_names=1
datadir=/data/mysql
basedir=/usr/local/mysql
socket=/data/mysql/mysql.sock
log-error=/var/log/mysql/mysql.log
max_connections=1000
connect_timeout = 60
wait_timeout=600
interactive_timeout=600
lock_wait_timeout=3600
skip_name_resolve=1
skip_external_locking=1
open_files_limit=65535
explicit_defaults_for_timestamp=1
slow_query_log=1
#slow_query_log_file=/var/log/mysql/slow.log
general-log=1
#general-log-file=/var/log/mysql/sql_row.log
key_buffer_size=1M
read_buffer_size=4M
read_rnd_buffer_size=2M
table_open_cache=1024
table_definition_cache=1024
table_open_cache_instances=64
thread_cache_size=512
sort_buffer_size=16M
join_buffer_size=64M
bulk_insert_buffer_size=64M
tmp_table_size=64M
max_heap_table_size=776M
symbolic-links=0
max_allowed_packet =512M
bind-address =0.0.0.0
innodb_file_per_table=1
innodb_buffer_pool_size=1G
innodb_buffer_pool_instances=8
innodb_buffer_pool_load_at_startup=1
innodb_buffer_pool_dump_at_shutdown=1
innodb_log_file_size=1G
innodb_log_buffer_size=32M
innodb_log_files_in_group=2
innodb_flush_log_at_trx_commit=0
innodb_lock_wait_timeout=600
innodb_autoinc_lock_mode=2
innodb_doublewrite=OFF
innodb_thread_concurrency=16
innodb_flush_method=O_DIRECT
innodb_io_capacity=2000
innodb_io_capacity_max=4000
innodb_write_io_threads=32
innodb_read_io_threads=16
innodb_purge_threads=4
innodb_page_cleaners=4
innodb_open_files=65535
innodb_max_dirty_pages_pct=50
innodb_lru_scan_depth=4000
innodb_status_file=1
innodb_change_buffer_max_size=50
innodb_open_files=10000
open_files_limit=10000
[client]
socket=/data/mysql/mysql.sock
[mysql]
socket=/data/mysql/mysql.sock
[mysqladmin]
7.初始化数据库
[root@localhost log]# mysqld --defaults-file=/etc/my.cnf --user=mysql --initialize --datadir=/data/mysql --lower-case-table-names=1
8.配置systemctl mysqld服务
8.1创建编辑配置文件
vim /usr/lib/systemd/system/mysqld.service
[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
Type=notify
# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=0
# Execute pre and post scripts as root
PermissionsStartOnly=true
# Start main service
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
# Use this to switch malloc implementation
EnvironmentFile=-/etc/sysconfig/mysql
# Sets open_files_limit
LimitNOFILE = 10000
Restart=on-failure
RestartPreventExitStatus=1
# Set enviroment variable MYSQLD_PARENT_PID. This is required for restart.
Environment=MYSQLD_PARENT_PID=1
PrivateTmp=false
8.2启动mysql服务、查看状态、添加开机自启动
[root@localhost mysql]# systemctl start mysqld
[root@localhost mysql]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)
Active: active (running) since Wed 2024-08-07 15:56:39 CST; 14s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Main PID: 182646 (mysqld)
Status: "Server is operational"
Tasks: 82
Memory: 2.3G
CGroup: /system.slice/mysqld.service
└─182646 /usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
8月 07 15:56:32 localhost.localdomain systemd[1]: Starting MySQL Server...
8月 07 15:56:39 localhost.localdomain systemd[1]: Started MySQL Server.
[root@localhost mysql]# systemctl enable mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
9.登录MySQL
在MySQL初始化(initialize)完成后会自动为MySQL的root用户生成一个随机初始化密码,记录在MySQL的mysql.log文件中
9.1查找初始化密码
[root@localhost mysql]# grep password /var/log/mysql/mysql.log
2024-08-07T07:54:29.726454Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: ?LRPvlf:a6lQ
9.2登录数据库修改密码
[root@localhost mysql]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.32
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> alter user 'root'@'localhost' identified by "123123";
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql>
借鉴网址:https://cloud.tencent.com/developer/article/2390202