首先卸载apt安装的Mariadb

  1. 第一步
    1
    sudo apt-get remove mysql-*
    如果shell使用的是zsh,会出现如下错误
    1
    zsh: no matches found: mysql-*
    具体原因:
    因为zsh缺省情况下始终自己解释这个 *,而不会传递给 find 来解释。
    解决办法:
    ~/.zshrc中加入:setopt no_nomatch, 然后进行source .zshrc命令
  2. 第二步
    1
    dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P
    它会跳出一个对话框,你选择yes就好了

然后使用官方推荐安装

官方网址如下:

image-20200415110621120

这里选用的是清华大学的源
f👍

  1. 以下是在Ubuntu系统上运行的命令,可以从MariaDB资源库中安装MariaDB 10.4。
    1
    2
    3
    sudo apt-get install software-properties-common
    sudo apt-key adv --fetch-keys 'https://mariadb.org/mariadb_release_signing_key.asc'
    sudo add-apt-repository 'deb [arch=amd64,arm64,ppc64el] http://mirrors.tuna.tsinghua.edu.cn/mariadb/repo/10.4/ubuntu bionic main'
  2. 一旦导入了密钥并添加了存储库,你就可以从MariaDB存储库中安装MariaDB 10.4。
    1
    2
    sudo apt update
    sudo apt install mariadb-server

    当用apt或apt-cache搜索’mariadb’时,MariaDB调试包会显示出来,并且用’-dbgsym’后缀来区分。调试包一般只在开发过程中需要,除非你知道自己需要,否则通常不应该安装调试包。

  3. 查看版本
    1
    mysql --version
    image-20200415111407542
  4. MariaDB安装完成后,服务应该自动启动,您可以使用以下命令进行验证。
    1
    sudo systemctl status mysql
  5. 接下来,通过运行包附带的安全脚本来保护您的MariaDB安装。
    1
    sudo mysql_secure_installation
    然后输入yes/y来解决以下安全问题:
    • 设置root密码? [Y / n]: y
    • 删除匿名用户? (按y | Y代表是,其他代码按否): y
    • 禁止远程root登录? (按y | Y代表是,其他代码按否): y
    • 删除测试数据库并访问它? (按y | Y代表是,其他代码按否): y
    • 现在重新加载特权表? (按y | Y代表是,其他代码按否): y
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
      In order to log into MySQL to secure it, we'll need the current
      password for the root user. If you've just installed MySQL, and
      you haven't set the root password yet, the password will be blank,
      so you should just press enter here.
      Enter current password for root (enter for none):<–初次运行直接回车
      OK, successfully used password, moving on…
      Setting the root password ensures that nobody can log into the MySQL
      root user without the proper authorisation.
      Set root password? [Y/n] <– 是否设置root用户密码,输入y并回车或直接回车
      New password: <– 设置root用户的密码
      Re-enter new password: <– 再输入一次你设置的密码
      Password updated successfully!
      Reloading privilege tables..
      … Success!
      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? [Y/n] <– 是否删除匿名用户,生产环境建议删除,所以直接回车
      … 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? [Y/n] <–是否禁止root远程登录,根据自己的需求选择Y/n并回车,建议禁止
      … Success!
      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? [Y/n] <– 是否删除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? [Y/n] <– 是否重新加载权限表,直接回车
      … Success!
      Cleaning up…
      All done! If you've completed all of the above steps, your MySQL
      installation should now be secure.
      Thanks for using MySQL!

      如果你是生产环境,建议全部直接按回车。

  6. 首先进入 MariaDB 控制台
    1
    mysql -u root -p
  7. 查看端口
    1
    2
    3
    4
    5
    6
    7
    MariaDB [(none)]> show global variables like 'port';
    +---------------+-------+
    | Variable_name | Value |
    +---------------+-------+
    | port | 3306 |
    +---------------+-------+
    1 row in set (0.001 sec)
  8. 修改MariadB的端口
    MariadB 的配置文件在/etc/mysql/my.cnf
    这里有两个关于port的配置。
    [client]节点下面的port表示你用 MariaDB 工具去访问远程服务器的时候,连接远端的默认接口
    举个例子,你可以这样去访问远端的数据库:
    1
    2
    $ mysql -u root -p -h 192.168.0.5
    $ mysql -u root -p -h 192.168.0.5 -P 3306
    第一种方式,用的就是[client]节点下面的port配置作为默认连接端口
    第二种方式,则是手动指定端口
    [mysqld]节点下面也有一个port配置,这个是本地的数据库的端口号。
    我们修改这里的数据。
    修改之后需要重启 MariaDB,这里可能需要 root 权限
    1
    $ sudo /etc/init.d/mysql restart
    再次查看,端口就应该修改了。
  9. 创建一个用户
    创建用户的命令如下:
    1
    MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO user@localhost IDENTIFIED BY password WITH GRANT OPTION;
    其中,user@localhost表示用户名允许在什么地址下访问,这里是本地。
    如果你希望用户可以在任何地址下访问,格式为user@%,使用通配符。
    后面的password是用户的密码。
    前面的ALL PRIVILEGES ON *.*表示授权所有权限在所有数据库上。
    权限细分如下:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    全局管理权限:
    FILE: 在服务器上读写文件。
    PROCESS: 显示或杀死属于其它用户的服务线程。
    RELOAD: 重载访问控制表,刷新日志等。
    SHUTDOWN: 关闭MySQL服务。
    数据库/数据表/数据列权限:
    ALTER: 修改已存在的数据表(例如增加/删除列)和索引。
    CREATE: 建立新的数据库或数据表。
    DELETE: 删除表的记录。
    DROP: 删除数据表或数据库。
    INDEX: 建立或删除索引。
    INSERT: 增加表的记录。
    SELECT: 显示/搜索表的记录。
    UPDATE: 修改表中已存在的记录。
    特别的权限:
    ALL: 允许做任何事(和root一样)。
    USAGE: 只允许登录,其它什么也不允许做。
    完成后,需要执行下面的命令刷新权限表,配置才能立即生效:
    1
    MariaDB [(none)]> FLUSH PRIVILEGES;
    可以用下面的命令查询所有的用户信息:
    1
    MariaDB [(none)]> select host, user from mysql.user;