1 官网下载,链接 https://www.mysql.com/downloads/
Download MySQL Community Server
默认为你选好了Mac OS X 平台
选择的是.dmg的。点击右侧的download进行下载。
跳转到另外一个界面,提示你需不需要注册,直接选择最下面的“No thanks,just take me to downloads!”
2 安装MySQL
安装完成后终端输入:
$mysql -version
-bash: mysql: command not found
”/usr/local/mysql/bin/mysql”为mysql默认安装路径:
$/usr/local/mysql/bin/mysql -version
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
$cd /usr/local/bin
$sudo ln -fs /usr/local/mysql/bin/mysql mysql
Password:
$mysql -version
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
配置root账号的密码,默认没有配置,
$ mysql -u root
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 3
Server version: 5.7.18 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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>
mysql>update mysql.user set authentication_string = password('******') where user ='root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> flush privileges;
mysql> quit
flush privileges后mysql -u root就登录不上了,需要用密码了例如下面
$mysql -u root
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 5
Server version: 5.7.18 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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> create database homework;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
出现这样的报错,解决办法:重新设置一遍密码
mysql> set password =password('******');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> create database homework;
Query OK, 1 row affected (0.00 sec)
mysql> use homework;
Database changed
mysql> show tables;
Empty set (0.00 sec)
数据库的基本操作,创建数据库。
mysql> create table Student(Sno int(10),Sname varchar(255),Ssex varchar(255),Sage int(10),Sdept varchar(255));
Query OK, 0 rows affected (0.03 sec)
mysql> show tables;
+--------------------+
| Tables_in_homework |
+--------------------+
| Student |
+--------------------+
1 row in set (0.00 sec)
查看创建表的信息语句:
mysql> show create table Student;
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Student | CREATE TABLE `Student` (
`Sno` int(10) DEFAULT NULL,
`Sname` varchar(255) DEFAULT NULL,
`Ssex` varchar(255) DEFAULT NULL,
`Sage` int(10) DEFAULT NULL,
`Sdept` varchar(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+---------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.02 sec)
mysql> create table Course(Con int(10),Cname varchar(255),Cpno int(10),Ccredit int(10));
Query OK, 0 rows affected (0.03 sec)
mysql> show create table Course;
+--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Course | CREATE TABLE `Course` (
`Con` int(10) DEFAULT NULL,
`Cname` varchar(255) DEFAULT NULL,
`Cpno` int(10) DEFAULT NULL,
`Ccredit` int(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
插入一条数据:
mysql> insert into Course values('4','data structure','7','4');
Query OK, 1 row affected (0.00 sec)
更新一条数据:
mysql> update Course set Cno='5' where Cname='data structure';
ERROR 1054 (42S22): Unknown column 'Cno' in 'field list'
发现创建的字段应该是Cno,创建错了,成Con
更改字段:
mysql> alter table Course change Con Cno int(10);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> update Course set Cno='5' where Cname='data structure';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
查询数据:
mysql> select * from Course;
+------+--------------------+------+---------+
| Cno | Cname | Cpno | Ccredit |
+------+--------------------+------+---------+
| 1 | database | 5 | 4 |
| 2 | math | 0 | 2 |
| 3 | information system | 1 | 4 |
| 4 | operation system | 6 | 3 |
| 5 | data structure | 7 | 4 |
| 6 | data process | 0 | 2 |
| 7 | pascal | 6 | 4 |
+------+--------------------+------+---------+
7 rows in set (0.00 sec)