1. 程式人生 > 其它 >MariaDB 安裝和配置

MariaDB 安裝和配置

一、MariaDB資料庫管理系統是MySQL的一個分支,主要由開源社群在維護,採用GPL授權許可。

  1、關閉selinux

    ①修改selinux的配置檔案

      [root@localhost ~]#  vim  /etc/selinux/config

        SELINUX=disabled

    ②關閉selinux

      [root@localhost ~]#  setenforce  0

  2、關閉防火牆

    [root@localhost ~]# systemctl stop firewalld
    [root@localhost ~]# systemctl enable firewalld

  3、手動安裝MariaDB源配置

[root@www yum.repos.d]# cd /etc/yum.repos.d/
[root@www yum.repos.d]# vim /etc/yum.repos.d/mariadb.repo

[mariadb]
name=MariaDB
baseurl=https://mirrors.ustc.edu.cn/mariadb/yum/10.4/centos7-amd64
gpgkey=https://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB
gpgchek=1

    [root@www yum.repos.d]# yum  clean  all  #清空本地yum快取

    [root@www yum.repos.d]# yum makecache   #建立本地yum快取

  4、MariaDB的安裝

    [root@www yum.repos.d]# yum  -y  install  MariaDB-server  MariaDB-client  #安裝MDB的伺服器和客戶端

    報錯為檔案衝突問題

Transaction check error:
file /usr/share/mysql/charsets/Index.xml from install of MariaDB-common-10.4.25-1.el7.centos.x86_64 conflicts with file from package mysql-community-common-5.7.38-1.el7.x86_64

解決方法

  rpm  -qa  |  grep  -i  mysql  #檢視

  yum  -y  remove  mysql-community-......  #刪除

  5、MariaDB的基本配置

    ①MariaDB 管理命令

[root@localhost yum.repos.d]# systemctl start mariadb  #啟動
[root@localhost yum.repos.d]# systemctl stop mariadb  #關閉
[root@localhost yum.repos.d]# systemctl restart mariadb  #重啟

      

    ②MariaDB 程序檢視 

[root@localhost yum.repos.d]# ps aux | grep mariadb  #檢視程序
  root 9264 0.0 0.0 112808 968 pts/0 R+ 02:52 0:00 grep --color=auto mariadb
[root@localhost yum.repos.d]# netstat -ntlp | grep 3306  #檢視埠3306
[root@localhost yum.repos.d]# lsof -i:3306    #檢視埠3306
  COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
  mysqld 9139 mysql 20u IPv6 622570 0t0 TCP *:mysql (LISTEN)

      

    ③MariaDB 資料庫初始化

為了確保資料庫的安全性和正常運轉,需要先對資料庫程式進行初始化操作。這個初始化操作涉及下面5個步驟。
  。設定root管理員在資料庫中的密碼值(注意,該密碼並非root管理員在系統中的密碼,這裡的密碼值預設應該為空,可直接按回車鍵).
  。設定root管理員在資料庫中的專有密碼。
  。隨後刪除匿名賬戶,並使用root管理員從遠端登入資料庫,以確保資料庫上執行的業。務的安全性。
  。刪除預設的測試資料庫,取消測試資料庫的一系列訪問許可權。o重新整理授權列表,讓初始化的設定立即生效。

        (1)初始化命令

[root@localhost yum.repos.d]# 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] y
Enabled successfully!
Reloading privilege tables..
... Success!


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

Change the root password? [Y/n] y    #是否設定root密碼,輸入y或者回車,並回答
New password:6a.Ajn3ihlOaF      #設定密碼,並確認密碼
Re-enter new password:6a.Ajn3ihlOaF
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.

Disallow root login remotely? [Y/n] 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] y  #是否刪除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] 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!
[root@localhost yum.repos.d]#

 

(2)初始化測試登入

[root@localhost yum.repos.d]# mysql  -uroot  -p'6a.Ajn3ihlOaF'    #通過root使用者登入MySQL資料庫

Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 17
Server version: 10.4.25-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)]>

MariaDB [(none)]> show databases;  #檢視資料庫
+--------------------------------+
| Database        |
+--------------------------------+
| information_schema    |
| mysql            |
| performance_schema  |
+---------------------------------+
3 rows in set (0.003 sec)

MariaDB [(none)]>

 

    ④MariaDB 設定utf8字符集

       [root@localhost yum.repos.d]# vim /etc/my.cnf

  6、MariaDB 的使用

    MariaDB 的使用和MySQL是語句完全是一樣的

    ①基本使用

    MariaDB [(none)]> set password =PASSWORD('6a.Ajn3ihlOaF');  #修改MySQL密碼

Query OK, 0 rows affected (0.003 sec)

MariaDB [(none)]> create  database  test  charset=utf8;  #建立一個test資料庫,並設定字符集為utf8
  Query OK, 1 row affected (0.001 sec)

MariaDB [(none)]> use test;  #進入test資料庫
  Database changed
  MariaDB [test]>

MariaDB [test]> create table mytest(id int,name char(32));  #建立mytest資料表
  Query OK, 0 rows affected (0.012 sec)

MariaDB [test]> show tables;  #查看錶

+--------------------+
| Tables_in_test |
+--------------------+
| mytest     |
+--------------------+
1 row in set (0.000 sec)

MariaDB [test]> desc mytest;  #查看錶結構

+-------+------------+--------+-------+---------+-----------+
| Field   | Type      | Null   | Key  | Default | Extra    |
+--------+------------+-------+-------+---------+-----------+
| id   | int(11)    | YES  |    | NULL |     |
| name  | char(32) | YES  |    | NULL |     |
+---------+-----------+--------+-------+---------+-----------+
2 rows in set (0.005 sec)

    ②簡單的增刪改查

MariaDB [test]> insert into mytest(id,name) values(1,"zero"),(2,"one");    #向mytest表中插入兩條資料
  Query OK, 2 rows affected (0.004 sec)
  Records: 2 Duplicates: 0 Warnings: 0

MariaDB [test]> select id,name from mytest;  #檢視id和name列資料
  +--------+------+
  | id  |name|
  +-------+-------+
  | 1  | zero  |
  | 2  | one   |
  +-------+-------+
  2 rows in set (0.001 sec)

MariaDB [test]> delete from mytest where id=2;  #刪除所有id為2的所有資料
  Query OK, 1 row affected (0.002 sec)

    ③關於使用者及許可權常用命令

      MariaDB [test]> create  user  llg@‘localhost’  identified  by  '6a.Ajn3ihlOaF';    #建立使用者和密碼

        Query OK, 0 rows affected (0.004 sec)

      # MySQL使用grant命令對賬號進行授權,grant命令常用格式如下:

      # grant  許可權  on  資料庫.表名  to  賬號@主機      對特定的資料庫中特定的表授權

      

      # grant  許可權  on  資料庫.*  to  賬號@主機        對特定的資料庫中所有表授權

      

      # grant  許可權1,許可權2,許可權3  on  資料庫.*  to  賬號@主機  對所有庫中的所有表給予多個許可權

      

      # grant  all  privileges  on  *.*  to  賬號@主機       對待所有資料庫中所有表授予所有許可權

      

      #drop  user  使用者名稱;       刪除使用者

      

      #flush   privileges;        重新整理許可權

      

    ④資料庫備份與恢復

      (1)備份

        mysqldump  -u  root  -p  --all-databases   >   /tem/db.dump    #備份所有資料庫命令

          Enter password:6a.Ajn3ihlOaF

        mysqldump  -u  root  -p  text   >   /tmp/test.sql    #備份單個數據庫命令

          Enter password:6a.Ajn3ihlOaF

      (2)恢復

        mysql  -uroot  -p  text2<   /tmp/test.sql    #將備份的資料庫匯入