MariaDB数据库管理系统是MYSQL的一个分支,主要由开源社区在维护,采用GPL授权许可,mariad的目的是完全兼容MYSQL,包括API和命令行,使之成为MYSQL的替代品。在存储引擎方面使用XtraDB来代替MYSQL的InnoDB。
连接池:认证、线程重用、连接数限制、内存检查、缓存
SQL接口:DDL, DML, 关系型数据库的基本抽象
parser: 查询转换、对象权限检查
优化器:访问路径,性能相关的统计数据
caches和buffers:与存储引擎自身相关的I/O性能提升工具
存储引擎:MyISAM、InnoDB(变种:XtraDB)、Memory、Merge、Federated、
CSV、Archive、Blackholl、Aria、SphinxSE、TokuDB
MariaDB基础
1.数据库操作
创建数据库
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
删除数据库
DROP {DATABASE | SCHEMA} [IF EXISTS] db_name
修改数据库
ALTER {DATABASE | SCHEMA} [IF EXISTS] db_name
查看数据库
2.表操作
创建表
CREATE TABLE [IF NOT EXISTS] tb_name
(col1_def,col2_def,PRIMARY KEY(col_name,...),UNIQUE (col1,...),INDEX (col1,...))
[table_option]
table_option:
ENGINE [=] engine_name
COMMENT [=]
'string'
ROW_FORMAT [=] {DEFAULT|DYNAMIC|FIXED|COMPRESSED|REDUNDANT|COMPACT}
TABLESPACE tablespace_name [STORAGE {DISK|MEMORY|DEFAULT}]
删除表
DROP [TEMPORARY] TABLE [IF EXISTS] tbl_name [, tbl_name] ...
修改表
ALTER [ONLINE | OFFLINE] [IGNORE] TABLE tbl_name
[alter_specification [, alter_specification] ...]
alter_specification:
#插入新字段
ADD [COLUMN] col_name column_definition [FIRST | AFTER col_name ]
#删除字段
DROP [COLUMN] col_name
#修改字段属性
ALTER [COLUMN] col_name {SET DEFAULT literal | DROP DEFAULT}
#修改字段名
CHANGE [COLUMN] old_col_name new_col_name column_definition [FIRST|AFTER col_name]
#修改字段类型及属性等
MODIFY [COLUMN] col_name column_definition [FIRST | AFTER col_name]
修改表名
ALTER TABLE tb_name RENAME TO new_tb_name;
RENAME TABLE old_name TO new_name;
#指定排序字段
ORDER BY col_name [, col_name] ...
#转换字符集及排序规则
CONVERT TO CHARACTER SET charset_name [COLLATE collation_name]
查看表的索引
SHOW {INDEX | INDEXES | KEYS}
{FROM | IN} tbl_name
[{FROM | IN} db_name]
[WHERE expr]
用户及权限管理:
用户账号:'username'@'host'
host可使用IP、网络地址、使用通配符(_和%);来源主机
MariaDB [(none)]> CREATE USER 'lys'@'192.168.0.0' IDENTIFIED BY '123';
创建用户账号,并赋予密码;
CREATE USER 'username'@'host' IDENTIFIED BY [PASSWORD] 'password';
删除用户:
DROP USER 'username'@'host';
授权:
GRANT priv1,... ON db_name.tbl_name TO 'username'@'host' [IDENTIFIED BY [PASSWORD] 'password'];
MariaDB [(none)]> GRANT ALL ON mysql.* TO 'lys'@'192.168.0.0';
如果用户事先不存在,则创建此账号并授权;
ALL [PRIVILEGES]:所有权限
查看用户已经获得的授权:
SHOW GRANTS [FOR user]
收回授权:REVOKE
REVOKE priv1,... ON db_name.tbl_name FROM 'username'@'host';
mariadb的安装
mariadb的安装主要有3种方式:源代码,通用二进制格式,包管理器格式。
第一步,下载mariadb安装包,并将安装包解压到/usr/local目录下。同时做一个连接,连接到mysql。
第二步,进入到mysql中
第三步,复制support-files中的my-large.cnf到/etc/mysql/my.cnf。
以上只显示部分配置文件。在启动mariadb前,必须先初始化数据库。
[root@lys mysql]# scripts/mysql_install_db 数据字典
[root@lys mysql]# scripts/mysql_install_db --help 查看安装参数
Usage: scripts/mysql_install_db [OPTIONS]
--basedir=path The path to the MariaDB installation directory.
--builddir=path If using --srcdir with out-of-directory builds, you
will need to set this to the location of the build
directory where built files reside.
--cross-bootstrap For internal use. Used when building the MariaDB system
tables on a different host than the target.
--datadir=path The path to the MariaDB data directory.
--defaults-extra-file=name
Read this file after the global files are read.
--defaults-file=name Only read default options from the given file name.
--force Causes mysql_install_db to run even if DNS does not
work. In that case, grant table entries that normally
use hostnames will use IP addresses.
--help Display this help and exit.
--ldata=path The path to the MariaDB data directory. Same as
--datadir.
--no-defaults Don't read default options from any option file.
--defaults-file=path Read only this configuration file.
--rpm For internal use. This option is used by RPM files
during the MariaDB installation process.
--skip-name-resolve Use IP addresses rather than hostnames when creating
grant table entries. This option can be useful if
your DNS does not work.
--srcdir=path The path to the MariaDB source directory. This option
uses the compiled binaries and support files within the
source tree, useful for if you don't want to install
MariaDB yet and just want to create the system tables.
--user=user_name The login username to use for running mysqld. Files
and directories created by mysqld will be owned by this
user. You must be root to use this option. By default
mysqld runs using your current login name and files and
directories that it creates will be owned by you.
All other options are passed to the mysqld program
[root@lys mysql]# scripts/mysql_install_db --user=mysql --datadir=/data/mydata
以上参数--user是以什么用户运行,--datadir指定数据库的工作目录.
如果在/data/mydata出现如上内容说明数据库初始化成功。接下来就开始编辑数据库的配置文件,在配置前 先进行备份工作。
cp mysql.server /etc/rc.d/init.d/mysqld 复制数据库的启动脚本
chmod +x /etc/rc.d/init.d/mysqld 给启动脚本执行的权限
chkconfig --add mysqld 添加服务
chkconfig mysqld on 服务自启动
[root@lys support-files]# service mysqld start
Starting MySQL.... [ OK ]
mysql监听在3306端口。
[root@lys mysql]# vi /etc/profile.d/mysql.sh
[root@lys mysql]# cat /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin:$PATH
至此,基于通用二进制格式安装mariadb完成。