1 认识MariaDB数据库系统

MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可 MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品。

2 在CentOS 7X上安装MariaDB

执行如下命令进行mariadb的安装

  1. [root@dbserver ~]# yum -y install mariadb-server
  2. [root@dbserver ~]# systemctl start mariadb
  3. [root@dbserver ~]# systemctl enable mariadb
  4. [root@dbserver ~]# systemctl stop firewalld
  5. [root@dbserver ~]# systemctl disable firewalld
  6. [root@dbserver ~]# setenforce 0

3 对MariaDB进行初始配置

运行mysql_secure_installation脚本程序,对mariadb数据库系统进行初始化,过程如下:

  1. [root@dbserver ~]# mysql_secure_installation
  2. NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
  3. SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
  4. In order to log into MariaDB to secure it, we'll need the current
  5. password for the root user. If you've just installed MariaDB, and
  6. you haven't set the root password yet, the password will be blank,
  7. so you should just press enter here.
  8. Enter current password for root (enter for none): [回车]
  9. #输入当前的root账户的密码(回车表示密码为空):
  10. ......
  11. Set root password? [Y/n] y
  12. #是否设置root的密码,Y表示要设置,n表示不设置
  13. New password: [输入新的密码,然后回车]
  14. Re-enter new password: [再次输入密码,然后回车]
  15. ......
  16. Remove anonymous users? [Y/n] y
  17. #是否移除匿名用户
  18. ......
  19. Disallow root login remotely? [Y/n] y
  20. #是否禁止root账户从远程登录数据库服务器
  21. .....
  22. Remove test database and access to it? [Y/n] y
  23. #是否移除test测试数据库
  24. ......
  25. Reload privilege tables now? [Y/n] y
  26. #重新加载权限列表(privilege tables)

4 使用mysql 命令行客户端连接Mariadb

安装好mariadb服务器程序后,可以通过mysql命令连接到服务器,可以使用这个命令行客户端执行相应的SQL语句。

  1. [root@dbserver ~]# mysql -u root -p -h localhost
  2. Enter password:
  3. Welcome to the MariaDB monitor. Commands end with ; or \g.
  4. Your MariaDB connection id is 10
  5. Server version: 5.5.68-MariaDB MariaDB Server
  6. Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
  7. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  8. MariaDB [(none)]> show databases;
  9. +--------------------+
  10. | Database |
  11. +--------------------+
  12. | information_schema |
  13. | mysql |
  14. | performance_schema |
  15. +--------------------+
  16. 3 rows in set (0.00 sec)
  17. MariaDB [(none)]> use mysql
  18. Reading table information for completion of table and column names
  19. You can turn off this feature to get a quicker startup with -A
  20. Database changed

5 创建数据库服务器用户账户

创建用户账户的命令:create user
例如:

  1. MariaDB [mysql]> create user visitor@'%' identified by "123567";
  2. Query OK, 0 rows affected (0.00 sec)
  3. #说明:
  4. #(1)visitor@'%' 代表用户名和登录主机,visitor是登录用户名,@'%'表示用户可以从任意网络主机中登录数据库服务器
  5. #(2)identified by 后面的字符串就是用户的登录密码
  6. #(3)新建的用户账户会被保存到mysql数据库的user表中
  7. MariaDB [mysql]> select user,host from user;
  8. +---------+-----------+
  9. | user | host |
  10. +---------+-----------+
  11. | visitor | % |
  12. | root | 127.0.0.1 |
  13. | root | ::1 |
  14. | root | localhost |
  15. +---------+-----------+
  16. 4 rows in set (0.00 sec)

6 使用NAVCAT等图形界面的客户端远程登录数据库服务器

登录前需要掌握如下参数:
(1)服务器IP地址或者域名
(2)数据库服务器程序的端口
(3)用户名和登录密码
image.png

7 建立employees样例数据库

employees数据库是mysql官方提供的一个用于学习和测试的样例数据库
image.png

1 获取employees样例数据库的源代码
2 将employees样例数据库的源代码复制到数据库服务器上
3 将employees样例数据库源代码进行解压

  1. [root@dbserver ~]# pwd
  2. /root
  3. #把employees_db.tar.gz的存放目录作为当前目录
  4. [root@dbserver ~]# ls
  5. anaconda-ks.cfg employees_db.tar.gz env3.6.10 env3.8.3 perl5
  6. [root@dbserver ~]# tar -xzvf employees_db.tar.gz
  7. #在当前目录下对employees.tar.gz文件进行解压,解压后会在当前目录下生成employees_db目录

4 进行employees数据库的导入操作

  1. [root@dbserver ~]# pwd
  2. /root
  3. [root@dbserver ~]# ls
  4. anaconda-ks.cfg employees_db employees_db.tar.gz env3.6.10 env3.8.3 perl5
  5. [root@dbserver ~]# cd employees_db
  6. #进入employees_db目录,将它作为当前目录
  7. [root@dbserver employees_db]# pwd
  8. /root/employees_db
  9. [root@dbserver employees_db]# ls
  10. Changelog employees.sql load_employees.dump README
  11. employees_partitioned2.sql load_departments.dump load_salaries.dump test_employees_md5.sql
  12. employees_partitioned3.sql load_dept_emp.dump load_titles.dump test_employees_sha.sql
  13. employees_partitioned.sql load_dept_manager.dump objects.sql
  14. [root@dbserver employees_db]# mysql -u root -p
  15. #在当前目录中打开mysql客户端
  16. Enter password:
  17. Welcome to the MariaDB monitor. Commands end with ; or \g.
  18. Your MariaDB connection id is 16
  19. Server version: 5.5.68-MariaDB MariaDB Server
  20. Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
  21. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  22. MariaDB [(none)]> source employees.sql
  23. #执行employees数据库的导入操作
  24. MariaDB [employees]> show databases;
  25. +--------------------+
  26. | Database |
  27. +--------------------+
  28. | information_schema |
  29. | employees |
  30. | mysql |
  31. | performance_schema |
  32. +--------------------+
  33. 4 rows in set (0.00 sec)
  34. #使用show databases命令可以看到employees数据库已经导入系统

8 授权用户访问数据库

可以在MariaDB中使用grant命令,给用户访问数据库授权

  1. MariaDB [employees]> grant select,insert,update,delete on employees.* to visitor@'%';
  2. Query OK, 0 rows affected (0.00 sec)
  3. #说明:
  4. #(1)select,insert,update,delete 代表 增删改查的权限
  5. #(2)on employees.* 代表 employees数据库的所有表
  6. #(3)to visistor@'%' 代表 权限授予 visistor@'%'
  7. MariaDB [mysql]> show grants for visitor@'%';
  8. #使用show grants命令查看用户授权的情况