安装mysql数据库服务器:
选择pkg,dmg包直接图形化安装;
2. 设置mysql环境变量,让系统能在任意路径下找到mysql执行文件:
1) 打开终端,输入: cd ~
2) 然后输入:sudo vim .bash_profile
3) 在文档的最下方输入:export PATH=${PATH}:/usr/local/mysql/bin;然后esc退出insert状态,并在最下方输入:wq保存退出。
4) 输入:source .bash_profile 回车执行,运行环境变量
3. 在偏好设置中找到mysql图标,可以开启关闭mysql服务器;
1)mac终端连接mysql服务器: ( 使用root账号连接mysql服务器)
mysql -u root -p
2) 修改root用户的密码:
SET PASSWORD FOR 'root'@'localhost' = PASSWORD(‘新密码');
4. 对数据库 ( database )整体操作:
(增删改查)
1) 创建数据库 create
create database MyTest;
2) 查看所有数据库 show
show databases;
3) 查看数据库的默认编码,以及修改数据库的默认编码
命令: status;
显示数据: Db characters: latin1 表示数据库的字符集
命令: alter database MyTest character set utf8; ( 修改成utf-8编码 )
4) 删除数据库 drop
drop database MyTest;
5) 使用指定数据库
use MyTest; use 数据库名字
6) 查看当前使用的数据库 (select)
select database;
5. 对数据库表(table)整体的管理操作:
(增删改查)
1. 显示指定数据库中所有的table:
show tables;
2. 增table:
1) 创建table:
create table student;
2) 利用已知数据创建表:
create table newTable select * from ExistTable; 3. 删table:
drop table student;
4.改table:
1)table表名重命名:
rename table student to teacher;
( rename table oldName to newName )
2) table修改表的字符编码:
alter table student default character set utf8;
(数据库中的utf-8用utf8表示)
3) 增加表的字段:
alter table student add class int(4);
(alter table 表名 add 字段名 数据类型)
4) 修改表字段数据类型:
alter table student modify class int(5);
(alter table 表名 modify 字段 新数据类型)
5) 删除表字段:
alter table student drop column class;
(alter table 表名 drop column 字段)
5. 查table:
1)查看指定数据库下所有的table:
show tables;
2) 查看指定table的表结构:
desc teacher; (desc table名字);
3)查看指定table中的所有数据:
select * from teacher; ( select * from table名字 )
4) 查看table的默认编码:
show create table teacher;
http://www.jb51.net/article/62768.htm#selectDatabase