数据库基础概念
数据库(Database, DB)是按照数据结构来组织、存储和管理数据的建立在计算机存储设备上的仓库。
DBMS: Database Management System, 数据库管理系统。我们平时所说的数据库其实指DBMS.
RDBMS: Relation Database Management System, 关系型数据库管理系统,其主要的概念:
1. 关系:二维表,表中的行、列次序不重要;
2. 元祖:表中的每一行,又称为一条记录
3. 属性:表中的每一列,又称为字段
4. 域:属性的取值范围
5. 主键:用于唯一确定一行的一个或多个字段
事务transaction:用户定义个一组不可分割的数据库操作序列。事务满足ACID原则:
- A: Atomicity, 原子性。事务是一个整体,这组操作要么都没做要么都做完。如果事务失败,事务中已执行完成的操作会回滚(roolback);
- C: Consistency, 一致性。与事务相关的所有数据库状态在事务提交后有一个期望值,事务执行完后所有的值都应该满足期望;如果部分满足部分不满足,说明事务是失败的,要回滚;
- I: Isolation, 隔离性。事务执行过程中,处于中间状态的数据应该对其他事务不可见;
- D: Duration, 持久性。事务一旦被提交对数据库的修改是永久性的。
MariaDB(CentOS6.9)
MariaDB是一种RDBMS,和MySQL一脉相承。
yum安装
yum源配置
yum源可以在MariaDB相关网页获取,根据自己的系统版本选择:
安装
yum install mariadb-server --enablerepo=MariaDB
# --enablerepo=MariaDB, MariaDB为使用的yum源名称
# 我是用的操作系统版本是centOS6.9,在CentOS7中还需要安装mariadb
配置
安全配置
不做安全配置,任意用户都可以用root账户登录mysql,并且不需要输入密码,这是不安全的。
# 输入当前root用户密码,刚安装完密码为空,直接回车
Enter current password for root (enter for none):
# 设置新的root账号密码
Set root password? [Y/n] y
New password:
Re-enter new password:
# MariaDB安装默认会设置一个匿名账户允许任何人登录,建议删除
Remove anonymous users? [Y/n] y
# 默认可以从远程主机使用root账户登录,建议将root账户的使用限定在本机,以确保root账户密码通过网络被获取
Disallow root login remotely? [Y/n] y
# 是否删除测试用的数据库'test',生产环境建议删除
Remove test database and access to it? [Y/n] y
# 重新加载授权表,使之前的修改生效
Reload privilege tables now? [Y/n] y
字符集配置
为了避免中文乱码的问题建议对MariaDB的,默认字符集进行设置。修改配置文件/etc/my.cnf.d/server.cnf
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
character-set-server=utf8
init_connect='SET collation_connection=utf8-unicode_ci'
init_connect='SET NAMES utf8'
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
其他配置
[mysqld]
datadir = /app/data # 数据文件存储位置,默认存在mysql账户的家目录
innodb_file_per_table = on # 为每个表格创建新文件
skip_name_resolve = on # 禁止主机名解析
服务
启动
$ service mysql start
# 查看服务是否成功启动,默认监听端口3306
$ ss -nlt |grep 3306
LISTEN 0 50 *:3306 *:*
# 查看进程
$ ps aux |grep mysql
root 13041 0.0 0.1 11488 1472 pts/1 S 05:31 0:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --pid-file=/var/lib/mysql/centos6.9-python.pid
mysql 13225 0.0 10.0 732340 101216 pts/1 Sl 05:31 0:03 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/lib/mysql/centos6.9-python.err --pid-file=/var/lib/mysql/centos6.9-python.pid --wsrep_start_position=00000000-0000-0000-0000-000000000000:-1
root 13267 0.0 0.3 225688 3064 pts/0 S+ 05:32 0:00 mysql
root 13646 0.0 0.0 103332 844 pts/1 S+ 06:54 0:00 grep mysql
命令行交互式客户端程序mysql
交换命令行的输入对大小写不敏感。
登录 mysql -uUserName -p
登录选项:-uUSERNAME: 用户名 ,默认为root
-hHOST: 服务器主机, 默认为localhost
-pPASSWORD:用户的密码,默认为空密码
当前状态 status
可以查看当前用户、字符集等信息。
MariaDB [mysql]> status
--------------
mysql Ver 15.1 Distrib 5.5.58-MariaDB, for Linux (x86_64) using readline 5.1
Connection id: 2
Current database: mysql
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server: MariaDB
Server version: 5.5.58-MariaDB-wsrep MariaDB Server, wsrep_25.22.r9949137
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: utf8
Db characterset: utf8
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /var/lib/mysql/mysql.sock
Uptime: 2 hours 3 min 21 sec
Threads: 1 Questions: 78 Slow queries: 0 Opens: 17 Flush tables: 2 Open tables: 41 Queries per second avg: 0.010
--------------
查看用户
MariaDB [mysql]> show databases; # 查看MariaDB中管理的所有数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
MariaDB [test]> use mysql # 选择要使用的数据库
Database changed
MariaDB [mysql]> select user(); # 查看当前用户
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
MariaDB [mysql]> select host, user, password from user; # 查看当前系统的所有账户
+-----------+------+-------------------------------------------+
| host | user | password |
+-----------+------+-------------------------------------------+
| localhost | root | *E74858DB86EBA20BC33D0AECAE8A8108C56B17FA |
| 127.0.0.1 | root | *E74858DB86EBA20BC33D0AECAE8A8108C56B17FA |
| ::1 | root | *E74858DB86EBA20BC33D0AECAE8A8108C56B17FA |
+-----------+------+-------------------------------------------+
3 rows in set (0.00 sec)
MariaDB [mysql]> desc user; # 查看user表结构
+------------------------+-----------------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+-----------------------------------+------+-----+---------+-------+
| Host | char(60) | NO | PRI | | |
| User | char(16) | NO | PRI | | |
| Password | char(41) | NO | | | |
user表的主键包含两个字段,‘host’和‘user’,表明mysql用户账号由这两个字段唯一确定
用户授权
grant PRIVS on TABLES to USERNAME@'HOSTS' identified by 'PASSWD'; # 大写的部分是自定义的
- PRIV: 限定用户权限;all, select, insert, update, delete, create, drop, index, alter, grant, references, reload, shutdown, process, file其中一部分
- TABLES: 用户对那些表有权限
- *.* 所有库的所表
- db_name.*指定库的所有表
- db_name.tb_name: 指定库的指定表
-
HOSTS: 限定用户可以通过那些远程主机连接mysql,支持通配符
-
% 匹配任意长度的任意字符 172.16.0.0/16 等价于 172.16.%.%
-
_ 匹配任意单个字符
MariaDB [mysql]> grant all on *.* to python@'192.168.129.0/24' identified by '123456';
Query OK, 0 rows affected (0.01 sec)
MariaDB [mysql]> select host, user, password from user;
+------------------+--------+-------------------------------------------+
| host | user | password |
+------------------+--------+-------------------------------------------+
| localhost | root | *E74858DB86EBA20BC33D0AECAE8A8108C56B17FA |
| 127.0.0.1 | root | *E74858DB86EBA20BC33D0AECAE8A8108C56B17FA |
| ::1 | root | *E74858DB86EBA20BC33D0AECAE8A8108C56B17FA |
| 192.168.129.0/24 | python | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+------------------+--------+-------------------------------------------+
4 rows in set (0.00 sec)
查看用户权限 SHOW GRANTS FOR 'user'@'host';
可以查看帮助:help show grants
示例:
MariaDB [mysql]> show grants; #显示当前用户权限
+----------------------------------------------------------------------------------------------------------------------------------------+
| Grants for root@localhost |
+----------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*E74858DB86EBA20BC33D0AECAE8A8108C56B17FA' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
+----------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
回收权限 REVOKE priv_type, ... ON db_name.tb_name FROM 'user'@'host
MariaDB服务进程启动时会读取mysql库中所有授权表至内存
1. GRANT或REVOKE等执行权限操作会保存于系统表中,MariaDB的服务进程通常会自动重读授权表,使之生效
2. 对于不能够或不能及时重读授权表的命令,可手动让MariaDB的服务进程重读授权表,在交换命令行中执行:FLUSH PRIVILEGES
;
服务器参数
查看变量: show [global|session] variables [like Pattern]
修改变量:set variable_assignment
variable_assignment:
user_var_name = expr
| [GLOBAL | SESSION] system_var_name = expr
| [@@global. | @@session. | @@]system_var_name = expr
示例:
MariaDB [mysql]> show variables like '%slow%';
+---------------------+--------------------------------------------------------------------------------------------------------------+
| Variable_name | Value |
+---------------------+--------------------------------------------------------------------------------------------------------------+
| log_slow_filter | admin,filesort,filesort_on_disk,full_join,full_scan,query_cache,query_cache_miss,tmp_table,tmp_table_on_disk |
| log_slow_queries | OFF |
| log_slow_rate_limit | 1 |
| log_slow_verbosity | |
| slow_launch_time | 2 |
| slow_query_log | OFF |
| slow_query_log_file | centos6-slow.log |
+---------------------+--------------------------------------------------------------------------------------------------------------+
7 rows in set (0.00 sec)
MariaDB [mysql]> set global slow_query_log=on;
Query OK, 0 rows affected (0.00 sec)
MariaDB [mysql]> show variables like 'slow_query_log';
+----------------+-------+
| Variable_name | Value |
+----------------+-------+
| slow_query_log | ON |
+----------------+-------+
1 row in set (0.00 sec)
参考
[1] https://www.cnblogs.com/the-one/p/5554976.html 字符集配置