Debian12 MySQL8安装
先安装apt update && apt install wget net-tools -y
下载MySQL
MySQL官网下载
MySQL版本为:8.0.35
# 二进制安装
cd /usr/local/src
wget https://cdn.mysql.com/archives/mysql-8.0/mysql-8.0.35-linux-glibc2.28-x86_64.tar.xz
# 编译安装
cd /usr/local/src
wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.35.tar.gz
安装与配置MySQL
二进制文件安装MySQL
安装必要包
apt install -y libaio1 numactl libssl-dev libncurses5-dev libtinfo5 zlib1g-dev
下载mysql二进制包并解压
# 解压
tar xf mysql-8.0.35-linux-glibc2.28-x86_64.tar.xz
# 剪切
mv mysql-8.0.35-linux-glibc2.28-x86_64 /usr/local/mysql
添加环境变量
echo -e '# MySQL8\nexport PATH=$PATH:/usr/local/mysql/bin' > /etc/profile.d/mysql8.sh
# 使环境变量生效
source /etc/profile
创建用户和组
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
设置目录权限
chown -R mysql:mysql /usr/local/mysql
chmod -R 755 /usr/local/mysql
创建mysql.service
cat > /etc/systemd/system/mysql.service << EOF
[Unit]
Description=MySQL Community Server
After=network.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/usr/local/mysql/my.cnf
ExecStop=/usr/local/mysql/bin/mysqladmin shutdown
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
创建my.cnf(也可以向编译安装中的my.cnf那么写,当然步骤也要按照编译安装的来,从创建目录开始)
配置可以参考官网配置
cat > /usr/local/mysql/my.cnf << EOF
[client]
port = 3306
[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
tmpdir = /tmp
port = 3306
lc-messages-dir = /usr/local/mysql/share
EOF
初始化 (此步骤会生成root临时密码)
mysqld --initialize --user=mysql --basedir=/usr/local/mysql
启动mysql
systemctl daemon-reload && systemctl restart mysql.service && systemctl status mysql.service
登陆数据库
mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 29
Server version: 8.0.35 Source distribution
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>
修改密码请参考密码与权限
编译安装MySQL
安装必要库与编译软件
apt install pkg-config git build-essential cmake bison doxygen openssl libssl-dev libncurses5-dev libtinfo5 zlib1g-dev libffi-dev -y
编译安装MySQL
cd /usr/local/src/mysql-8.0.35/
编译指令 如下指令均参考官网源配置
cmake \
-DFORCE_INSOURCE_BUILD=1 \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/data/mysql \
-DSYSCONFDIR=/etc/mysql \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DDEFAULT_CHARSET=utf8mb4 \
-DDEFAULT_COLLATION=utf8mb4_0900_ai_ci \
-DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/boost
看到如下即编译完成
-- Configuring done
-- Generating done
-- Build files have been written to: /usr/local/src/mysql-8.0.35
安装
make && make install
配置MySQL
配置环境变量
echo -e '# MySQL8\nexport PATH=$PATH:/usr/local/mysql/bin' > /etc/profile.d/mysql8.sh
# 使环境变量生效
source /etc/profile
创建用户与目录
# 创建目录
mkdir -p /data/mysql /etc/mysql /var/log/mysql /var/lib/mysql /var/run/mysqld
# 创建mysql组和mysql用户
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
chown -R mysql:mysql /data/mysql /var/log/mysql /var/lib/mysql /var/run/mysqld
chmod -R 755 /data/mysql /var/log/mysql /var/lib/mysql /var/run/mysqld
vim /etc/mysql/my.cnf
[client]
port = 3306
socket = /var/lib/mysql/mysql.sock
[mysqld]
# 基本设置
user = mysql
port = 3306
tmpdir = /tmp
datadir = /data/mysql
basedir = /usr/local/mysql
socket = /var/lib/mysql/mysql.sock
pid_file = /var/run/mysqld/mysqld.pid
# 日志设置
log_error = /var/log/mysql/error.log
log_bin = /var/log/mysql/mysql-bin.log
binlog_expire_logs_seconds = 864000
slow_query_log = 1
slow_query_log_file = /var/log/mysql/mysql-slow.log
long_query_time = 2
lc_messages_dir = /usr/local/mysql/share
# 字符集设置
character-set-server = utf8mb4
collation-server = utf8mb4_0900_ai_ci
# 网络设置
# bind-address = 0.0.0.0 # 允许所有 IP 连接(生产环境建议限制 IP)
# max_connections = 10 # 最大连接数
# 安全设置
# skip-name-resolve # 禁止主机名解析,以提高安全性和性能
# InnoDB 设置
innodb_data_home_dir = /data/mysql
innodb_log_group_home_dir = /data/mysql
innodb_data_file_path = ibdata1:10M:autoextend
innodb_buffer_pool_size = 1G # InnoDB 缓冲池大小(更具服务器内存可调整)
innodb_redo_log_capacity = 512M # InnoDB 日志文件大小
innodb_flush_log_at_trx_commit = 1 # 日志刷新策略
innodb_file_per_table = 1 # 每个表使用独立的表空间
# 缓存和临时文件设置
tmp_table_size = 64M # 临时表大小
max_heap_table_size = 64M # 内存表的最大大小
[mysqldump]
quick
[mysqld_safe]
log_error = /var/log/mysql/error.log
pid_file = /var/run/mysql/mysql.pid
[mysql]
no-auto-rehash
[isamchk]
key_buffer = 16M
[myisamchk]
key_buffer = 16M
[mysqlhotcopy]
interactive-timeout
初始化数据库
mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
查看root用户的临时密码
cat /var/log/mysql/error.log | grep 'temporary password'
A temporary password is generated for root@localhost: (f%If(1N;!1=
创建mysql.service
vim /etc/systemd/system/mysql.service
[Unit]
Description=MySQL8 Server
After=network.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/mysql/my.cnf
LimitNOFILE = 5000
[Install]
WantedBy=multi-user.target
启动mysql
systemctl daemon-reload && systemctl restart mysql.service && systemctl status mysql.service
登陆数据库
mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 29
Server version: 8.0.35 Source distribution
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>
密码与权限
创建、删除用户
# 创建用户
CREATE USER 'username'@'%' IDENTIFIED BY 'password';
# 删除用户
DROP USER 'username'@'%';
# 刷新权限(通常不需要)
FLUSH PRIVILEGES;
# 查看用户
SELECT User, Host FROM mysql.user;
'username'
是用户名'%'
表示允许从任何主机连接。可以替换为特定的 IP 地址,如'192.168.1.100'
修改密码
- 第一种 进入数据库修改
mysql -u root -p
SET PASSWORD FOR 'root'@'localhost' = '123456';
FLUSH PRIVILEGES;
- 第二种 使用mysql_secure_installation(这里也可以重置root能不能远程登陆)
mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root:
The 'validate_password' component is installed on the server.
The subsequent steps will run with the existing configuration
of the component.
Using existing password for root.
Estimated strength of the password: 25
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y
New password:
Re-enter new password:
Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y # 表示是否使用提供的密码
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y # 是否禁止匿名用户登陆
Success.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n # 表示root能不能远程登陆
... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y #是否删除test数据库(是测试数据库)
- Dropping test database...
Success.
- Removing privileges on test database...
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y # 是否立马生效
Success.
All done!
修改密码复杂程度
SET GLOBAL validate_password.length = 6;
设置密码长度SET GLOBAL validate_password.mixed_case_count = 0;
设置密码需要多少个大写字母SET GLOBAL validate_password.number_count = 0;
设置密码需要多少个数字SET GLOBAL validate_password.special_char_count = 0;
设置密码需要多少个特殊字符SET GLOBAL validate_password.policy = LOW;
密码策略级别,可以是LOW
、MEDIUM
或STRONG
SHOW VARIABLES LIKE 'validate_password%';
查看当前策略mysql SHOW VARIABLES LIKE 'validate_password%'; +-------------------------------------------------+-------+ | Variable_name | Value | +-------------------------------------------------+-------+ | validate_password.changed_characters_percentage | 0 | | validate_password.check_user_name | ON | | validate_password.dictionary_file | | | validate_password.length | 8 | | validate_password.mixed_case_count | 1 | | validate_password.number_count | 1 | | validate_password.policy | LOW | | validate_password.special_char_count | 1 | +-------------------------------------------------+-------+
远程登录给予现有用户
GRANT ALL PRIVILEGES ON *.* TO 'username'@'%' IDENTIFIED BY 'your_password' WITH GRANT OPTION;
# 刷新缓存
FLUSH PRIVILEGES;
'username'
是用户名'%'
表示允许从任何主机连接。可以替换为特定的 IP 地址,如'192.168.1.100'
'your_password'
是root
用户的密码
报错
查看错误日志
# 错误日志所在的地方,my.cnf中如果写的如下,就在/var/log/mysql/error.log,否则会直接输出到控制台
log_error = /var/log/mysql/error.log
初始化失败
# 删除数据目录里的所有东西在执行
rm -rf /data/mysql # 看清楚my.cnf的datadir
# 初始化
mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql # 这里的datadir需要和my.cnf一样
pkg-config未安装
CMake Warning at cmake/pkg-config.cmake:29 (MESSAGE):
Cannot find pkg-config. You need to install the required package:
···
# 执行
apt install pkg-config -y
boost未找到(试过安装libboost-all-dev还是报错说未找到)
CMake Error at cmake/boost.cmake:108 (MESSAGE):
You can download it with -DDOWNLOAD_BOOST=1 -DWITH_BOOST=<directory>
···
# 在编译指令最后加上
-DDOWNLOAD_BOOST=1 -DWITH_BOOST={boost准备安装的目录}
# 可以自己安装
wget https://archives.boost.io/release/1.85.0/source/boost_1_85_0.tar.bz2
tar --bzip2 -xf boost_1_77_0.tar.bz2 -C {boost准备安装的目录}
# 在编译指令最后加上
-DWITH_BOOST={boost安装的目录}
让你换个目录编译
CMake Error at CMakeLists.txt:604 (MESSAGE):
Please do not build in-source. Out-of source builds are highly
recommended: you can have multiple builds for the same source, and there is
an easy way to do cleanup, simply remove the build directory (note that
'make clean' or 'make distclean' does *not* work)
You *can* force in-source build by invoking cmake with
-DFORCE_INSOURCE_BUILD=1
# 直接加它最后一句话
# 换个目录编译 如下
cd
mkdir build
cd build
# 然后执行编译安装那段话(其中的-DFORCE_INSOURCE_BUILD=1去掉)
# 然后make && make install