UBUNTU下安装MYSQL

一、安装

1.通过apt命令安装

更新源:

1
$ sudo apt-get update

安装mysql服务

1
$ sudo apt-get install mysql-server

初始化配置

1
$ sudo mysql_secure_installation

配置如下:

  • VALIDATE PASSWORD PLUGIN can be used to test passwords...
    Press y|Y for Yes, any other key for No: N (选择N ,不会进行密码的强校验)
    
    1
    2
    3
    4
    5
    6
    7

    * 设置密码(这一步可能会因为:默认情况下使用 auth_socket 进行身份验证,因此跳过为 root 设置的密码)

    ```shell
    Please set the password for root here...
    New password: (输入密码)
    Re-enter new password: (重复输入)
  • 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...
    Remove anonymous users? (Press y|Y for Yes, any other key for No) : N (选择N,不删除匿名用户)
    
    1
    2
    3
    4
    5
    6

    * ```shell
    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 (选择N,允许root远程连接)
  • By default, MySQL comes with a database named 'test' that
    anyone can access...
    Remove test database and access to it? (Press y|Y for Yes, any other key for No) : N (选择N,不删除test数据库)
    
    1
    2
    3
    4
    5

    * ```shell
    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 (选择Y,修改权限立即生效)

检查mysql服务状态

1
$ systemctl status mysql.service
  • 会出现如下图片:

    image-20240523172714126

查看安装的mysql版本

1
$ mysql --version

2.卸载mysql

1
2
3
4
sudo apt purge mysql-*
sudo rm -rf /etc/mysql/ /var/lib/mysql
sudo apt autoremove
sudo apt autoclean

apt-get purge 与 apt-get remove是不同的,简单来说:

  • purge可以将包以及软件的配置文件全部删除
  • remove仅可以删除包,但不会删除配置文件

3.一些常用的systemctl命令

  • 启动MySQL服务

    1
    sudo systemctl start mysql
  • 停止MySQL服务

    1
    sudo systemctl stop mysql
  • 重启MySQL服务:

    1
    sudo systemctl restart mysql

    如果你对MySQL的配置文件进行了修改,通常需要重启服务以使更改生效。

  • 设置MySQL服务自启动:

    1
    sudo systemctl enable mysql
  • 禁用MySQL服务自启动:

    1
    sudo systemctl disable mysql

4.登录mysql

  • 终端使用root用户登录mysql

    1
    sudo mysql -u root -p

    输入密码,密码就是你的ubuntu系统,root用户的密码

  • 选择mysql数据库

    登录后,切换到mysql数据库,这个数据库存储了关于用户和权限的信息:

    1
    $ use mysql;
  • 查询user表

    1
    $ SELECT User, Host FROM user;
    • User列显示了每个用户的登录名
    • Host列指定了用户可以从哪些主机连接。例如,localhost表示用户只能从本地机器连接
    • image-20240523175813132
  • 查看特定条件的用户

    1
    $ SELECT User FROM user WHERE Host = 'localhost';  # localhost 用户名,上图的zxz或者其他
  • 创建一个新的mysql用户

    使用CREATE USER语句创建一个新用户。你需要指定新用户的用户名和从哪些主机可以连接。同时,你也需要为新用户设置一个密码。以下是一个示例命令:

    1
    2
    # newuser 为 设置的用户名   password 设置的用户密码
    $ CREATE USER 'newuser'@'localhost' IDENTIFIED BY 'password';
    • 'newuser' 是新用户的用户名
    • 'localhost' 指用户只能从本地机器连接。如果你想允许用户从任何主机连接,可以使用'%'作为主机名
    • 'password' 是用户的密码,应替换为一个强密码
  • 授予新创建的用户所有权限

    使用 GRANT 语句来授予 新创建的 用户所有权限。如果你希望这个用户能管理服务器上的所有数据库并执行所有操作(包括添加其他用户和修改权限),可以使用如下命令:

    1
    GRANT ALL PRIVILEGES ON *.* TO '用户名'@'localhost' WITH GRANT OPTION;

    这里的 *.* 表示对所有数据库的所有表授予权限。WITH GRANT OPTION 允许 新创建的用户将其权限授予其他用户

  • 应用权限更改

    执行完 GRANT 语句后,需要运行 FLUSH PRIVILEGES; 命令来立即使权限更改生效:

    1
    FLUSH PRIVILEGES;
  • 权限测试

    使用EXIT命令,退出当前的MYSQL会话,假设新创建的用户为zxz,则使用zxz用户登录测试新的权限

    1
    $ mysql -u zxz -p

    登录后,尝试创建一个数据库或进行其他需要高权限的操作,以验证权限是否正确设置:

    1
    CREATE DATABASE testdb;

5.数据简单操作

(1)创建数据库
  • 创建数据库

    1
    create database 数据库名 ;
  • 除数据库

    1
    drop database 数据库名;
  • 切换数据库

    1
    use 数据库名;
  • 查看当前选择的数据库

    1
    select database();
  • 列出数据库

    1
    show databases;
(2)创建表
  • 查看当前数据库中所有表

    1
    show tables;
  • 创建表

    1
    2
    3
    4
    5
    # create table 表名(列及类型);
    create table students(
    id int auto_increment primary key,
    sname varchar(10) not null
    );
  • 删除表

    1
    drop table 表名;
  • 查看表结构

    1
    desc 表名;
  • 查询表中的所有数据

    1
    select * from 表名;
  • 表中插入数据

    1
    2
    3
    4
    5
    6
    7
    # 全列插入
    insert into 表名 values(…)
    # 缺省插入
    insert into 表名(列1,…) values(值1,…)
    # 同时插入多条数据
    insert into 表名 values(…),(…)…;
    insert into 表名(列1,…) values(值1,…),(值1,…)…;