Mysql的基本应用笔记1

MySQL的导入导出

导入sql脚本

方式一

mysql -u 用户名 -p 数据库名 < 存放位置

mysqljump -u root -p test < c:\a.sql

 

方式二

1.在命令行输入以下命令

>mysql -u root -p123

mysql>use test;

mysql> source c:/test.sql

 

show tables;查看有哪些表

desc tableName;查看表结构

 

2.在命令行输入以下命令

>mysql -u root -p123 test < c:/test.sql

 

导出sql脚本

mysqldump -u 用户名 -p 数据库名 > 存放位置

mysqljump -u root -p test > c:\a.sql

 

Mysql mysqldump用法导出表结构及表数据

命令行下具体用法如下: mysqldump -u用戶名 -p密码 -d 数据库名 表名 脚本名;

1、导出数据库为dbname的表结构(其中用戶名为root,密码为dbpasswd,生成的脚本名为db.sql

mysqldump -uroot -pdbpasswd -d dbname >db.sql;

2、导出数据库为dbname某张表(test)结构

mysqldump -uroot -pdbpasswd -d dbname test>db.sql;

3、导出数据库为dbname所有表结构及表数据(不加-d

mysqldump -uroot -pdbpasswd dbname >db.sql;

4、导出数据库为dbname某张表(test)结构及表数据(不加-d

mysqldump -uroot -pdbpasswd dbname test>db.sql;

 

多表操作

多表更新

#首先创建一张表

create temporary table test_music(

    id int(11) primary key,

    picUrl varchar(300)

);

select * from db_ayyc.test_music;

insert into test_music(id,picUrl) value('21','aaaaaaaa');

insert into test_music(id,picUrl) select id,picUrl from music where picUrl='none';

mysqldump -uroot -pdbpasswd -d db_ayyc test_music > ./test_music.sql;

source ./test_music.sql

update music m,test_music tm set m.picUrl=tm.picUrl where m.id=tm.id;

#注意:多表 UPDATE 不可以使用 ORDER BY LIMIT

 

多表删除

仅仅在 FROM USING 子句 之前 列出的表中的匹配记录行被删除。效果就是,你要以从多个表中同时删除记录行,并且同样可以有其它的表用于检索。 在表名后的 .* 仅仅是为了兼容 Access

DELETE t1,t2 FROM t1,t2,t3 WHERE t1.id=t2.id AND t2.id=t3.id

or

DELETE FROM t1,t2 USING t1,t2,t3 WHERE t1.id=t2.id AND t2.id=t3.id

 

在上面的情况下,我们仅仅从 t1 t2 表中删除匹配的记录行。如果一个 ORDER BY 子句被使用, 记录行将以指定的次序删除。这实际上只有连同 LIMIT 一起才有用。示例如下:

DELETE FROM somelog WHERE user = 'jcole' ORDER BY timestamp LIMIT 1

 

这将删除匹配 WHERE 子句的,并且最早被插入(通过 timestamp 来确定)的记录行。 DELETE 语句的LIMIT rows 选项是 MySQL 特有的,它告诉服务器在控制权被返回到客户端之前可被删除的最大记录行数目。这可以用来确保一个特定的 DELETE 命令不会占用太长的时间。你可以简单地重复使用 DELETE 命令,直到被影响的记录行数目小于 LIMIT 值。

MySQL 4.0 开始,在 DELETE 语句中可以指定多个表,用以从一个表中删除依赖于多表中的特殊情况的记录行。然而,在一个多表删除中,不能使用 ORDER BY LIMIT

 

联接查询

music表中找出上线的 非宜搜的 图库的图片地址值非空且不等于'none' 本库图片地址值不等于'none' 歌曲

select ma.* from

(select m1.* from music m1 where m1.musicStatus='1' and m1.type='0' and m1.nonEasouSongId <> '0' and m1.picUrl<>'none') ma

left join

(select m2.* from music m2 where m2.musicStatus='1' and m2.type='0' and m2.nonEasouSongId <> '0' and m2.picUrl<>'none' and m2.downPicUrl ='none') mb

using (downPicUrl)

where isnull(mb.downPicUrl);

 

music表中找出上线的 非宜搜的 图库的图片地址值不等于'none' 本库图片地址值不等于'none' 歌曲

select count(m1.id) from music m1 where musicStatus='1' and type='0' and nonEasouSongId <> '0'

and not exists (

select m2.* from music m2 where m2.musicStatus='1' and m2.type='0' and m2.nonEasouSongId <> '0' and (m2.picUrl='none' or m2.downPicUrl = 'none')

and m1.id=m2.id

)

 

真正执行查询的语句:

==================================================================

select count(*) totalCount from(

select m1.* from music m1 where musicStatus='1' and type='0' and nonEasouSongId <> '0'

and not exists (

select m2.* from music m2 where m2.musicStatus='1' and m2.type='0' and m2.nonEasouSongId <> '0' and (m2.picUrl='none' or m2.downPicUrl = 'none')

and m1.id=m2.id

) group by m1.musicName) mc;

 

select mc.* totalCount from(

select m1.* from music m1 where musicStatus='1' and type='0' and nonEasouSongId <> '0'

and not exists (

select m2.* from music m2 where m2.musicStatus='1' and m2.type='0' and m2.nonEasouSongId <> '0' and (m2.picUrl='none' or m2.downPicUrl = 'none')

and m1.id=m2.id

) group by m1.musicName) mc;

 

Mysql alter 语句的用法

mysql修改表名,列名,列类型,添加表列,删除表列

alter table test rename test1; --修改表名

 

alter table test add column name varchar(10); --添加表列

 

alter table test add column name varchar(10) after id;

alter table test modify column name varchar(10) after nickname; --修改列的排序

 

alter table test drop column name; --删除表列

 

alter table test modify address char(10) --修改表列类型

alter table test change address address char(40)

 

alter table test change column address address1 varchar(30)--修改表列名

 

mysql alter 对表字段的操作

//主键

alter table tabelname add new_field_id int(5) unsigned default 0 not null auto_increment ,add primary key (new_field_id);

 

//增加一个新列

alter table t2 add d timestamp;

alter table infos add ex tinyint not null default '0';

 

//删除列549830479

alter table t2 drop column c;

 

//重命名列

alter table t1 change a b integer;

 

//改变列的类型

alter table t1 change b b bigint not null;

alter table infos change list list tinyint not null default '0';

 

//重命名表

alter table t1 rename t2;

 

加索引

mysql> alter table tablename change depno depno int(5) not null;

mysql> alter table tablename add index 索引名 (字段名1[,字段名2 ]);

mysql> alter table tablename add index emp_name (name);

 

加主关键字的索引

mysql> alter table tablename add primary key(id);

 

加唯一限制条件的索引

mysql> alter table tablename add unique emp_name2(cardnumber);

 

删除某个索引

mysql>alter table tablename drop index emp_name;

 

修改原字段名称及类型:

mysql> ALTER TABLE table_name CHANGE old_field_name new_field_name field_type;

 

删除字段:

mysql> ALTER TABLE table_name DROP field_name;

 

linux安装mysql

方式一

参考地址:http://dev.mysql.com/doc/refman/5.5/en/linux-installation-native.html

Red Hat Linux, Fedora, CentOS

yum install mysql mysql-server mysql-libs mysql-server

service mysqld start

chkconfig --levels 235 mysqld on

 

Debian, Ubuntu, Kubuntu

apt-get install mysql-client-5.1 mysql-server-5.1

 

方式二

安装版本:mysql-5.5.8.tar.gz

1、安装 cmake 编译器。

1)、下载cmake

#cd /usr/local/src

#wget http://www.cmake.org/files/v2.8/cmake-2.8.3.tar.gz

 

2)、解压cmake

#tar -zvxf cmake-2.8.3.tar.gz

 

3)、配置编译

#cd cmake-2.8.3

#yum -y install gcc

#yum -y install gcc-c++

#./configure

#make

#make install

 

2、安装MySQL

1)、下载MySQL

#cd /usr/local/src

#wget http://mirrors.ircam.fr/pub/mysql/Downloads/MySQL-5.5/mysql-5.5.8.tar.gz

 

2)、添加必要的组和拥有者

#groupadd mysql

#useradd -r -g mysql mysql

 

3)、解压MySQL

#tar -zvxf mysql-5.5.8.tar.gz

 

4)、配置编译

#mkdir /usr/local/mysql

#mkdir /usr/local/mysql/data

#cd /usr/local/src/mysql-5.5.8

#cmake . \

-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \

-DINSTALL_DATADIR=/usr/local/mysql/data \

-DDEFAULT_CHARSET=utf8 \

-DDEFAULT_COLLATION=utf8_general_ci \

-DEXTRA_CHARSETS=all \

-DENABLED_LOCAL_INFILE=1

 

3、参数说明:

-DCMAKE_INSTALL_PREFIX=/usr/local/mysql //安装目录

-DINSTALL_DATADIR=/usr/local/mysql/data //数据库存放目录

-DDEFAULT_CHARSET=utf8     //使用utf8字符

-DDEFAULT_COLLATION=utf8_general_ci //校验字符

-DEXTRA_CHARSETS=all     //安装所有扩展字符集

-DENABLED_LOCAL_INFILE=1    //允许从本地导入数据

#make

#make install

注意事项:

重新编译时,需要清除旧的对象文件和缓存信息。

# make clean

# rm -f CMakeCache.txt

 

4、设置目录权限

# cd /usr/local/mysql

# chown -R root:mysql . //把当前目录中所有文件的所有者所有者设为root,所属组为mysql

# chown -R mysql:mysql data

 

5、配置文件

# cp support-files/my-medium.cnf /etc/my.cnf //这个配置仅适合小内存系统(32M - 64M)

打开如下注释:

innodb_data_home_dir = /usr/local/mysql/data

innodb_data_file_path = ibdata1:10M:autoextend

innodb_log_group_home_dir = /usr/local/mysql/data

 

innodb_buffer_pool_size = 16M

innodb_additional_mem_pool_size = 2M

 

innodb_log_file_size = 5M

innodb_log_buffer_size = 8M

innodb_flush_log_at_trx_commit = 1

innodb_lock_wait_timeout = 50

 

添加默认字符集:

[client]

default-character-set = utf8    // 添加编码支持

[mysqld]

default-character-set = utf8   // 添加编码支持

max_connections = 10000    //根据服务器性能调节

basedir = /usr/local/mysql //设置安装目录,这样在系统启动时才能正确运行到/etc/rc.d/init.d/mysql start

 

6、创建系统数据库的表

# cd /usr/local/mysql

# scripts/mysql_install_db --user=mysql

 

7、设置权限启动

设置环境变量:

# vi /root/.bash_profile

 

PATH=$PATH:$HOME/bin添加参数为:

PATH=$PATH:$HOME/bin:/usr/local/mysql/bin:/usr/local/mysql/lib

#source /root/.bash_profile

 

手动启动MySQL

# cd /usr/local/mysql

# ./bin/mysqld_safe --user=mysql &   //启动MySQL,但不能停止

 

启动日志写在此文件下:

/usr/local/mysql/data/localhost.err

 

关闭MySQL服务

# mysqladmin -u root -p shutdown  //这里MySQLroot用户还没有配置密码,所以为空值。

 

通过脚本启动MySQL

# ln -s /usr/local/mysql/support-files/mysql.server /usr/local/mysql

//必须注意,是放在mysql目录下,不是bin目录下

# cp /usr/local/mysql/support-files/mysql.server /usr/local/mysql  

# mysql.server start //启动mysql

# mysql.server stop //停止mysql

 

在引导时启动MySQL

# ln -s /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysql

# ln -s /usr/local/mysql/mysql.server /etc/rc.d/init.d/mysql

# cd /etc/rc.d/init.d

# chkconfig --add mysql       //配置是否自动启动, chkconfig --del mysql 可删除

# chmod +x /etc/rc.d/init.d/mysql //添加如执行权限

 

Linux运行级别:

分成了8种运行级别,其中常用7种。可在/etc/inittab文件中设置。

0 - halt

1 - Single user mode

2 - Multiuser, without NFS

3 - Full multiuser mode

4 - unused

5 - x11

6 - reboot

默认设置为:id:3:initdefault:

每一种动行级别都有自已独立的文件夹,例如:

/etc/rc.d/rc3.d 表示运行级别为3的配置都存放在这个文件侠中。

 

# chkconfig --list |grep mysql    //检查看是否设置为自启动

mysql 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭

表示: 运行级别2345都会自动启动mysql

 

另一种手动配置自已启动:

可能会出现这种情况,如果你试图在/etc/rc.d/rc3.d目录下运行../init.d/mysql start可能会收到如下错误

Starting MySQLCouldn't find MySQL server (./bin/mysqld_safe[失败]

 

可见mysql.server内部引用了一个相对路径./bin/mysqld_safe,所以这样就导致失败。

这样我们可以直接在rc.local文件中添加启动脚本:

# chkconfig --del mysql

# cd /etc/rc.d

# vi rc.local //添加: /usr/local/mysql/bin/mysqld_safe --user=mysql &

解决办法:在/etc/my.cnf 配置文件中添加:

 

basedir = /usr/local/mysql

 

8、修改MySQLroot用户的密码:

# mysql -u root mysql

 

mysql>use mysql;

mysql>desc user;

mysql>update user set Password = password('xxxxxx') where User='root';

mysql>select Host,User,Password from user where User='root';

mysql>flush privileges;

mysql>exit

 

重新登录:mysql -u root -p

 

9、添加软链接

# ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql

# ln -s /usr/local/mysql/include/mysql /usr/include/mysql

 

注意事项:

<1>MySQL5.5 默认使用InnoDB作为存储引擎,所以可以不设置DWITH_MYISAM_STORAGE_ENGINE

 

MySQL数据库的管理

1.直接在数据库中修改记录

mysql> use mysql

mysql> update user set password = password("new_password") where user = "user_name";

mysql> flush privileges;

其实这种方法就是更新一条数据库记录,与普通update语句不同的是,密码加密存储,需用password()函数来生成,另一个不同点是需要刷新权限表。

 

2.在数据库中运行set password

mysql> set password for user_name = password("new_password");

mysql> flush privileges;

同第一种方法,也要刷新权限表

 

3.直接在shell环境运行mysqladmin

> mysqladmin -u user_name -p password "new_password"

> mysqladmin flush-privileges

格式:mysqladmin -u用户名 -p旧密码 password 新密码

示例:给root加个密码123456

键入以下命令

[root@test1 local]# /usr/bin/mysqladmin -u root password 123456

注:因为开始时root没有密码,所以-p旧密码一项就可以省略了。

 

4.授权指定表予以指定用户

grant all privileges on db.table to user_name@localhost identified by "your_pwd";

用户名密码的生效不必用flush privileges刷新

注:

db.table: db表示授权哪个库,table是相应库里的表。可以用*.*表示所有库所有表。注意,如果想表示某个库的所有表,必须用db_name.*,后面的".*"不可省略,否则权限将无法赋予。

user_name@localhost: user_name表示用户名,localhost表示该用户只能在本地访问该库,可以用%表示从任何地方访问该库,也可以用111.11.22.33来表示地址

your_pwd: 给用户设置的密码

 

5.mysql授权给其它机子访问

授予所有机器:grant all on *.* to root@'%' identified by '123';

授予指定机器权限:grant all on *.* to root@'192.168.1.88' identified by '123';

授予级连权限:grant all privileges on *.* to root@'%' identified by "123" with grant option;

取消指定机器的权利:drop user root@'192.168.1.88';

 

给指定用户授指定数据户的权限

CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;

CREATE USER 'sonar' IDENTIFIED BY 'sonar';

GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar';

GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar';

FLUSH PRIVILEGES;

 

6.设置编码

修改/etc/my.cnf my.ini

[client]

default-character-set=utf8

 

[mysqld]

character_set_server=utf8

default-character-set=utf8

参考链接:http://www.busfly.net/post/58.html

 

原文地址:https://www.cnblogs.com/luowei010101/p/2669341.html