mysql5.7安装及基本命令使用

1.Mysql5.7的安装

安装环境:mac osx10.11

1.1. 首先,在mysql官网下载mysql5.7的安装包,本人下载的版本是mysql-5.7.15-osx10.11-x86_64.dmg,后面所有的实践也都是基于这个版本的;

1.2. 然后,根据提示一步步安装,安装完成后,会生成root账户的随机密码,一定要保存好这个密码;

1.3. 配置环境变量,mysql默认是安装在/usr/local下的,使用命令echo $SHELL查看系统使用的shell类型,本人用的是zsh,所以修改.bash_profile是无效的,应该修改.zshrc文件;

➜  ~ vi ~/.zshrc
    export PATH=${PATH}:/usr/local/mysql/bin
1.4. 启动mysql服务,可以使用命令:

➜  sudo /usr/local/mysql/support-files/mysql.server start
      也可以通过【系统偏好设置】,找到mysql图标的应用,点击按钮启动服务

1.5.使用之前保存的临时密码登录,并重置密码

➜  ~ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 68
Server version: 5.7.15 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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.

此时可以成功登录了,但是如果执行任何一个命令,如show databases; 就会报错:

ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
所以需要重置密码:

set password =password('你的密码');


通过查看数据库版本的命令,来验证下:

mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.15    |
+-----------+
1 row in set (0.00 sec)

2.基本使用

2.1 数据库实例的连接与断开

参考地址:http://dev.mysql.com/doc/refman/5.7/en/connecting-disconnecting.html

使用下面的命令:mysql -h host -u user -p 连接实例,如果连接的是本地机器,可以省略host

如:

➜  ~ mysql -h 127.0.0.1 -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9

使用命令quit断开连接,如:

mysql> QUIT
Bye


2.2 查询

参考地址:http://dev.mysql.com/doc/refman/5.7/en/entering-queries.html

比如查询数据库实例的版本及当前日期:

mysql> select version(),current_date;
+-----------+--------------+
| version() | current_date |
+-----------+--------------+
| 5.7.15    | 2016-11-19   |
+-----------+--------------+
1 row in set (0.00 sec)

mysql> 
这里有几个注意的点:

(1). 所有的语句以分号结束,除了quit命令;

     多行输入时,如果突然想取消执行,可以输入\c,如:

mysql> select   
    -> user()\c
mysql> 

(2). 除了返回结果集,还返回了结果集行数及查询消耗的时间,但是这个“查询消耗时间”,并不是精确的,它代表的是“挂钟时间”,并不是cpu或机器时间,而且还会受到服务器负载和网络延迟的影响;

(3). 关键词大小写不敏感,如:

mysql> select Version(),current_DATE;
+-----------+--------------+
| Version() | current_DATE |
+-----------+--------------+
| 5.7.15    | 2016-11-19   |
+-----------+--------------+
1 row in set (0.01 sec)

(4). 多行输入的提示符及代表的含义,有以下几种:


2.3 库表相关

创建库表以及基础的SQL查询比较简单,这里不做说明,简单介绍下获取数据库信息的方式:

查看所有的库:show databases;

使用某个库:use dbname;

查看库有哪些表:show tables;

查看表的列:describe tablename;

 直接登录某个数据库:mysql -h host -u user -p dbname,如:

➜  ~ mysql -h 127.0.0.1 -u root -p mytest;
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.

上面没有指定端口,使用的是默认端口3306,如果要指定端口,可以加--port或-P,如:

➜  ~ mysql -h 127.0.0.1 -u root -P 3306 -p    
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.

当然还可以指定其他选项,如protocal,可以参考:

http://dev.mysql.com/doc/refman/5.7/en/connecting.html


3.Mysql5.7的卸载

sudo rm -rf /usr/local/mysql  
sudo rm -rf /usr/local/mysql*  
sudo rm -rf /Library/PreferencePanes/My*  
sudo rm -rf /var/db/receipts/com.mysql.*  
参考: http://blog.csdn.net/maxsky/article/details/40347505



  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值