debian,ubuntu下安装MariaDB,并设置密码,修改端口,允许外网访问

文章目录

    • 安装MariaDB
    • 设置密码
    • 修改端口
    • 允许远程访问

安装MariaDB

MariaDB存在apt 的更新中,如果追求最新版,可以下载二进制文件进行编译安装,但普通人用没必要那么麻烦,直接装就行了。

apt update && apt install mariadb-server mariadb-client

查看数据库情况:

-> # systemctl status mariadb
● mariadb.service - MariaDB 10.1.38 database server
   Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2019-03-29 10:03:39 CST; 1min 13s ago
     Docs: man:mysqld(8)
           https://mariadb.com/kb/en/library/systemd/
 Main PID: 30065 (mysqld)
   Status: "Taking your SQL requests now..."
    Tasks: 27 (limit: 1055)
   CGroup: /system.slice/mariadb.service
           └─30065 /usr/sbin/mysqld

Mar 29 10:03:43 ubuntu /etc/mysql/debian-start[30112]: Processing databases
Mar 29 10:03:43 ubuntu /etc/mysql/debian-start[30112]: information_schema
Mar 29 10:03:43 ubuntu /etc/mysql/debian-start[30112]: mysql
Mar 29 10:03:43 ubuntu /etc/mysql/debian-start[30112]: performance_schema
Mar 29 10:03:43 ubuntu /etc/mysql/debian-start[30112]: Phase 6/7: Checking and upgrading tables
Mar 29 10:03:43 ubuntu /etc/mysql/debian-start[30112]: Processing databases
Mar 29 10:03:43 ubuntu /etc/mysql/debian-start[30112]: information_schema
Mar 29 10:03:43 ubuntu /etc/mysql/debian-start[30112]: performance_schema
Mar 29 10:03:43 ubuntu /etc/mysql/debian-start[30112]: Phase 7/7: Running 'FLUSH PRIVILEGES'
Mar 29 10:03:43 ubuntu /etc/mysql/debian-start[30112]: OK

设置密码

运行如下命名进行密码设置:

mysql_secure_installation

示例如下:

-> # 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.
#设置root密码
Set 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用户原创登录
Disallow root login remotely? [Y/n] n
 ... 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] 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...

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

Thanks for using MariaDB!

进行连接测试:

mysql -u root -p

输入密码后连接即可成功

修改端口

nano /etc/mysql/mariadb.conf.d/50-server.cnf 

修改如下:

[mysqld]

#
# * Basic Settings
#
user            = mysql
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
port            = 51009
#修改端口为一个随机端口号
basedir         = /usr
datadir         = /var/lib/mysql
tmpdir          = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking

# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
bind-address            = 0.0.0.0
#bind-address改为0.0.0.0,从而可以外网访问

注意:MariaDB 在Debian 上的默认字符集是utf8mb4,可以直接使用中文


#
# * Character sets
#
# MySQL/MariaDB default is Latin1, but in Debian we rather default to the full
# utf8 4-byte character set. See also client.cnf
#
character-set-server  = utf8mb4
collation-server      = utf8mb4_general_ci

重启mariadb:

systemctl restart mysql

进行测试:

mysql -h localhost -P 51009 -u root -p

允许远程访问

连接mariadb,添加root的远程访问权限

-> # mysql -h localhost -P 51009 -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 30
Server version: 10.1.38-MariaDB-0ubuntu0.18.04.1 Ubuntu 18.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)]> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [mysql]> select user,host,password from user;
+------+-----------+-------------------------------------------+
| user | host      | password                                  |
+------+-----------+-------------------------------------------+
| root | localhost | *1945EC0A6D14A304922B91B7F14585A0B75D12 |
+------+-----------+-------------------------------------------+
1 row in set (0.02 sec)
MariaDB [mysql]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'etrvrvsfse56uh' WITH GRANT OPTION;
Query OK, 0 rows affected (0.23 sec)

MariaDB [mysql]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)

MariaDB [mysql]> select user,host,password from user;
+------+-----------+-------------------------------------------+
| user | host      | password                                  |
+------+-----------+-------------------------------------------+
| root | localhost | *194500000000ECA6D000000000000585AB7E5D12 |
| root | %         | *2470C0C06DEE41618BB00000000000000EC9DE19 |
+------+-----------+-------------------------------------------+
2 rows in set (0.04 sec)

然后就可以用授权的密码远程访问了。

你可能感兴趣的:(sql)