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): ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) Enter current password for root (enter for none): ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) 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.
You already have a root password set, so you can safely answer 'n'.
Change the root password? [Y/n] n ... skipping.
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] n ... skipping.
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 - 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. 第一次启动MySQL只能使用root用户
show databases; #查看数据库 use XXX; #进入数据库 show tables; # 查看表 create database XXXX charset utf8; #创建数据库,utf8格式 drop database XXXX; #删除数据库 show create database XXXXX; #查看创建数据库的 show create table tablename; #查看创建的表 description tablenmae; #查看表结构 表结构说明:
使用mysqladmin管理配置mairadb
#自增长 auto_increment #类型 Type #非空 not null #默认值 default 'xx' #唯一 unique #指定字符集 charset #主键 primary key #外键 增加两个表之间的联系 增加表:
create table students( id int auto_increment primary key, name varchar(10) not null, sex varchar(3) default '女', address varchar(50), phone int not null unique, age, ); =============================== create table scores( id int auto_increnent primary key, s_id int not null, grade float not null, ); 删除表
drop table tablename; truncate tablename;#快速删除表 增
insert into student (name,money,sex,phone) values ('hk',10000,'男',188); insert into student values('','小明',100,'',120); 改
update student set money=100;#不指定条件,修改所有 update student set money=110 where name='hk';#只改hk 查: select * from students limit 1,5; #从第几条开始,下面的x条,不包含开始的那一条 SELECT * from students limit 5;查询5条 SELECT id,stu_name,sex,money,phone from students;#指定查询的字段 SELECT * from students;#查询所有的数据 SELECT * from students where sex='男';#指定条件 SELECT * from students where sex='男' and money>100; #多个条件,必须同时满足 SELECT * from students where sex='男' or sex='未知' ; #多个条件,有一个满足即可 SELECT * from students where sex !='男'; #<>也是不等于 SELECT * FROM students where addr like '%东京%';#模糊匹配,%代表的是通配符,必须得用like SELECT * from students a where a.stu_name like '姚_';#_通配符表示任意一个单字符,姚字后面只能跟一个字 SELECT a.stu_name '学生名称',a.phone '学生电话' from students as a where a.stu_name='姚远';#给表起别名,as可以省略 SELECT * from students a where a.stu_name in ('×××','林倩','林远');# in SELECT * from students a where a.money BETWEEN 1000 and 10000;#在什么什么之间的数据 SELECT * from students ORDER BY money desc; #order by xxx desc,根据哪个字段继续排序,默认是升序, 降序是desc,升序asc SELECT * from students a where a.addr = '' or a.addr is null; #查询字段为空的数据 SELECT DISTINCT a.money from students a ;#去重 SELECT COUNT(*) '学生人数' from students where sex='女'; #统计行数 SELECT MAX(a.money) 钱最多 from students a; #最大值 SELECT min(money) 钱最少 from students;#最小值 SELECT AVG(a.money) 平均多少钱 from students a; #平均数 SELECT sum(a.money) 总共多少钱 from students a;#总和 SELECT sex 性别,count(*) 人数 from students GROUP BY sex; #分组 四、mysqladmin操作
更改管理员密码
mysqladmin -u root -p oldpassword newpaaswd 创建数据库
# mysqladmin -u root -p create ABC Enter password: 查看mariaDB服务状态
# mysqladmin proc stat -u root -p Enter password: 使用mysqladmin管理配置mairadb
说明:
Uptime :在线运行时间,单位秒
Threads:活动线程(客户)的数目
Question:服务器启动以来的客户问题(查询)数目
Slow queries:执行时间超过long-query_time秒的查询数量
Opens:服务器已经打开的数据库的数量
Flush tables:服务器已经执行的flush、refresh和reload的命令的数量
Queries per second avg:平均每秒查询数
mysqladmin常用命令
mysqladmin [OPTIONS] command command.. #语法 参数选项: -c number 自动运行次数统计,必须和 -i 一起使用 -i number 间隔多长时间重复执行 #每个两秒查看一次服务器的状态,总共重复5次。 mysqladmin -uroot -p -i 2 -c 5 status 使用mysqladmin管理配置mairadb
#查看服务器的状况: mysqladmin -uroot -p status 使用mysqladmin管理配置mairadb