CentOS7安装MariaDB 10.2

准备工作:

一台VPS

CentOS7系统

开始安装:

vi /etc/yum.repos.d/MariaDB.repo

按i写入

# MariaDB 10.2 CentOS repository list – created 2017-06-15 02:25 UTC
# http://downloads.mariadb.org/mariadb/repositories/
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/10.2/centos7-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1

按ESC输入wq回车

然后输入安装命令

yum install MariaDB-server MariaDB-client

等待安装完毕启动数据库

systemctl start mariadb.service

启动后输入

mysql_secure_installation

安全配置向导详解

a)为root用户设置密码
b)删除匿名账号
c)取消root用户远程登录
d)删除test库和对test库的访问权限
e)刷新授权表使修改生效

[root@server1 ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we’ll need the current
password for the root user. If you’ve just installed MySQL, 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 MySQL
root user without the proper authorisation.
Set root password? [Y/n] <– 是否设置root用户密码,输入y并回车或直接回车
New password: <– 设置root用户的密码
Re-enter new password: <– 再输入一次你设置的密码
Password updated successfully!
Reloading privilege tables..
… Success!
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL 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] <– 是否删除匿名用户,生产环境建议删除,所以直接回车
… 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] <–是否禁止root远程登录,根据自己的需求选择Y/n并回车,建议禁止
… Success!
By default, MySQL 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] <– 是否删除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] <– 是否重新加载权限表,直接回车
… Success!
Cleaning up…
All done! If you’ve completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!

到这数据库安装完毕!

数据库常用命令(慢慢添加):

登陆命令:

mysql -u root -p

远程登陆命令:

mysql -h主机地址 -u用户名 -p用户密码

修改用户密码:

mysqladmin -u 用户名 -p 旧密码 password 新密码

创建数据库命令(先登陆):

create database <数据库名>;

显示所有数据库命令(先登陆):

show databases;

删除数据库命令(先登陆):

drop database <数据库名>;

使用数据库命令(先登陆):

use <数据库名>;

当前选择(连接)的数据库:

select database();

创建数据表:

create table <表名> (<字段名1> <类型1> [,..<字段名n> <类型n>]);

例如,建立一个名为MyClass的表:

字段名 数字类型 数据宽度 是否为空 是否主键 自动增加 默认值
id int 4 primary key auto_increment
name char 20
sex int 4 0
degree double 16

mysql> create table MyClass(
> id int(4) not null primary key auto_increment,
> name char(20) not null,
> sex int(4) not null default ‘0’,
> degree double(16,2));

获取数据表结构:

desc 表名;
同样
show columns from 表名;
也能获取数据表结构。

举例如下:
mysql> desc MyClass;
mysql> show columns from MyClass;

删除数据表:

drop table <表名>;

例如,删除表名为 MyClass 的表:
mysql> drop table MyClass;

向表中插入数据:

insert into <表名> [(<字段名1>[,..<字段名n > ])] values ( 值1 )[, ( 值n )];

例如:往表 MyClass中插入两条记录,这两条记录表示:编号为1的名为Tom的成绩为96.45,编号为2 的名为Joan 的成绩为82.99,编号为3 的名为Wang 的成绩为96.5。
mysql> insert into MyClass values(1,’Tom’,96.45),(2,’Joan’,82.99), (2,’Wang’, 96.59);

查询表中的数据:

select <字段1, 字段2, …> from < 表名 > where < 表达式 >;

例如,查看表 MyClass 中所有数据:
mysql> select * from MyClass;

删除表中的数据:

delete from 表名 where 表达式

例如,删除表 MyClass中编号为1 的记录:
mysql> delete from MyClass where id=1;

修改表中的数据:

update 表名 set 字段=新值,… where 条件;

举例如下:
mysql> update MyClass set name=’Mary’ where id=1;

例子1,单表的MySQL UPDATE语句:
UPDATE [LOW_PRIORITY] [IGNORE] tbl_name SET col_name1=expr1 [, col_name2=expr2 …] [WHERE where_definition] [ORDER BY …] [LIMIT row_count];

例子2,多表的UPDATE语句:
UPDATE [LOW_PRIORITY] [IGNORE] table_references SET col_name1=expr1 [, col_name2=expr2 …] [WHERE where_definition];

增加表的字段:

alter table 表名 add字段 类型 其他;

例如,在表MyClass中添加了一个字段passtest,类型为int(4),默认值为0:
mysql> alter table MyClass add passtest int(4) default ‘0’;

1) 加索引
mysql> alter table 表名 add index 索引名 (字段名1[,字段名2 …]);

例子: mysql> alter table employee add index emp_name (name);

2) 加主关键字的索引
mysql> alter table 表名 add primary key (字段名);

例子: mysql> alter table employee add primary key(id);

3) 加唯一限制条件的索引
mysql> alter table 表名 add unique 索引名 (字段名);

例子: mysql> alter table employee add unique emp_name2(cardnumber);

4) 删除某个索引
mysql> alter table 表名 drop index 索引名;

例子: mysql>alter table employee drop index emp_name;

5) 增加字段
mysql> ALTER TABLE table_name ADD field_name field_type;

6) 修改原字段名称及类型
mysql> ALTER TABLE table_name CHANGE old_field_name new_field_name field_type;

7) 删除字段
MySQL ALTER TABLE table_name DROP field_name;

修改表名:

rename table 原表名 to 新表名;

例如,在表MyClass名字更改为YouClass:
mysql> rename table MyClass to YouClass;

当你执行 RENAME 时,你不能有任何锁定的表或活动的事务。你同样也必须有对原初表的 ALTER 和 DROP 权限,以及对新表的 CREATE 和 INSERT 权限。

如果在多表更名中,MySQL 遭遇到任何错误,它将对所有被更名的表进行倒退更名,将每件事物退回到最初状态。

备份数据库:

1) 导出整个数据库(导出文件默认是存在mysql\bin目录下)
mysqldump -u 用户名 -p 数据库名 > 导出的文件名
mysqldump -u user_name -p123456 database_name > outfile_name.sql

2) 导出一个表
mysqldump -u 用户名 -p 数据库名 表名> 导出的文件名
mysqldump -u user_name -p database_name table_name > outfile_name.sql

3) 导出一个数据库结构
mysqldump -u user_name -p -d –add-drop-table database_name > outfile_name.sql
-d 没有数据 –add-drop-table 在每个create语句之前增加一个drop table

4) 带语言参数导出
mysqldump -uroot -p –default-character-set=latin1 –set-charset=gbk –skip-opt database_name > outfile_name.sql

例如,将aaa库备份到文件back_aaa中:
[root@test1 root]# cd /home/data/mysql
[root@test1 mysql]# mysqldump -u root -p –opt aaa > back_aaa

 

发表评论

邮箱地址不会被公开。 必填项已用*标注