centos7.3通过yum方式安装mariadb
通过安装包组的方式安装
yum groupinfo mariadb mariadb-client
yum groupinstall mariadb mariadb-client
启动服务,第一次启动时间有点长
systemctl start mariadb
systemctl status mariadb
systemctl enable mariadb
安全策略设置,设置密码等
mysql_secure_installation
查看tcp 3306端口是否开启
ss -tunlp
mariadb配置文件
more /etc/my.cnf
数据库存放位置
ls /var/lib/mysql
登录数据库
mysql -uroot -p
帮助及查询命令
help
\s
select version();
select user();
show databases;
show mysql;
show tables;
system ls /root
system hostname
quit
centos6.8通过二进制包安装mariadb
二进制包下载地址:
https://downloads.mariadb.org/mariadb/5.5.57/
创建用户mysql
useradd -r -u 306 -d /app/data/ -s /sbin/nologin mysql
id mysql
getent passwd mysql
创建家目录即数据库存放目录,并更改其属主为mysql
mkdir /app/data
chown mysql. /app/data/
ll /app/data/ -d
解压mariadb安装包,注意必须指定目录/usr/local/
tar -xf mariadb-5.5.57-linux-x86_64.tar.gz -C /usr/local/
cd /usr/local/
创建一个软链接,并递归更改文件权限
ln -s mariadb-5.5.57-linux-x86_64/ mysql
chown -R mysql. mysql
ll mysql/
进入安装目录mysql,安装mariadb
cd /usr/local/mysql
生成数据库文件,指定数据库存放目录和用户
scripts/mysql_install_db --datadir=/app/data/ --user=mysql
复制配置文件
mkdir /etc/mysql
cp support-files/my-large.cnf /etc/mysql/my.cnf
编辑复制的配置文件,在[mysqld]服务配置中增加如下3条内容
vi /etc/mysql/my.cnf
[mysqld]
datadir = /app/data/
skip_name_resolve = on
innodb_file_per_table = on
添加mysqld服务启动项,并尝试启动服务
chkconfig --add mysqld
chkconfig --list | grep mysqld
service mysqld start
报错提示如下
Can‘t create/write to file ‘/var/log/mysqld.log‘ (Errcode: 13)
创建日志文件,尝试重新启动服务
touch mysqld.log
chown mysql. mysqld.log
service mysqld start
ss -tnlp
添加PATH路径,方便执行mysql命令
cd /etc/profile.d/
vi mysql.sh
export PATH=/usr/local/mysql/bin:$PATH
. mysql.sh
登录数据库,首次登录无密码
mysql
exit
修改安全策略yynny
mysql_secure_installation
通过密码登录
mysql -uroot -p
SQL语句
查看数据库,SQL语句不区分大小写 show databases; SHOW DATABASES; 切换数据库 use mysql; 查询库中的表 show tables; 查询表结构 desc user; 查询表中的所有信息 select * from user; 查询表中的指定列信息 select host,user,password from user; 如果未指定use哪个数据库,需指定数据库名.表名 select host,user,password from mysql.user; 查询当前用户 select user(); 创建数据库 create database mydb; SELECT也可以直接显示内容,类似于echo,as表示定义字段别名,支持直接算数运算 select "usera" as name,1+2 as result; 删除数据库 drop database mydb; 查看所有支持的字符集 show character set; 查看所有支持的排序规则 show collation; 获取命令帮助 help show help create database 查看系统自带的引擎 show engines; 创建表 help create table; use mydb create table student (id int unsigned primary key,name varchar(20) not null,age tinyint unsigned); show tables; desc student; 查看表索引 show indexes from mydb.student; 添加数据,字符串必须加‘‘ insert into student values(1,‘zhang‘,20); 添加个别字段数据 insert into student(id,name) values(2,‘wang‘); 查询数据 select * from student; 更改记录 update student set name=‘li‘ where id=2; 删除记录 delete from student where id=2; 清空表 truncate table mydb.student; select * from mydb.student; 排序,默认索引会自动排序 insert into student values(1,‘zhang‘,20); insert into student values(2,‘wang‘,21); insert into student values(4,‘li‘,24); insert into student values(10,‘zhao‘,20); insert into student values(5,‘song‘,21); select * from student; 查询按name列排序 select * from student order by name; 倒序desc select * from student order by name desc; 查询结果以别名显示,as关键字可以省略 select id 编号,name 名字 from student; 查询空值null insert into student(id,name) values(8,‘yang‘); select * from student where age is null;
通过旧表来创建新表 create table test1 (sid int,name varchar(20),address varchar(100)); create table test2 select * from test1; desc test2; 同上,导入旧表的所有数据到新表 create table student2 select * from student; select * from student2; 同上,只导入部分数据 create table student3 select id,name from student; select * from student3; 批量导入数据到新表 insert into test2 select id,name from student; 查找name字段包含a的数据条目 select * from test2 where name like "%a%"; 条件过滤 select * from student where id in (1,5,10); select * from student where id >=2 and id <=8; select * from student where is between 2 and 8; select * from student where name != ‘zhang‘;
添加远程登录用户,用户的组成有两部分:‘用户名‘@‘主机地址‘,如下用户名[email protected]客户端网段,密码redhat 创建远程登录用户 create user ‘usera‘@‘192.168.%.%‘ identified by ‘redhat‘; create user ‘userb‘@‘192.168.10.%‘ identified by ‘redhat‘; 测试远程登录 mysql -uusera -h 192.168.10.10 -p 默认为普通用户权限,不能显示所有数据库 show databases; 删除用户,注意用户名必须和之前创建的保持一致。用户名格式为:‘用户名‘@‘主机地址‘ drop user ‘userb‘@‘192.168.10.%‘; 更改用户口令 set password for ‘usera‘@‘192.168.%.%‘=password(‘centos‘); 通过mysql管理命令更改用户口令 /usr/local/mysql/bin/mysqladmin -u usera -p centos password ‘redhat‘ 授权查询和删除,和允许所有操作 grant select on mydb.* to ‘usera‘@‘192.168.%.%‘; grant delect on mydb.test to ‘usera‘@‘192.168.%.%‘; grant all on mydb.* to ‘userb‘@‘192.168.%.%‘ identified by userb; #创建账号并授权其对mydb的所有权限 远程通过usera用户登录,再次查看数据库,即可看到mydb的数据库 show databases; use mydb; delete from test; 禁止用户操作 revoke delete on mydb.* from ‘userb‘@‘192.168.%.%‘; flush privileges; 需要退出再重新登录,才会生效