Ubuntu 安装 MySQL8 配置、授权、备份、远程连接

目录

  • 0100 系统环境
  • 0200 下载
  • 0300 安装
  • 0400 服务管理
    • 0401 关闭、启动、重启服务
    • 0402 查看服务状态
  • 0500 查看配置文件
  • 0600 账号管理
    • 0601 添加账号
    • 0602 删除账号
    • 0603 修改密码
    • 0604 忘记root密码
  • 0700 自动备份
  • 0800 远程访问

0100 系统环境

[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
Ubuntu 安装 MySQL8 配置、授权、备份、远程连接_第1张图片

# 下载
wget https://cdn.mysql.com//Downloads/MySQL-8.2/mysql-server_8.2.0-1ubuntu22.04_amd64.deb-bundle.tar

0300 安装

# 解压
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 关闭、启动、重启服务

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 查看服务状态

ps aux | grep mysql
# 或者
sudo service mysql status
# 或者
sudo systemctl status mysql

0500 查看配置文件

which mysql
/usr/bin/mysql
 /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 
sudo vim /etc/mysql/my.cnf
...
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
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查看

mysql -uroot -p
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 添加账号

# 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 删除账号

-- 添加
CREATE USER 'aaa'@'localhost' IDENTIFIED BY '123456';
-- 删除
DROP USER 'aaa'@'localhost';

0603 修改密码

ALTER USER 'auser'@'%' IDENTIFIED WITH MYSQL_NATIVE_PASSWORD BY '234567';
FLUSH PRIVILEGES;

0604 忘记root密码

  • 关闭服务,安全模式启动服务,跳过权限检查
# 关闭服务
sudo service mysql stop

# 安全模式启动服务,跳过权限检查
sudo mysqld_safe --skip-grant-tables &
  • 进入数据库,不用输入密码
mysql -uroot -p
--先刷新权限
flush privileges;

-- 修改密码
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';

--再刷新权限
flush privileges;
-- 退出
exit
  • 关闭服务,正常启动,验证
# 关闭服务
sudo service mysql stop

# 正常启动服务
sudo systemctl start mysql

mysql -uroot -p

0700 自动备份

  • 创建备份脚本
vim /usr/bin/mysql_dump.sh
  • 内容如下:
#!/bin/bash
  
name=mysql_dump_`date '+%Y%m%d_%H%M'`.sql.gz
 
mysqldump -uduser-P3306 -p123456 -B dbname | gzip > /bak/db/$name
  • 脚本授权
chmod -R 755 /usr/bin/mysql_dump.sh
  • 添加到定时任务
crontab -e
 
20 1 * * * /usr/bin/mysql_dump.sh

0800 远程访问

# 查看防火墙状态
sudo ufw status

# 关闭防火墙
sudo ufw disable

# 开启防火墙
sudo ufw enable

# 开放端口
sudo ufw allow 3306

# 立即生效
sudo ufw reload

# 查看已开放端口
netstat -ntpl
# 远程连接
mysql -hIP -u账号 -P端口 -p

你可能感兴趣的:(ubuntu,数据库,mysql,运维,服务器)