1 认识MariaDB数据库系统
MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可 MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品。
2 在CentOS 7X上安装MariaDB
执行如下命令进行mariadb的安装
[root@dbserver ~]# yum -y install mariadb-server
[root@dbserver ~]# systemctl start mariadb
[root@dbserver ~]# systemctl enable mariadb
[root@dbserver ~]# systemctl stop firewalld
[root@dbserver ~]# systemctl disable firewalld
[root@dbserver ~]# setenforce 0
3 对MariaDB进行初始配置
运行mysql_secure_installation脚本程序,对mariadb数据库系统进行初始化,过程如下:
[root@dbserver ~]# 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): [回车]
#输入当前的root账户的密码(回车表示密码为空):
......
Set root password? [Y/n] y
#是否设置root的密码,Y表示要设置,n表示不设置
New password: [输入新的密码,然后回车]
Re-enter new password: [再次输入密码,然后回车]
......
Remove anonymous users? [Y/n] y
#是否移除匿名用户
......
Disallow root login remotely? [Y/n] y
#是否禁止root账户从远程登录数据库服务器
.....
Remove test database and access to it? [Y/n] y
#是否移除test测试数据库
......
Reload privilege tables now? [Y/n] y
#重新加载权限列表(privilege tables)
4 使用mysql 命令行客户端连接Mariadb
安装好mariadb服务器程序后,可以通过mysql命令连接到服务器,可以使用这个命令行客户端执行相应的SQL语句。
[root@dbserver ~]# mysql -u root -p -h localhost
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
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)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
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
5 创建数据库服务器用户账户
创建用户账户的命令:create user
例如:
MariaDB [mysql]> create user visitor@'%' identified by "123567";
Query OK, 0 rows affected (0.00 sec)
#说明:
#(1)visitor@'%' 代表用户名和登录主机,visitor是登录用户名,@'%'表示用户可以从任意网络主机中登录数据库服务器
#(2)identified by 后面的字符串就是用户的登录密码
#(3)新建的用户账户会被保存到mysql数据库的user表中
MariaDB [mysql]> select user,host from user;
+---------+-----------+
| user | host |
+---------+-----------+
| visitor | % |
| root | 127.0.0.1 |
| root | ::1 |
| root | localhost |
+---------+-----------+
4 rows in set (0.00 sec)
6 使用NAVCAT等图形界面的客户端远程登录数据库服务器
登录前需要掌握如下参数:
(1)服务器IP地址或者域名
(2)数据库服务器程序的端口
(3)用户名和登录密码
7 建立employees样例数据库
employees数据库是mysql官方提供的一个用于学习和测试的样例数据库
1 获取employees样例数据库的源代码
2 将employees样例数据库的源代码复制到数据库服务器上
3 将employees样例数据库源代码进行解压
[root@dbserver ~]# pwd
/root
#把employees_db.tar.gz的存放目录作为当前目录
[root@dbserver ~]# ls
anaconda-ks.cfg employees_db.tar.gz env3.6.10 env3.8.3 perl5
[root@dbserver ~]# tar -xzvf employees_db.tar.gz
#在当前目录下对employees.tar.gz文件进行解压,解压后会在当前目录下生成employees_db目录
4 进行employees数据库的导入操作
[root@dbserver ~]# pwd
/root
[root@dbserver ~]# ls
anaconda-ks.cfg employees_db employees_db.tar.gz env3.6.10 env3.8.3 perl5
[root@dbserver ~]# cd employees_db
#进入employees_db目录,将它作为当前目录
[root@dbserver employees_db]# pwd
/root/employees_db
[root@dbserver employees_db]# ls
Changelog employees.sql load_employees.dump README
employees_partitioned2.sql load_departments.dump load_salaries.dump test_employees_md5.sql
employees_partitioned3.sql load_dept_emp.dump load_titles.dump test_employees_sha.sql
employees_partitioned.sql load_dept_manager.dump objects.sql
[root@dbserver employees_db]# mysql -u root -p
#在当前目录中打开mysql客户端
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 16
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)]> source employees.sql
#执行employees数据库的导入操作
MariaDB [employees]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| employees |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)
#使用show databases命令可以看到employees数据库已经导入系统
8 授权用户访问数据库
可以在MariaDB中使用grant命令,给用户访问数据库授权
MariaDB [employees]> grant select,insert,update,delete on employees.* to visitor@'%';
Query OK, 0 rows affected (0.00 sec)
#说明:
#(1)select,insert,update,delete 代表 增删改查的权限
#(2)on employees.* 代表 employees数据库的所有表
#(3)to visistor@'%' 代表 权限授予 visistor@'%'
MariaDB [mysql]> show grants for visitor@'%';
#使用show grants命令查看用户授权的情况