1 数据库的基本介绍
1.1什么是数据库-------存放数据的仓库
数据库的种类:
从大的方向上:
1、传统的关系型数据库:MYSQL,Oracle,db2,sysbase
互联网最火的:MYSQL,MariaDB
PS:关系型数据库的本质就就二维表
特点:读写更多的是和磁盘打交道,数据一致性,安全性
缺点:速度慢
2、非关系型数据库:nosql:not only sql
特点:以高效,高性能为目的,凡是和效率性能无关的因素都尽可能抛弃
它是关系型数据库的补充,非革命;但是越来越火!
memcached 纯内存缓存的软件
memcached不再是二维表的形式了,而是键值对的形式
数据类型:
key-->value 键值对的形式
redis 内存加持久化软件(磁盘)
数据类型:
key-->value 键值对的形式
类型更多:集合set,列表list
1.2 数据库小结
关系型数据库:
1、二维表格
2、mysql和Oracle数据库,互联网最常用的是Mysql
3、通过SQL结构化查询语句存取数据
4、保持数据一致性方面很强。ACID理论
NOSQL非关系型数据库:
1、NOSQL不是否定关系型数据库,而是作为关系型数据库的一个重要补充
2、NOSQL为了高性能、高并发而生的
3、NOSQL典型产品:memcached(纯内存),redis(持久化缓存),mongodb
MYSQL:
商业版
社区版*****
2 mysql的安装与配置
2.1 linux软件的安装方式
1、yum/rpm 简单 快,无法定制。
2、编译安装,./configure;make;make install 复杂,速度慢,可定制
针对MYSQL 5.0--5.1,选择此编译方式
针对MYSQL 5.5以上,编译安装方式为===》 ./cmake;gmake;gmake install
3、二进制包*****
直接解压用(绿色软件,无需安装) 简单,快,不好定制
2.2 Mysql的安装(二进制包)
下载好二进制包,传到服务器
[root@lnmp02 tools]# rz -y rz waiting to receive. Starting zmodem transfer. Press Ctrl+C to cancel. Transferring mysql-5.5.32-linux2.6-x86_64.tar.gz... 100% 182346 KB 4052 KB/sec 00:00:45 0 Errors [root@lnmp02 tools]# ls mysql-5.5.32-linux2.6-x86_64.tar.gz nginx-1.6.3 nginx-1.6.3.tar.gz
添加mysql用户
[root@lnmp02 tools]# useradd mysql -s /sbin/nologin -M [root@lnmp02 tools]# id mysql uid=506(mysql) gid=506(mysql) 组=506(mysql)
解压
[root@lnmp02 tools]# tar xf mysql-5.5.32-linux2.6-x86_64.tar.gz [root@lnmp02 tools]# ls mysql-5.5.32-linux2.6-x86_64 nginx-1.6.3 mysql-5.5.32-linux2.6-x86_64.tar.gz nginx-1.6.3.tar.gz
移动到软件安装目录
[root@lnmp02 tools]# mv mysql-5.5.32-linux2.6-x86_64 /application/mysql-5.5.32
做个软链接
[root@lnmp02 tools]# ln -s /application/mysql-5.5.32/ /application/mysql [root@lnmp02 tools]# ls -l /application/ 总用量 8 lrwxrwxrwx 1 root root 26 6月 25 11:44 mysql -> /application/mysql-5.5.32/ drwxr-xr-x 13 root root 4096 6月 25 11:41 mysql-5.5.32 lrwxrwxrwx 1 root root 25 6月 24 16:58 nginx -> /application/nginx-1.6.3/ drwxr-xr-x 11 root root 4096 6月 24 17:05 nginx-1.6.3
操作到此步骤相当于编译安装make install之后,安装完成了。
2.3 mysql的基本配置
初始化数据库
[root@lnmp02 tools]# /application/mysql/scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/application/mysql/data/ --user=mysql [root@lnmp02 tools]# echo $? 0 授权mysql管理数据库目录文件 [root@lnmp02 tools]# chown -R mysql.mysql /application/mysql/ 生成mysql配置文件 [root@lnmp02 tools]# cp /application/mysql/support-files/my-small.cnf /etc/my.cnf
配置启动mysql
[root@lnmp02 mysql]# sed -i 's#/usr/local/mysql#/application/mysql#g' /application/mysql/bin/mysqld_safe
启动mysql
[root@lnmp02 tools]# /application/mysql/bin/mysqld_safe & [1] 1128 [root@lnmp02 tools]# 170625 13:40:28 mysqld_safe Logging to '/application/mysql/data/lnmp02.err'. 170625 13:40:28 mysqld_safe Starting mysqld daemon with databases from /application/mysql/data
检查
[root@lnmp02 tools]# lsof -i :3306 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME mysqld 1346 mysql 10u IPv4 17488 0t0 TCP *:mysql (LISTEN)
配置环境变量
[root@lnmp02 tools]# echo $PATH /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin [root@lnmp02 tools]# cp /application/mysql/bin/* /usr/local/sbin/ [root@lnmp02 tools]# which mysql /usr/local/sbin/mysql
比较规范的方法配置环境变量
vi /etc/profile PATH="/application/mysql/bin/:$PATH" source /etc/profile
登录测试
[root@lnmp02 tools]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.5.32 MySQL Community Server (GPL) Copyright (c) 2000, 2013, 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>
2.4 配置传统方式启动mysql
[root@lnmp02 tools]# cd /application/mysql [root@lnmp02 mysql]# cp support-files/mysql.server /etc/init.d/mysqld [root@lnmp02 mysql]# sed -i 's#/usr/local/mysql#/application/mysql#g' /etc/init.d/mysqld [root@lnmp02 mysql]# chmod +x /etc/init.d/mysqld [root@lnmp02 mysql]# killall mysqld [root@lnmp02 mysql]# 170625 13:57:54 mysqld_safe mysqld from pid file /application/mysql/data/lnmp02.pid ended [root@lnmp02 mysql]# lsof -i :3306 [root@lnmp02 mysql]# [root@lnmp02 mysql]# /etc/init.d/mysqld start Starting MySQL.. SUCCESS! [root@lnmp02 mysql]# lsof -i :3306 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME mysqld 1633 mysql 10u IPv4 18284 0t0 TCP *:mysql (LISTEN)
设置开机启动
[root@lnmp02 mysql]# chkconfig mysqld on [root@lnmp02 mysql]# chkconfig --list mysqld mysqld 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
设置mysql密码及更改密码
[root@lnmp02 mysql]# mysqladmin -uroot password "123456" [root@lnmp02 mysql]# mysql ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) [root@lnmp02 mysql]# mysql -uroot -p123456 [root@lnmp02 mysql]# mysqladmin -uroot -p123456 password "wangxian" 《==更改密码
安全优化,删除test库
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.04 sec) mysql> drop database test; Query OK, 0 rows affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+ 3 rows in set (0.00 sec)
查找mysql库的用户表,并删除无用的用户
mysql> select user,host from mysql.user; +------+-----------+ | user | host | +------+-----------+ | root | 127.0.0.1 | | root | ::1 | | | lnmp02 | | root | lnmp02 | | | localhost | | root | localhost | +------+-----------+ 6 rows in set (0.00 sec) mysql> drop user ''@'lnmp02'; Query OK, 0 rows affected (0.00 sec) mysql> drop user ''@'localhost'; Query OK, 0 rows affected (0.00 sec) mysql> drop user 'root'@'lnmp02'; Query OK, 0 rows affected (0.00 sec) mysql> drop user 'root'@'::1'; Query OK, 0 rows affected (0.00 sec) mysql> select user,host from mysql.user; +------+-----------+ | user | host | +------+-----------+ | root | 127.0.0.1 | | root | localhost | +------+-----------+ 2 rows in set (0.00 sec)
如果主机名有大写或者特殊字符,drop无法删除,可以用delete删除
delete from mysql.user where user="root" and host="A"; flush privileges; 《==============刷新,让权限生效
mysql简单命令
help------------help show databases;
查所有的库 show databases; 《==============ls
切库 use mysql; 《==============cd
查表 show tables;
查看用户列表 select user,host from mysql.user;mysql库user表查看字段
查看当前用户 select user(); 《==============whoami
查看当前所在库 select database(); 《==============pwd
删除数据库 drop database 库名;
删除用户 drop user '用户'@'主机';
生效配置 flush privileges;
创建及增加用户 create user 'root'@'127.0.0.1' identified by 'wangxian';
mysql错误日志
[root@mysql-server ~]# cat /application/mysql/data/mysql-server.err