MySQL运用实战


1,登录服务器:

mysql -hIP -u用户名 -p密码;

本地服务器的话-hIP可以省略,用户名和密码间可以有空格,但-p后面不输入密码,安全的,一般的写法如下

mysql -uroot -p;

2,数据库文件在datadir的路径下,更改的话要重启服务,数据库对应的文件夹下有几个文件,.opt文件时配置说明文件,.frm是表结构文件。移植数据如果直接拷贝此文件夹的话,在不同的版本中会有兼容性的问题。因此一般写好sql脚本,在执行sql脚本即可。

执行外部sql脚本文件命令,例如:source d:\test.sql;

生成sql脚本本件可以用phpmyadmin导出功能。

3,建库建表语句

drop database if exists test; --判断
create database test;
use test;    --切换,经常忘呵呵
drop table if exists user; --判断
create table user(
id int auto_increment not null primary key,
name varchar(20) not null,
pwd varchar(20) not null default '上海'
);

describe user; --显示表结构
insert into user values(null,'a1',default);  --插入带有默认值的数据,自动生成列值为null

4,修改数据库root用户密码

use mysql;  --切换到mysql数据库

update user set password=password('新密码') where user='root';

然后重启服务即可。

5,取得当前时间

date_format(now(),'%Y-%m-%d %h:%m:%s');

相关内容