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)
