一、安装mariaDB
1、更换国内yum源
[root@template] mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
[root@template]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
2、安装mariadb
[root@template yum.repos.d]# yum install mariadb mariadb-server -y
3、启动mariadb服务程序并添加到开机启动项中
[root@template yum.repos.d]# systemctl start mariadb
[root@template yum.repos.d]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
4、为了保证数据库的安全性,一定要进行初始化工作
第1步:设定root用户密码。
第2步:删除匿名帐号。
第3步:禁止root用户从远程登录。
第4步:删除test数据库并取消对其的访问权限。
第5步:刷新授权表,让初始化后的设定立即生效。
5、初始化数据库服务
[root@template ~]# mysql_secure_installation
/usr/bin/mysql_secure_installation: line 379: find_mysql_client: command not found
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] y
New password: 输入要为root用户设置的数据库密码。
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.
Disallow root login remotely? [Y/n] y(禁止root用户从远程登录)
... Success!
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@linuxprobe ~]# firewall-cmd --permanent --add-service=mysql
success
[root@linuxprobe ~]# firewall-cmd --reload
success
使用root用户登录到数据库中:
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
MariaDB [mysql]> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| host |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| servers |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
24 rows in set (0.00 sec)
MariaDB [mysql]> select host,user from user;
+-----------+------+
| host | user |
+-----------+------+
| 127.0.0.1 | root |
| ::1 | root |
| localhost | root |
+-----------+------+
3 rows in set (0.00 sec)
MariaDB [mysql]> drop user 'root'@'::1';
Query OK, 0 rows affected (0.00 sec)
MariaDB [mysql]> select host,user from user;
+-----------+--------+
| host | user |
+-----------+--------+
| 127.0.0.1 | root |
| localhost | root |
+-----------+--------+
3 rows in set (0.00 sec)
6、修改当前用户在数据库中的密码(示例中的密码为nulige)
MariaDB [(none)]> set password = password('nulige'); Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> exit Bye
二、创建用户并授权
数据库GRANT命令的授权操作常用方案:
命令 | 作用 |
GRANT 权限 ON 数据库.表单名称 TO 用户名@主机名 | 对某个特定数据库中的特定表单给予授权。 |
GRANT 权限 ON 数据库.* TO 用户名@主机名 | 对某个特定数据库中的所有表单给予授权。 |
GRANT 权限 ON *.* TO 用户名@主机名 | 对所有数据库及所有表单给予授权。 |
GRANT 权限1,权限2 ON 数据库.* TO 用户名@主机名 | 对某个数据库中的所有表单给予多个授权。 |
GRANT ALL PRIVILEGES ON *.* TO 用户名@主机名 | 对所有数据库及所有表单给予全部授权,(谨慎操作)。 |
1、创建一个新的数据库用户:
创建数据库用户的命令:CREATE USER 用户名@主机名 IDENTIFIED BY '密码';
MariaDB [mysql]> create user nulige@localhost IDENTIFIED BY 'oldboy123'; Query OK, 0 rows affected (0.00 sec)
#给用户授权
MariaDB [(none)]> GRANT SELECT,UPDATE,DELETE,INSERT on mysql.user to nulige@localhost;
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> show grants for nulige@localhost;
+---------------------------------------------------------------------------------------------------------------+
| Grants for nulige@localhost |
+---------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'nulige'@'localhost' IDENTIFIED BY PASSWORD '*FE28814B4A8B3309DAC6ED7D3237ADED6DA1E515' |
| GRANT SELECT, INSERT, UPDATE, DELETE ON `mysql`.`user` TO 'nulige'@'localhost' |
+---------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
三、创建数据库与表单
常用的数据库表单管理命令有:
用法 | 作用 |
CREATE database 数据库名称。 | 创建新的数据库。 |
DESCRIBE 表单名称; | 描述表单。 |
UPDATE 表单名称 SET attribute=新值 WHERE attribute > 原始值; | 更新表单中的数据。 |
USE 数据库名称; | 指定使用的数据库。 |
SHOW databases; | 显示当前已有的数据库。 |
SHOW tables; | 显示当前数据库中的表单。 |
SELECT * FROM 表单名称; | 从表单中选中某个记录值。 |
DELETE FROM 表单名 WHERE attribute=值; | 从表单中删除某个记录值。 |
#创建数据库
MariaDB [(none)]> create database crm character set=utf8;
Query OK, 1 row affected (0.00 sec)
#查看数据库
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| crm |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)
#进入crm
MariaDB [(none)]> use crm
Database changed
#创建表,表名book
MariaDB [crm]> create table book (name char(20),price int,pages int);
Query OK, 0 rows affected (0.01 sec)
MariaDB [crm]> desc book;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| name | char(20) | YES | | NULL | |
| price | int(11) | YES | | NULL | |
| pages | int(11) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
#插入数据
MariaDB [crm]> INSERT INTO book(name,price,pages) VALUES('python','60',518);
Query OK, 1 row affected (0.00 sec)
MariaDB [crm]> INSERT INTO book(name,price,pages) VALUES('linux','48',618);
Query OK, 1 row affected (0.00 sec)
MariaDB [crm]> INSERT INTO book(name,price,pages) VALUES('go','78',818);
Query OK, 1 row affected (0.00 sec)
#更新数据
MariaDB [crm]> update book set price=55;
Query OK, 2 rows affected (0.01 sec)
Rows matched: 2 Changed: 2 Warnings: 0
#查看表中内容
MariaDB [crm]> select * from book;
+--------+-------+-------+
| name | price | pages |
+--------+-------+-------+
| python | 60 | 518 |
| linux | 48 | 618 |
| go | 78 | 818 |
+--------+-------+-------+
3 rows in set (0.00 sec)
#清空book表中内容
MariaDB [crm]> delete from book;
Query OK, 2 rows affected (0.00 sec)
#查看删除结果
MariaDB [crm]> select * from book;
Empty set (0.00 sec)
#删除数据库
MariaDB [(none)]> drop database crm;
Query OK, 0 rows affected (0.00 sec)
where命令用于在数据库匹配查询的条件,可用的条件有:
参数 | 作用 |
= | 相等。 |
<>或!= | 不相等。 |
> | 大于。 |
< | 小于。 |
>= | 大于或等于。 |
<= | 小于或等于。 |
BETWEEN | 在某个范围内。 |
LIKE | 搜索一个例子。 |
IN | 在列中搜索多个值。 |
查看价格大于75元的书籍:
MariaDB [crm]> select * from book where price>75;
+------+-------+-------+
| name | price | pages |
+------+-------+-------+
| go | 78 | 818 |
+------+-------+-------+
1 row in set (0.00 sec)
搜索价格不等于60元的书籍:
MariaDB [crm]> select * from book where price!=60;
+-------+-------+-------+
| name | price | pages |
+-------+-------+-------+
| linux | 48 | 618 |
| go | 78 | 818 |
+-------+-------+-------+
2 rows in set (0.01 sec)
四、数据库的备份与恢复
mysqldump命令用于备份数据库数据,格式为:“mysqldump [参数] [数据库名称]”。
参数 | 作用 |
-u | 数据库的用户名称。 |
-p | 密码提示符。 |
--no-data | 指备份数据库的描述结构,而不要数据。 |
--lock-all-tables |
备份完成后将不再允许修改数据。 |
1、备份前,先查看数据库名
#查看数据库
MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | crm | | mysql | | performance_schema | +--------------------+ 4 rows in set (0.00 sec)
#进入crm库 MariaDB [(none)]> use crm 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 [crm]> show tables; +---------------+ | Tables_in_crm | +---------------+ | book | +---------------+ 1 row in set (0.01 sec)
#查看表中数据
MariaDB [crm]> select * from book;
+--------+-------+-------+
| name | price | pages |
+--------+-------+-------+
| python | 60 | 518 |
| linux | 48 | 618 |
| go | 78 | 818 |
+--------+-------+-------+
3 rows in set (0.00 sec)
2、备份数据库
格式为:“mysqldump [参数] [数据库名称]” ,后面接保存地址
[root@template yum.repos.d]# mysqldump -u root -p crm > /root/crm.dump
Enter password:
[root@template yum.repos.d]# cd /root/
[root@template ~]# ll
-rw-r--r--. 1 root root 1900 Apr 16 02:48 crm.dump
导入备份的数据
#导入备份的数据 [root@template ~]# mysql -u root -p crm < /root/crm.dump Enter password: #登录数据库 [root@template ~]# mysql -u root -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or g. Your MariaDB connection id is 25 Server version: 5.5.52-MariaDB MariaDB Server Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others. Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. #进入crm库 MariaDB [(none)]> use crm 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 [crm]> show tables; +---------------+ | Tables_in_crm | +---------------+ | book | +---------------+ 1 row in set (0.00 sec) #查看表结构 MariaDB [crm]> desc book; +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | name | char(20) | YES | | NULL | | | price | int(11) | YES | | NULL | | | pages | int(11) | YES | | NULL | | +-------+----------+------+-----+---------+-------+ 3 rows in set (0.00 sec) #查看表中数据 MariaDB [crm]> select * from book; +--------+-------+-------+ | name | price | pages | +--------+-------+-------+ | python | 60 | 518 | | linux | 48 | 618 | | go | 78 | 818 | +--------+-------+-------+ 3 rows in set (0.00 sec)