目录
- [0100 系统环境](#0100 系统环境)
- [0200 下载](#0200 下载)
- [0300 安装](#0300 安装)
- [0400 服务管理](#0400 服务管理)
-
- [0401 关闭、启动、重启服务](#0401 关闭、启动、重启服务)
- [0402 查看服务状态](#0402 查看服务状态)
- [0500 查看配置文件](#0500 查看配置文件)
- [0600 账号管理](#0600 账号管理)
-
- [0601 添加账号](#0601 添加账号)
- [0602 删除账号](#0602 删除账号)
- [0603 修改密码](#0603 修改密码)
- [0604 忘记root密码](#0604 忘记root密码)
- [0700 自动备份](#0700 自动备份)
- [0800 远程访问](#0800 远程访问)
0100 系统环境
shell
[root@localhost ~]# cat /proc/version
Linux version 5.15.0-89-generic (buildd@bos03-amd64-016) (gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0, GNU ld (GNU Binutils for Ubuntu) 2.38) #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023
[root@localhost ~]#
[root@localhost ~]# getconf LONG_BIT
64
0200 下载
https://dev.mysql.com/downloads/mysql/
https://cdn.mysql.com//Downloads/MySQL-8.2/mysql-server_8.2.0-1ubuntu22.04_amd64.deb-bundle.tar
shell
# 下载
wget https://cdn.mysql.com//Downloads/MySQL-8.2/mysql-server_8.2.0-1ubuntu22.04_amd64.deb-bundle.tar
0300 安装
shell
# 解压
tar xvf mysql-server_8.2.0-1ubuntu22.04_amd64.deb-bundle.tar
# 安装,注意顺序
sudo dpkg -i mysql-community-client-plugins_8.2.0-1ubuntu22.04_amd64.deb
sudo dpkg -i mysql-community-client-core_8.2.0-1ubuntu22.04_amd64.deb
sudo dpkg -i mysql-common_8.2.0-1ubuntu22.04_amd64.deb
sudo dpkg -i mysql-community-client_8.2.0-1ubuntu22.04_amd64.deb
sudo dpkg -i libmysqlclient22_8.2.0-1ubuntu22.04_amd64.deb
sudo dpkg -i libmysqlclient-dev_8.2.0-1ubuntu22.04_amd64.deb
sudo dpkg -i mysql-client_8.2.0-1ubuntu22.04_amd64.deb
# 依赖包 如果解压出来没有libaio1这个包,那么直接用apt安装:apt install libaio1
sudo apt install libaio1
# 依赖包
sudo apt install libmecab2
sudo dpkg -i mysql-community-server-core_8.2.0-1ubuntu22.04_amd64.deb
# 这一步要设置root密码,默认root只能localhost连接
sudo dpkg -i mysql-community-server_8.2.0-1ubuntu22.04_amd64.deb
sudo dpkg -i mysql-server_8.2.0-1ubuntu22.04_amd64.deb
# 查看版本
mysql -V
# mysql Ver 8.2.0 for Linux on x86_64 (MySQL Community Server - GPL)
0400 服务管理
0401 关闭、启动、重启服务
shell
sudo service mysql stop
sudo service mysql start
sudo service mysql restart
# 或者
sudo systemctl stop mysql
sudo systemctl start mysql
sudo systemctl restart mysql
0402 查看服务状态
shell
ps aux | grep mysql
# 或者
sudo service mysql status
# 或者
sudo systemctl status mysql
0500 查看配置文件
shell
which mysql
/usr/bin/mysql
shell
/usr/bin/mysql --verbose --help | grep -A 1 'Default options'
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf
shell
sudo vim /etc/mysql/my.cnf
...
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
shell
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
log-error = /var/log/mysql/error.log
也可以连接到mysql查看
shell
mysql -uroot -p
sql
mysql> show variables like '%datadir%';
+---------------+-----------------+
| Variable_name | Value |
+---------------+-----------------+
| datadir | /var/lib/mysql/ |
+---------------+-----------------+
1 row in set (0.00 sec)
mysql> show variables like '%log_err%';
+----------------------------+----------------------------------------+
| Variable_name | Value |
+----------------------------+----------------------------------------+
| binlog_error_action | ABORT_SERVER |
| log_error | /var/log/mysql/error.log |
| log_error_services | log_filter_internal; log_sink_internal |
| log_error_suppression_list | |
| log_error_verbosity | 2 |
+----------------------------+----------------------------------------+
5 rows in set (0.00 sec)
0600 账号管理
0601 添加账号
sql
# read and write
CREATE USER 'auser'@'%' IDENTIFIED BY '123456';
GRANT ALL PRIVILEGES ON *.* TO 'auser'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
# read only
CREATE USER 'ruser'@'%' IDENTIFIED BY '123456';
GRANT SELECT ON *.* TO 'ruser'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
# dump
CREATE USER 'duser'@'localhost' IDENTIFIED BY '123456';
GRANT SELECT, LOCK TABLES, PROCESS ON *.* TO 'duser'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
0602 删除账号
sql
-- 添加
CREATE USER 'aaa'@'localhost' IDENTIFIED BY '123456';
-- 删除
DROP USER 'aaa'@'localhost';
0603 修改密码
sql
ALTER USER 'auser'@'%' IDENTIFIED WITH MYSQL_NATIVE_PASSWORD BY '234567';
FLUSH PRIVILEGES;
0604 忘记root密码
- 关闭服务,安全模式启动服务,跳过权限检查
shell
# 关闭服务
sudo service mysql stop
# 安全模式启动服务,跳过权限检查
sudo mysqld_safe --skip-grant-tables &
- 进入数据库,不用输入密码
shell
mysql -uroot -p
sql
--先刷新权限
flush privileges;
-- 修改密码
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
--再刷新权限
flush privileges;
-- 退出
exit
- 关闭服务,正常启动,验证
shell
# 关闭服务
sudo service mysql stop
# 正常启动服务
sudo systemctl start mysql
mysql -uroot -p
0700 自动备份
- 创建备份脚本
shell
vim /usr/bin/mysql_dump.sh
- 内容如下:
shell
#!/bin/bash
name=mysql_dump_`date '+%Y%m%d_%H%M'`.sql.gz
mysqldump -uduser-P3306 -p123456 -B dbname | gzip > /bak/db/$name
- 脚本授权
shell
chmod -R 755 /usr/bin/mysql_dump.sh
- 添加到定时任务
shell
crontab -e
20 1 * * * /usr/bin/mysql_dump.sh
0800 远程访问
shell
# 查看防火墙状态
sudo ufw status
# 关闭防火墙
sudo ufw disable
# 开启防火墙
sudo ufw enable
# 开放端口
sudo ufw allow 3306
# 立即生效
sudo ufw reload
# 查看已开放端口
netstat -ntpl
shell
# 远程连接
mysql -hIP -u账号 -P端口 -p