Linux 下MySQL安装及使用

一、Linux下安装MySQL具体步骤如下网址

http://www.linuxidc.com/Linux/2016-09/134940.htm

 

具体步骤如下:

CentOS7默认数据库是mariadb,配置等用着不习惯,因此决定改成mysql,但是CentOS7yum源中默认好像是没有mysql的。为了解决这个问题,我们要先下载mysqlrepo源。

 

1.下载mysqlrepo

$ wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm

 

2.安装mysql-community-release-el7-5.noarch.rpm

$ sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm

安装这个包后,会获得两个mysqlyum repo源:/etc/yum.repos.d/mysql-community.repo/etc/yum.repos.d/mysql-community-source.repo

 

3.安装mysql

$ sudo yum install mysql-server

根据提示安装就可以了,不过安装完成后没有密码,需要重置密码

 

4.重置mysql密码

$ mysql -u root

登录时有可能报这样的错:ERROR 2002 (HY000): Cant connect to local MySQL server through socket /var/lib/mysql/mysql.sock(2),原因是/var/lib/mysql的访问权限问题。下面的命令把/var/lib/mysql的拥有者改为当前用户:

$ sudo chown -R root:root /var/lib/mysql

重启mysql服务

 

$ service mysqld restart

接下来登录重置密码:

 

$ mysql -u root  //直接回车进入mysql控制台
mysql > use mysql;
mysql > update user set password=password('123456') where user='root';
mysql > exit;

本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-09/134940.htm

 

 

二、配置远程登录的权限

1.grant all privileges on *.* to 'root'@'%' identified by 'root123' with grant option;

2.flush privileges;  刷新mysql登录

3.远程navicate 登录时,出现2003错误,那就关闭3306的防火墙

 iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT

 

4. 远程登录数据库工具——navicat for mysql 的界面介绍如下图

 

 

 

 

 

 

三、登录mysql 并使用mysql

1. mysql -u root -p  提示输入数据库的密码

2. show databases;  查看数据库服务器中的数据库

3. 数据库服务器中有mysql 数据库,use mysql 使用mysql数据库

                        

四、数据库简介

1.关系型数据库

OracleDB2MySQLMS SQL 都是关系型数据库

有一对一、一对多和多对多的关系

2.非关系型数据库

HBaseMongoDBRedisCassandra

4.非关系型数据库在大数据中运用很广。非关系型数据库就是数据时非规则的,例如聊天记录、视频等都是非规则的数据。

5.数据库的应用无处不在:

① 游戏排行榜  

② 银行账户    

③ 美团、携程       

④ 淘宝、京东

6.12306使用Oracle的原因是

从安全性考虑,因为Oracle比MySQL的安全性更高

Oracle的见索性和查询速度都比MySQL要高很多

7.SQL Server是微软公司的;  

8.DB2是IBM公司的,IBM对DB2进行绑定销售,买IBM 的产品就必须使用DB2数据库,其他的数据库不兼容。

9.数据库服务器、数据库和数据库表的关系

通常所说的安装数据库就是安装的是数据库服务器,一个数据库服务器上可以创建多个数据库,一个数据库表中可以创建多个数据库表

10. 数据在数据库中的存储方式

① 数据库表的一行称为一个记录  

② 数据库表的一列称为一个字段

11. 概念和基本操作                

① SQL 结构化查询语言

② DDL 数据定义语言

③ DML  数据操纵语言

④ DQL  数据查询语言

⑤ CRUD操作

12. MySQL操作

查询——》新建查询编辑器,在此输入SQL语句

    create DATABASE test;

use test;

create table users3

(

  id int PRIMARY KEY auto_increment,

  name char(50)

)CHARACTER set utf8 #设置字符编码;

 

13. MySQL常用的数据类型

 

14. 创建一个员工表

    Create database test1

use test1;

create table employee

(

 id int PRIMARY KEY auto_increment,

 name char(50) not null,

 sex char(2) not null,

 birthday date not null,

 entry_date date not null,

 job char(100) not null,

 salary float(5,2) not null,

 resume longtext not null

)

15. 数据库表常用的操作

##数据定义语言(DDL)

(1) rename table employee to emp 更改表名

(2) show create table users   显示创建表users的过程

(3) desc users 显示表结构

(4) drop table users 删除表users

(5) alter table users1 add sex bit not null   更改表的性别的数据类型(bit)

(6) alter table users1 modify sex char(2) not null 更改数据表users1 sex的数据类型

(7) alter table users1 drop sex   删除users1表的sex列

(8) alter table user change name username char(50); 修改user表中列名name为username  

(9)   Insert into employee(name,sex,birthday,entry_date,job,salary,resume) values(‘lily’,1,’2002-10-21’,’2012-7-12’,’软件工程’,121.21,’你很好’)

 

(10) update tablename set col_name1=expr1 where col_name2=expr2  #修改表的列值

例如:update employee set salary=500.01 where name=’zhangsan’#把zhangsan的薪水改为500.01,

(11) update employee set salary=200.02 where name like ‘%zhangsan%’#把name包含zhangsan的所有员工的薪水全部改为200.02

(12)  update employee set salary=600.06 where salary < 500   #把所有薪水小于500的员工的薪水改为600.06

(13) update employee set salary=salary+500;将所有员工的薪增加500

(14) update employee set salary=salary+500 where 1==1;将所有员工的薪增加500

(15) 

(16) update employee set salary=100,resume='good employee' where name='wangwu';#修改王五的薪水和简历做修改。

 

(17) drop table employee_copy  #删除整个表名,即删除删除整个表结构和数据;

(18) delete from employee where name='lisi';  #删除名字为李四的员工

(19) delete from employee where salary < 600;  #删除薪水小于600 的员工

(20) truncate employee_copy;   #删除employee_copy中所有的记录

(21) delete from employee_copy/delete from employee_copy where 1==1 #也是删除表中的记录

 

(22) select * from employee  #从employee表中查询所有数据,一般不这么用

(23) select name,salary from employee   #从employee表查询名字及其薪水

(24) select name as 姓名,salary as 薪水 from employee  #从employee表查询名字及其薪水,并将name和salary起个别名为姓名和薪水,方便用户查看;

(25) select name as 姓名,sex as 性别,salary as 薪水 from employee where sex = 1;

(26) select  name as 姓名, salary as 薪水 from employee where salary>200 and salary<500   #查找薪水在200-500之间的员工的姓名和薪水;

(27) select  name as 姓名, salary as 薪水 from employee where salary between 200 and 500   #查询薪水在200-500之间的员工的姓名和薪水;

(28) select name as 姓名,job as 职位,salary as 薪水 from employee where salary > 500 or job=’DBA’  #查询薪水大于500或者职位(job)为DBA的所有员工的姓名 职位 薪水

(29) select sum(salary) as 所有员工薪水 from employee where sex=1    #mysql -devel API() 提供了sum函数库,计算总和;还提供 count函数计算数量。

(30) select count(*) as 公司员工总数 from employee

(31) select max(salary) as 薪水最高d的员工薪水,name as 姓名 from employee;

(32) select count(*) as 公司非女员工总数 from employee where sex<>1

(33) select * from employee where job in(‘测试工程师’,’DBA’)  #查询职位为测试工程师或者DBA的所有员工的信息;

(34) select salary as 薪水 from employee_copy where name like 'zhang%';选择姓zhang的员工的薪水;

 

(35) Select * from employee order by salary asc;   #按薪水的升序排序

(36) Select * from employee order by salary desc;   #按薪水的降序排序

(37) Select * from employee where job=’DBA’ order by salary asc;   #查询职位为DBA的所有员工,并按薪水的升序排序。

 

 

16. 数据库操纵语言

向数据表中插入值

INSERT INTO user (

NAME,

sex,

birthday,

entry_date,

job,

salary,

resume

)

VALUES

(

'zhangsan2',

0,

'2017-10-28',

'2017-10-29',

'数据库开发工程师',

100.02,

'不错!'

)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值