MySQL是一个开放源码的小型关联式数据库管理系统
,开发者为瑞典MySQL AB公司, 目前属于Oracle公司,MySQL被广泛地应用在Internet上的中小型网站中。由于其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,许多中小型网站为了降低网站总体拥有成本而选择了MySQL作为网站数据库。
对应目前主流的LAMP架构来说,Mysql更是得到各位IT运维、DBA的青睐,目前mysql已被orcacle收购,不过好消息是原来mysql创始人已独立出来自己重新开发了一个MariaDB,而且使用的人数越来越多。而且MariaDB兼容mysql所有的功能和相关参数。
Mysql常用的两大引擎有MyISAM和innoDB,那他们有什么明显的区别呢,什么场合使用什么引擎呢?
MyISAM类型
的表强调的是性能,其执行数度比InnoDB类型更快,但不提供事务支持,如果执行大量的SELECT操作,MyISAM是更好的选择,支持表锁。
InnoDB
提供事务支持事务,外部键等高级 数据库功能,执行大量的INSERT或UPDATE,出于性能方面的考虑,应该使用InnoDB表,支持行锁。
MySQL安装方式有两种,一种是yum/rpm安装
,另外一种是tar源码安装
。
Yum 安装方法很简单,执行命令如下
yum install –y mysql-server mysql-devel mysql
这个mysql安装到哪里去了呢
使用命令:
rpm -ql mariadb |more
mysql的配置文件位置
[root@localhost ~]# ll /etc/my.cnf
-rw-r--r--. 1 root root 570 9月 30 21:21 /etc/my.cnf
/var/lib/mysql/
是mysql的数据存放目录(这里面存放了许多库,库里有许多表)
[root@localhost ~]# cd /var/lib/mysql/
[root@localhost mysql]# ls
aria_log.00000001 ibdata1 ib_logfile1 mysql.sock ssrf
aria_log_control ib_logfile0 mysql performance_schema test
重启mysql的服务
[root@localhost mysql]# service mariadb restart
Redirecting to /bin/systemctl restart mariadb.service
直接输入mysql,是进入mysql这个库的终端,在这个终端里面我们可以对这个数据库进行操作
相关数据库操作
查看数据库
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
创建数据库
MariaDB [(none)]> create database chen;
Query OK, 1 row affected (0.07 sec)
进入到我们已经修改的数据库chen里面
MariaDB [(none)]> use chen;
Database changed
查看数据库中的表,当前数据库是新建的,所以表是空的。
MariaDB [chen]> show tables-> ;
Empty set (0.03 sec)
删除数据库
MariaDB [chen]> drop database chen;
Query OK, 0 rows affected (0.20 sec)
创建表,就相当于创建了一个表的结构
MariaDB [chen]> create table test_001 (name char(20),age char(20),job char(20));
Query OK, 0 rows affected (0.73 sec)
查看表
MariaDB [chen]> show tables-> ;
+----------------+
| Tables_in_chen |
+----------------+
| test_001 |
+----------------+
1 row in set (0.04 sec)
查看表的内容
MariaDB [chen]> select * from test_001;
Empty set (0.03 sec)
我们向表里面写入数据
MariaDB [chen]> insert into test_001 values ('zhangsan',27,'IT');
Query OK, 1 row affected (0.13 sec)MariaDB [chen]> insert into test_001 values ('lisi',30,'IT');
Query OK, 1 row affected (0.05 sec)MariaDB [chen]> insert into test_001 values ('wangyu',20,'enginer');
Query OK, 1 row affected (0.04 sec)MariaDB [chen]> insert into test_001 values ('wangyu',27,'enginer');
Query OK, 1 row affected (0.11 sec)
这次我们查看表 的内容
MariaDB [chen]> select * from test_001;
+----------+------+---------+
| name | age | job |
+----------+------+---------+
| zhangsan | 27 | IT |
| lisi | 30 | IT |
| wangyu | 20 | enginer |
| wangyu | 27 | enginer |
+----------+------+---------+
4 rows in set (0.04 sec)
我们只想查看表中第一行的内容
MariaDB [chen]> select * from test_001 limit 1;
+----------+------+------+
| name | age | job |
+----------+------+------+
| zhangsan | 27 | IT |
+----------+------+------+
1 row in set (0.04 sec)
现在我们想要查询名字为zhangsan
的
MariaDB [chen]> select * from test_001 where name='zhangsan';
+----------+------+------+
| name | age | job |
+----------+------+------+
| zhangsan | 27 | IT |
+----------+------+------+
1 row in set (0.06 sec)
查询名字为wangyu的
MariaDB [chen]> select * from test_001 where name='wangyu';
+--------+------+---------+
| name | age | job |
+--------+------+---------+
| wangyu | 20 | enginer |
| wangyu | 27 | enginer |
+--------+------+---------+
2 rows in set (0.06 sec)
我们只想查询名字为wangyu,年龄为20的
MariaDB [chen]> select * from test_001 where name='wangyu' and age=20;
+--------+------+---------+
| name | age | job |
+--------+------+---------+
| wangyu | 20 | enginer |
+--------+------+---------+
1 row in set (0.05 sec)
只是查询名字
MariaDB [chen]> select name from test_001;
+----------+
| name |
+----------+
| zhangsan |
| lisi |
| wangyu |
| wangyu |
+----------+
4 rows in set (0.06 sec)
查看这个表下面有多少条记录
MariaDB [chen]> select count(*) from test_001;
+----------+
| count(*) |
+----------+
| 4 |
+----------+
1 row in set (0.04 sec)
接下来我们想要删除lisi
这一行
MariaDB [chen]> delete from test_001 where name="lisi";
Query OK, 1 row affected (1.95 sec)
注意:drop table test_001 (删除表)
delete from test_001 (清空表内容)
看表的结构
MariaDB [chen]> describe test_001;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| name | char(20) | YES | | NULL | |
| age | char(20) | YES | | NULL | |
| job | char(20) | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
3 rows in set (0.33 sec
注意:describe test_001与desc test_001同理
查看表的创建语句
MariaDB [chen]> show create table test_001;
+----------+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+----------+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
| test_001 | CREATE TABLE `test_001` (`name` char(20) DEFAULT NULL,`age` char(20) DEFAULT NULL,`job` char(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+----------+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.07 sec)
show engines;
查看数据库引擎
查看这个表里char开头的
MariaDB [chen]> show variables like "%char%";
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.03 sec)
在mysql配置文件下去修改字符集,加入内容
[client]
default-character-set=utf8
[mysqld]
default-character-set=utf8
[mysql]
default-character-set=utf8
修改完配置记得重启
[root@localhost mysql]# service mariadb restart
Redirecting to /bin/systemctl restart mariadb.service
修改完以后数据库就可以显示中文了。
我们给客户机授权,使得客户机可以登录
MariaDB [chen]> grant all on *.* to chen@192.168.0.159 identified by "1";
Query OK, 0 rows affected (0.03 sec)
然后我们去客户机登录,登录成功
[root@localhost ~]# mysql -h192.168.0.155 -uchen -p1
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.5.68-MariaDB MariaDB ServerCopyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>
刷新权限
MariaDB [chen]> flush privileges;
Query OK, 0 rows affected (0.06 sec)
备份数据库
mysqldump -uroot -p chen>lin.sql
当删除数据库中的表时候,还原数据库
MariaDB [(none)]> source /root/lin.sql;
Query OK, 0 rows affected (0.58 sec)