Ubuntu22.04安装Mariadb

安装Mariadb

shell 复制代码
$ sudo apt update
$ sudo apt install mariadb-server

安装完后,可以用下面命令查看服务状态

shell 复制代码
$ sudo service mariadb status

配置Mariadb

安装完mariadb后,可以使用mysql_secure_installation命令做一下安全配置

shell 复制代码
$ sudo mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none):                     <--- 默认是空,我这里直接回车
OK, successfully used password, moving on...

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] n
 ... skipping.

You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] n
 ... skipping.

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB 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] y
 ... 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] y
 ... Success!

By default, MariaDB 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] n
 ... skipping.

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

验证

配置完成后,可以登录验证一下。

shell 复制代码
$ sudo mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 217
Server version: 10.6.12-MariaDB-0ubuntu0.22.04.1 Ubuntu 22.04

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

创建用户

shell 复制代码
$ sudo mysql
... 
MariaDB [(none)]> GRANT ALL ON *.* TO 'admin'@'localhost' IDENTIFIED BY '<password>' WITH GRANT OPTION;
MariaDB [(none)]> GRANT ALL ON *.* TO 'admin'@'%' IDENTIFIED BY '<password>' WITH GRANT OPTION;
MariaDB [(none)]> FLUSH PRIVILEGES;

使用新用户登录

shell 复制代码
$ mysql -u admin -p<password>

配置允许远程登录

用户创建后,发现不能从远程登录数据库,此时可以修改配置文件 /etc/mysql/my.cnf (可以使用命令 mariadbd --help --verbose | grep my.cnf 查看配置文件位置),在最后添加以下内容(如果已经存在,修改之)

shell 复制代码
[mysqld]
skip-networking=0
skip-bind-address

保存后,重新启动服务

shell 复制代码
$ sudo service mariadb restart

从远程访问数据库

shell 复制代码
$ mysql -h <host> -u admin -p<password>
相关推荐
雨白22 分钟前
深入理解广播机制 (BroadcastReceiver)
android
一只爱撸猫的程序猿1 小时前
构建一个简单的智能文档问答系统实例
数据库·spring boot·aigc
nanzhuhe1 小时前
sql中group by使用场景
数据库·sql·数据挖掘
消失在人海中2 小时前
oracle sql 语句 优化方法
数据库·sql·oracle
Clang's Blog2 小时前
一键搭建 WordPress + MySQL + phpMyAdmin 环境(支持 PHP 版本选择 & 自定义配置)
数据库·mysql·php·wordpr
zzc9212 小时前
MATLAB仿真生成无线通信网络拓扑推理数据集
开发语言·网络·数据库·人工智能·python·深度学习·matlab
未来之窗软件服务2 小时前
JAVASCRIPT 前端数据库-V1--仙盟数据库架构-—-—仙盟创梦IDE
数据库·数据库架构·仙盟创梦ide·东方仙盟数据库
LjQ20402 小时前
网络爬虫一课一得
开发语言·数据库·python·网络爬虫
烙印6012 小时前
MyBatis原理剖析(二)
java·数据库·mybatis
RestCloud2 小时前
如何通过ETLCloud实现跨系统数据同步?
数据库·数据仓库·mysql·etl·数据处理·数据同步·集成平台