×

抖音网红

mariadb允许远程连接权限(数据库远程连接设置方法)

douyinfenxiang douyinfenxiang 发表于2023-11-16 19:44:24 浏览17 评论0

抢沙发发表评论

今天我们来记录下centos7安装mariadb并配置root远程访问的整体流程。

一、安装mariadb

我的centos版本是7.3,这里使用yum来安装mariadb,默认版本是5.5.68,这个确实有些低了,截止今天我看到的正式版本已经是10.7.3了

yum install mariadb-server

这里没什么好说的,安装完毕后先启动服务

systemctl start mariadb  #启动mariadb
systemctl enable mariadb  #配置服务自启动

首次安装需要执行一些安全配置,如下:

[root@localhost1 ~]# 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
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.

Set root password? [Y/n]   #这里是设置root密码,选择Y
New password:                  #输入要设置的密码
Re-enter new password:    #重复设置的密码

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]   #是否禁用root用户远程登录,这里选择n,不过选啥root都不能远程登录,需要额外设置
 ... skipping.

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]  #是否删除test库,选择n
 - 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...

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

Thanks for using MariaDB!

登录mysql

[root@localhost1 ~]# mysql -u root -p
Enter password:                #输入刚才配置的密码
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 5.5.68-MariaDB MariaDB Server

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)]>

OK,到这里mariadb已经安装完毕了,接下来配置远程访问权限配置

二、远程访问权限配置

这里就和网上资料的不太一样了,很多文档说需要修改配置文件,像绑定IP,默认是127.0.0.1,需要修改成服务器可通信IP,否则无法远程访问,但这个版本确实是不需要修改,直接执行赋权命令即可。

MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION; 
MariaDB [(none)]> flush privileges;   #执行使配置生效

执行以上命令使root能够远程登录,第一个引号中是用户名,第二个引号是密码 ,%代表所有IP,如果只需要允许特定IP地址,这里替换一下就行。另外再来说下grant,这个是赋权命令。ALL PRIVILEGES代表所有权限,可以替换为具体的操作类型,如select、insert、update等操作,*.*中的第一个星代表数据库名,因此可以这样赋权:

GRANT SELECT, INSERT, UPDATE, DELETE ON TESTDB.* TO 'root'@'192.168.1.100' IDENTIFIED BY '123456' WITH GRANT OPTION;

接下我们navicat测试下

测试结果

OK,测试成功!