MySQL安装与使用

1.安装

# Debian 12 安装 MySQL
sudo apt update
sudo apt install default-mysql-server

# 查看服务
systemctl status mysql

# 重启服务
systemctl restart mysql

# 查看版本
mysql --version
# mysql  Ver 15.1 Distrib 10.11.6-MariaDB, for debian-linux-gnu (x86_64) using  EditLine wrapper

# 进入命令界面
mysql
# 查看数据库
> show databases;

2.使用

# 连接
mysql  # root用户可直接登录,无需输入密码
# 设置密码
mysqladmin -u root password your_password
# 普通用户连接
mysql -u root -p

安全配置 mysql_secure_installation 依据提示进行安全配置

-- 创建数据库
create database testdb1;
create databse if not exists testdb1;

-- 删除数据库
drop database testdb1;

-- 选择数据库
use testdb1;

-- 创建表
CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(50) NOT NULL,
    email VARCHAR(100) NOT NULL,
    birthdate DATE,
    is_active BOOLEAN DEFAULT TRUE
);

-- 删除表
drop table users;

-- 插入数据
INSERT INTO users (username, email, birthdate, is_active)
VALUES
    ('test1', 'test1@runoob.com', '1985-07-10', true),
    ('test2', 'test2@runoob.com', '1988-11-25', false),
    ('test3', 'test3@runoob.com', '1993-05-03', true);

-- 查询数据
select * from users;

3.配置dbeaver连接MySQL

MySQL配置外部访问

vi /etc/mysql/mariadb.conf.d/50-server.cnf
# 配置 bind-address = 0.0.0.0
# 重启mysql

dbeaver下载地址: https://github.com/dbeaver/dbeaver/releases

dbeaver配置

  1. 数据库 - 新建数据库连接 - MySQL
  2. 配置 服务器地址: 服务器IP 端口:3306 数据库: testdb1 认证用户名: root 密码: 配置的密码
  3. 测试连接

参考文档:

  1. https://www.runoob.com/mysql/mysql-tutorial.html
posted @ 2024-06-02 14:29  rustling  阅读(10)  评论(0编辑  收藏  举报