mysql配置小全
2009-05-11 16:39
以mysql-noinstall-5.1.6(win32)为例
1>把压缩文件mysql-noinstall-5.1.6-alpha-win32.zip解压到一个目录下,即安装目录,通常为mysql
2>创建my.ini配置文件,内容如下:
[mysqld]
#设置basedir指向mysql的安装路径
basedir=...\mysql
datadir=...\mysql\data
my.ini文件放在系统文件下面
XP系统在C:\windows目录下,2000系统在C:\winnt下
3>启动与停止mysql
启动:net start mysql
停止:net stop mysql
进入sql :mysql -uroot -p
4>连接mysql
格式: mysql -h主机地址 -u用户名 -p用户密码
例1:连接到本机上的mysql
net start mysql例2:连接到远程主机上的mysql
mysql -h110.110.110.110 -uroot -pabcd123
5>退出mysql命令
exit(回车)
或者
quit(回车)
6>修改密码
格式:mysqladmin -u用户名 -p旧密码 password 新密码
例:给root加个密码ab12。首先在DOS下进入目录mysqlbin,然后键入以下命令
mysqladmin -uroot password ab12
注:因为开始时root没有密码,所以-p旧密码一项就可以省略了。
2、 例2:再将root的密码改为djg345。
mysqladmin -uroot -pab12 password djg345
下面的是mysql环境中的命令, 以分号作为命令结束符
7>增加新用户
格式:grant select on 数据库.* to 用户名@登录主机 identified by "密码";
例1、增加一个用户test1密码为abc,让他可以在任何主机上登录, 并对所有数据库有
查询、插入、修改、删除的权限。首先用以root用户连入mysql,然后键入以下命令:
grant select,insert,update,delete on *.* to test1@"%" Identified by "abc";
例2、增加一个用户test2密码为abc,让其只可以在localhost上登录, 并可以对数据库
mydb进行查询、插入、修改、删除的操作(localhost指本地主机, 即mysql数据
库所在的那台主机),这样用户即使用知道test2的密码,也无法从internet上直
接访问数据库,只能通过mysql主机上的web页来访问了。
grant select,insert,update,delete on mydb.* to test2@localhost identified by "abc";
8>显示数据库列表
show databases;
9>显示库中的数据表
use mysql;//数据库名称
show tables;
10>显示数据表的结构
describe 表名;
11>建库
create database 库名;
12>建表
use 库名;
create table 表名(字段设定列表);
13>删库和删表
drop database 库名;
drop table 表名;
14>将表中记录清空
delete from 表名;
15>显示表中的记录
select * from 表名;
例:
drop database if exists school; //如果存在SCHOOL则删除
create database school; //建立库SCHOOL
use school; //打开库SCHOOL
create table teacher //建立表TEACHER
(
id int(3) auto_increment not null primary key,
name char(10) not null,
address varchar(50) default '深圳',
year date
); //建表结束
//以下为插入字段
insert into teacher values('','glchengang','深圳一中','1976-10-10');
insert into teacher values('','jack','深圳一中','1975-12-23');
注:在建表中
(1) 将ID设为长度为3的数字字段:int(3),并让它每个记录自动加一: auto_increment,
并不能为空:not null,而且让它成为主字段primary key
(2) 将NAME设为长度为10的字符字段
(3) 将ADDRESS设为长度50的字符字段,而且缺省值为深圳。varchar和char有什么区别
呢,只有等以后的文章再说了。
(4) 将YEAR设为日期字段。
如果你在mysql提示符键入上面的命令也可以,但不方便调试。 你可以将以上命令
原样写入一个文本文件中假设为school.sql,然后复制到c:下,并在DOS状态进入目录
mysql in,然后键入以下命令:
mysql -uroot -p密码 < c:school.sql
如果成功,空出一行无任何显示;如有错误,会有提示。(以上命令已经调试,你
只要将//的注释去掉即可使用)。
16>将文本数据转到数据库中
1、 文本数据应符合的格式:字段数据之间用tab键隔开,null值用来代替。例:
3 rose 深圳二中 1976-10-10
4 mike 深圳一中 1975-12-23
2、 数据传入命令load data local infile "文件名" into table 表名;
注意:你最好将文件复制到mysql in目录下,并且要先用use命令选表所在的库。
17>导出和导入数据
1、导出表
mysqldump --opt school > school.sql
注释:将数据库school中的表全部备份到school.sql文件,school.sql是一个文本文件,
文件名任取,打开看看你会有新发现。
mysqldump --opt school teacher student > school.teacher.student.sql
注释:将数据库school中的teacher表和student表备份到school.teacher.student.sql文
件,school.teacher.student.sql是一个文本文件,文件名任取,打开看看你会有新发现。
2、导入表
mysql
mysql>create database school;
mysql>use school;
mysql>source school.sql;
(或将school.sql换为school.teacher.sql / school.teacher.student.sql)
3、导出数据库
mysqldump --databases db1 db2 > db1.db2.sql
注释:将数据库dbl和db2备份到db1.db2.sql文件,db1.db2.sql是一个文本文件,文件名
任取,打开看看你会有新发现。
(举个例子:
mysqldump -h host -u user -p pass --databases dbname > file.dump
就是把host上的以名字user,口令pass的数据库dbname导入到文件file.dump中。)
4、导入数据库
mysql < db1.db2.sql
5、复制数据库
mysqldump --all-databases > all-databases.sql
注释:将所有数据库备份到all-databases.sql文件,all-databases.sql是一个文本文件,
文件名任取。
6、导入数据库
mysql
mysql>drop database a;
mysql>drop database b;
mysql>drop database c;
...
mysql>source all-databases.sql; (或exit退出mysql后 mysql < all-databases.sql)
18>创建一个可以从任何地方连接服务器的一个完全的超级用户,但是必须使用一个口令something做这个
GRANT ALL PRIVILEGES ON *.* TO monty@"%" IDENTIFIED BY 'something' WITH GRANT OPTION;
19>删除授权
REVOKE ALL PRIVILEGES ON *.* FROM root@"%";
USE mysql;
DELETE FROM user WHERE User="root" and Host="%";
FLUSH PRIVILEGES;
20>创建一个用户custom在特定客户端weiqiong.com登录,可访问特定数据库bankaccount
mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP ON bankaccount.*
TO custom@weiqiong.com IDENTIFIED BY 'stupid';
21>重命名表
ALTER TABLE t1 RENAME t2;
22>改变列
为了改变列a,从INTEGER改为TINYINT NOT NULL(名字一样),
并且改变列b,从CHAR(10)改为CHAR(20),同时重命名它,从b改为c:
ALTER TABLE t2 MODIFY a TINYINT NOT NULL, CHANGE b c CHAR(20);
23>增加列
增加一个新TIMESTAMP列,名为d:
ALTER TABLE t2 ADD d TIMESTAMP;
24>在列d上增加一个索引,并且使列a为主键
ALTER TABLE t2 ADD INDEX (d), ADD PRIMARY KEY (a);
25>删除列
ALTER TABLE t2 DROP COLUMN c;
26>删除记录
DELETE FROM t1 WHERE C>10;
27>改变某几行
UPDATE t1 SET user=weiqiong,password=weiqiong;
28>创建索引
使用name列的头10个字符创建一个索引:
CREATE INDEX part_of_name ON customer (name(10));
另外推荐一个中文兼容比较好的图形界面工具 navicat。
my.ini 配置
# Example MySQL config file for very large systems.
#
# This is for a large system with memory of 1G-2G where the system runs mainly
# MySQL.
#
# You can copy this file to
# /etc/my.cnf to set global options,
# mysql-data-dir/my.cnf to set server-specific options (in this
# installation this directory is C:\mysql\data) or
# ~/.my.cnf to set user-specific options.
#
# In this file, you can use all long options that a program supports.
# If you want to know which options a program supports, run the program
# with the "--help" option.
# The following options will be passed to all MySQL clients
[client]
default-character-set = utf8
#password= acmteam
port= 3306
socket= /tmp/mysql.sock
# Here follows entries for some specific programs
# The MySQL server
[mysqld]
default-character-set = utf8
basedir = E:\mysql-5.1.42-win32
datadir = E:\mysql-5.1.42-win32/data
default-storage-engine = MyISAM
port= 3306
socket= /tmp/mysql.sock
skip-locking
key_buffer_size = 384M
max_allowed_packet = 1M
table_open_cache = 512
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size = 32M
# Try number of CPU's*2 for thread_concurrency
thread_concurrency = 8
# Don't listen on a TCP/IP port at all. This can be a security enhancement,
# if all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# Note that using this option without enabling named pipes on Windows
# (via the "enable-named-pipe" option) will render mysqld useless!
#
#skip-networking
# Replication Master Server (default)
# binary logging is required for replication
log-bin=mysql-bin
# required unique id between 1 and 2^32 - 1
# defaults to 1 if master-host is not set
# but will not function as a master if omitted
server-id= 1
# Replication Slave (comment out master section to use this)
#
# To configure this host as a replication slave, you can choose between
# two methods :
#
# 1) Use the CHANGE MASTER TO command (fully described in our manual) -
# the syntax is:
#
# CHANGE MASTER TO MASTER_HOST=<host>, MASTER_PORT=<port>,
# MASTER_USER=<user>, MASTER_PASSWORD=<password> ;
#
# where you replace <host>, <user>, <password> by quoted strings and
# <port> by the master's port number (3306 by default).
#
# Example:
#
# CHANGE MASTER TO MASTER_HOST='125.564.12.1', MASTER_PORT=3306,
# MASTER_USER='joe', MASTER_PASSWORD='secret';
#
# OR
#
# 2) Set the variables below. However, in case you choose this method, then
# start replication for the first time (even unsuccessfully, for example
# if you mistyped the password in master-password and the slave fails to
# connect), the slave will create a master.info file, and any later
# change in this file to the variables' values below will be ignored and
# overridden by the content of the master.info file, unless you shutdown
# the slave server, delete master.info and restart the slaver server.
# For that reason, you may want to leave the lines below untouched
# (commented) and instead use CHANGE MASTER TO (see above)
#
# required unique id between 2 and 2^32 - 1
# (and different from the master)
# defaults to 2 if master-host is set
# but will not function as a slave if omitted
#server-id = 2
#
# The replication master for this slave - required
#master-host = <hostname>
#
# The username the slave will use for authentication when connecting
# to the master - required
#master-user = <username>
#
# The password the slave will authenticate with when connecting to
# the master - required
#master-password = <password>
#
# The port the master is listening on.
# optional - defaults to 3306
#master-port = <port>
#
# binary logging - not required for slaves, but recommended
#log-bin=mysql-bin
#
# binary logging format - mixed recommended
#binlog_format=mixed
# Point the following paths to different dedicated disks
#tmpdir= /tmp/
#log-update = /path-to-dedicated-directory/hostname
# Uncomment the following if you are using InnoDB tables
#innodb_data_home_dir = C:\mysql\data/
#innodb_data_file_path = ibdata1:2000M;ibdata2:10M:autoextend
#innodb_log_group_home_dir = C:\mysql\data/
# You can set .._buffer_pool_size up to 50 - 80 %
# of RAM but beware of setting memory usage too high
#innodb_buffer_pool_size = 384M
#innodb_additional_mem_pool_size = 20M
# Set .._log_file_size to 25 % of buffer pool size
#innodb_log_file_size = 100M
#innodb_log_buffer_size = 8M
#innodb_flush_log_at_trx_commit = 1
#innodb_lock_wait_timeout = 50
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
#safe-updates
[myisamchk]
key_buffer_size = 256M
sort_buffer_size = 256M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout