1、安装mariadb服务器

~$sudo apt install mariadb-server

2、链接、测试

 ~$sudo mysql -u root -p

测试结果:

~/Desktop$ sudo mysql -u root -p
[sudo] password for kyun:
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 41
Server version: 10.1.43-MariaDB-0ubuntu0.18.04.1 Ubuntu 18.04Copyright (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)]> show databases-> ;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)MariaDB [(none)]> 

3、设置数据库服务器的安全策略

 ~$sudo mysql_secure_installation

配置过程:

~/Desktop$ sudo mysql_secure_installationNOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDBSERVERS 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
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 MariaDB
root user without the proper authorisation.You already have a root password set, so you can safely answer 'n'.
# 修改root密码
Change the root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..... Success!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.
# 禁止root远程登录, 不要开放root远程登录权限
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.
# 删除test数据库和它的权限
Remove test database and access to it? [Y/n] y- 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] y... Success!Cleaning up...
# 完成上述步骤后,我们的MariaDB的安装就是安全的了
All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.Thanks for using MariaDB!

4、启动&重启关闭

  • 查看状态
sudo systemctl status mysql

如:

~/Desktop$ sudo systemctl status mysql
[sudo] password for kyun:
● mariadb.service - MariaDB 10.1.43 database serverLoaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: Active: active (running) since Sat 2019-11-30 00:12:38 CST; 16h agoDocs: man:mysqld(8)https://mariadb.com/kb/en/library/systemd/Main PID: 2401 (mysqld)Status: "Taking your SQL requests now..."Tasks: 28 (limit: 4915)CGroup: /system.slice/mariadb.service└─2401 /usr/sbin/mysqld
  • 启动
sudo systemctl start mysql
  • 重启
sudo systemctl restart mysql
  • 关闭
 sudo systemctl stop mysql

注:在mariadb数据库中,mysql的名称也可以用mariadb名称来代替。

5、配置

  • 登录
~$sudo mysql -u root -p
  • 创建远程登录用户
    格式:create user ‘用户名’@’%’ identified by ‘密码’;
MariaDB [(none)]> create user 'kyunwong'@'%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
  • 授权(如果权限不足)
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'kyunwong'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
  • 查看监听
~$ netstat -an | grep 3306
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN

如果显示监听端口是127.0.0.1:3306, 那么其他计算机将无法连接到数据库。

  • 修改配置文件,使其能够通过远程来访问
vim /etc/mysql/mariadb.conf.d/50-server.cnf

注释掉这一行bind-address = 127.0.0.1,如下图所示:

# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
#bind-address           = 127.0.0.1

50-server.cnf这个文件,配置了数据库服务端的端口、数据库文件目录、临时目录等重要信息。配置文件修改后,都必须重启服务才能生效。

  • 重启mariadb,再查看端口监听状态
sudo systemctl restart mysql
kyun@kyun-HP-348-G3:/etc/mysql/mariadb.conf.d$ netstat -an | grep 3306
tcp6       0      0 :::3306                 :::*                    LISTEN

现在显示监听端口是:::3306, 那么其他计算机就可以连接到数据库了。

远程访问

  • 直接登录
    账号kyunwong,-p表示密码,但后面不用跟上密码,在提示中输入密码即可。
~$ sudo mysql -h localhost -u kyunwong -p
  • 远程登录
    请继续阅读,在后面的部分会给出。

异常问题

  • Access denied for user ‘kyunwong’@‘localhost’

解决方法:

  1. 停止服务
~$ sudo systemctl stop mariadb
  1. 进入安全模式,并设置为后台进程
~$ sudo mysqld_safe --skip-grant-tables &
  1. 登陆数据库
~$ mysql -u root
  1. 修改mysql数据库的user表
# 查询用户
MariaDB [(none)]> select Host,User,plugin from mysql.user where user='root';
# 重置加密模式
MariaDB [(none)]> update mysql.user set plugin='mysql_native_password';
# 重置密码
MariaDB [(none)]> update mysql.user set password=PASSWORD("newpassword") where user='root';
# 刷新权限信息
MariaDB [(none)]> flush privileges;
# 安装unix_socket
MariaDB [(none)]> install plugin unix_socket soname 'auth_socket';
MariaDB [(none)]> exit
  1. 杀掉所有数据库相关的进程,重新启动
 ~$ sudo kill $(pgrep mysql)
  1. 重新启动服务
    数据库服务的名称可以使用mariadb或mysql。
~$ sudo service mariadb start
或
~$ sudo service mysql start
或
~$ sudo systemctl start mysql
或
~$ sudo systemctl start mariadb
  1. 再重新登录
~$ sudo mysql -u kyunwong -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 10.1.43-MariaDB-0ubuntu0.18.04.1 Ubuntu 18.04Copyright (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)]> 

使用数据库

  • 显示数据库
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
3 rows in set (0.08 sec)
  • 创建数据库
MariaDB [(none)]> create database wong_web_db-> ;
Query OK, 1 row affected (0.00 sec)MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| wong_web_db        |
+--------------------+
4 rows in set (0.00 sec)
  • 进入创建好的数据库,并查看现有的表,刚创建的数据库是没有任何表的
MariaDB [(none)]> use wong_web_db;
Database changed
MariaDB [wong_web_db]> show tables;
Empty set (0.00 sec)
  • 创建表
MariaDB [wong_web_db]> create table user(id int(10) auto_increment primary key,name varchar(32) not null);
Query OK, 0 rows affected (0.32 sec)MariaDB [wong_web_db]> show tables;
+-----------------------+
| Tables_in_wong_web_db |
+-----------------------+
| user                  |
+-----------------------+
1 row in set (0.00 sec)
  • 向表中插入一些数据
MariaDB [wong_web_db]> insert into user(name) values("fish");
Query OK, 1 row affected (0.09 sec)
  • 查询
MariaDB [wong_web_db]> select * from user;
+----+------+
| id | name |
+----+------+
|  1 | fish |
+----+------+
1 row in set (0.00 sec)
  • 更新
MariaDB [wong_web_db]> update user set name = "BigBigWorld";
Query OK, 1 row affected (0.52 sec)
Rows matched: 1  Changed: 1  Warnings: 0MariaDB [wong_web_db]> select * from user;
+----+-------------+
| id | name        |
+----+-------------+
|  1 | BigBigWorld |
+----+-------------+
1 row in set (0.00 sec)
  • 删除
MariaDB [wong_web_db]> delete from user where name="BigBigWorld";
Query OK, 1 row affected (0.10 sec)MariaDB [wong_web_db]> select * from user;
Empty set (0.00 sec)

在远程计算机上连接此数据库

方法:

mysql -u 用户名 -p  -h IP地址 -P 端口号 -D 数据库名字

示例:
(1)远程计算机上,安装访问mariadb的客户端

apt-get install mariadb-client

如:

root@f11e41905714:/# apt-get install mariadb-client

(2)使用客户端连接数据库

root@f11e41905714:/# mysql -u kyunwong -p  -h 172.17.0.1 -P 3306 -D wong_web_db
Enter password:
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -AWelcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.1.43-MariaDB-0ubuntu0.18.04.1 Ubuntu 18.04Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MariaDB [wong_web_db]> show tables;
+-----------------------+
| Tables_in_wong_web_db |
+-----------------------+
| user                  |
+-----------------------+
1 row in set (0.001 sec)MariaDB [wong_web_db]> select * from user;
Empty set (0.001 sec)MariaDB [wong_web_db]> insert into user(name) values("hello world");
Query OK, 1 row affected (0.309 sec)MariaDB [wong_web_db]> select * from user;
+----+-------------+
| id | name        |
+----+-------------+
|  2 | hello world |
+----+-------------+
1 row in set (0.001 sec)MariaDB [wong_web_db]> 

以上就是ubuntu18.04安装、使用、远程访问mariadb的所有内容。

谢谢阅读!

ubuntu18.04安装、使用、远程访问mariadb相关推荐

  1. Ubuntu18.04 安装搭建 hadoop-3.3.0 集群

    Ubuntu18.04 安装搭建 hadoop-3.3.0 集群 参考博文:https://blog.csdn.net/sunxiaoju/article/details/85222290?ops_r ...

  2. Ubuntu18.04安装教程及SLAM常用软件安装教程

    文章目录 前言 Ubuntu18.04安装教程 一.准备工作 1.1.下载ubuntu镜像 1.2.制作U盘启动项 1.3.给ubuntu分配硬盘空间 二 安装ubuntu18.04系统 2.1.设置 ...

  3. Ubuntu18.04安装g2o,及问题解决_HyperZhu的博客-CSDN博客_ubuntu18.04安装g2o

    Ubuntu18.04安装g2o,及问题解决_HyperZhu的博客-CSDN博客_ubuntu18.04安装g2o sudo apt-get install cmake libeigen3-dev ...

  4. linux18.04安装显卡驱动,详细介绍ubuntu18.04安装NVIDIA显卡驱动(亲测有效!)

    详细介绍ubuntu18.04安装NVIDIA显卡驱动(亲测有效!) 详细介绍ubuntu18.04安装NVIDIA显卡驱动(亲测有效!) 详细介绍ubuntu18.04安装NVIDIA显卡驱动(亲测 ...

  5. Ubuntu18.04安装Dlib-19.19.0成功

    VMware内Ubuntu18.04安装Dlib-19.19.0成功 本人第一篇博客,如果哪里写的不好还请多多包涵. 说明一下,我是通过pip去安装的所有库,除了Cmake.因为Dlib需要C++环境 ...

  6. ubuntu18.04安装python3.7并将python3指向python3.7

    ubuntu18.04系统默认装了三个版本的python,如下: ubuntu18.04安装python3.7并将python3指向python3.7 参考此篇文章:https://blog.csdn ...

  7. ubuntu18.04 安装qt5.12.8及环境配置的详细教程

    这篇文章主要介绍了ubuntu18.04 安装qt5.12.8及环境配置的教程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下 环境 系统: ...

  8. linux下anaconda3安装教程,Ubuntu18.04 安装 Anaconda3的教程详解

    Anaconda指的是一个开源的Python发行版本,其包含了conda.Python等180多个科学包及其依赖项. 因为包含了大量的科学包,Anaconda 的下载文件比较大(约 531 MB),如 ...

  9. Ubuntu18.04安装cuDNN和Tensorflow的正确姿势

    文章目录 Ubuntu18.04安装cuDNN和Tensorflow的正确姿势 一.检查NVIDIA驱动是否安装成功 二.检查CUDA是否安装成功 三.安装cuDNN 1. 确定版本 2. 下载安装包 ...

最新文章

  1. 写出下列的中文意思 HTML,计算机网络技术考试试题纸B卷
  2. go语言 echo框架_如何在Go Echo Web框架中设置嵌套HTML模板
  3. linux barrier,如何决定何时启用Linux文件系统barrier功能?
  4. 使用Moq、NUnit和Shoulded进行单元测试
  5. BZOJ2160 拉拉队排练【Manacher】
  6. 如果查找SCI期刊属于哪个中科院分区
  7. 2的6次方怎么用计算机,2的6次方是多少(进制转换计算器)
  8. 云队友丨张朝阳不再狂妄,搜狐的艰难复苏路
  9. Qt音视频开发45-视频传输TCP版
  10. [6月7日的脚本] 在Windows 8中将桌面版IE设置为默认浏览器 (VBScript)
  11. codeforces每日5题(均1500)-第十七天
  12. 【NLP】情绪分析与酒店评论
  13. C# 关于压缩、加密、解压问题
  14. android studio 配置+安装
  15. 模拟koa洋葱模型实现
  16. 中大南方学院计算机温澍潜,中大南方学院“男神”老师刷爆学生朋友圈
  17. 不走寻常路的Logitech G120
  18. 学计算机要重修一门语言嘛,大学挂科率“最高”的4门科目,不仅要补考还要重修,你中招了吗...
  19. 微信开发必备工具 php和java开发语言
  20. 解决PyQt: RuntimeError: wrapped C/C++ object has been deleted和has no attribute of flush in python

热门文章

  1. 图论最短路径算法 Java实现
  2. java找到指定接口的实现类
  3. POE技术原理及硬件实现
  4. IIS 绑定多个证书错误解决方法
  5. Java利用hutool的API获得一个时间段内的每一天日期
  6. 普通发票模板 html css写的
  7. 用计算机计算有什么特点,计算机的主要特点有哪些
  8. 25-windows10中PS/2键盘失灵导致按键无效、键盘按下一次出现多次----解决(排除硬件问题、鼠标可使用的情况)
  9. 大班音乐机器人反思_幼儿园大班音乐教案《机器人》及教学反思
  10. Mybatis中单双引号引发的惨案