MySQL相关概念:MySQL是单进程多线程接收应用的请求。
SQL/MySQL
1.事务,隔离,并发控制,锁
2.用户和权限
3.监控
STATUS
4.索引类型:查询
VARIABLES
5.备份和恢复
6.复制功能
7.集群
DML:数据操作语言
INSERT:插入
DELETE:删除
SELECT:挑选,选择,查询
UPDATE:更新,修改
DDL:数据定义语言
CREATE:创建
DROP:删除
ALTER:修改
DCL:数据控制语言
GRANT:授权
REVOKE:取消权限
MySQL插件式存储引擎:
MyISAM:不支持事务处理,应用于查询比较多,但修改比较少的数据存储仓库
该引擎中,每个表有三个文件:
.frm: 表结构文件
.MYD: 表数据文件
.MYI: 表索引文件
InnoDB:支持事务处理,查询没有那么快,应用于经常需要修改的,比如论坛数据存储
该引擎所以表共享一个表空间文件;
建议:每表一个独立的表空间文件;
.frm:表结构文件
.ibd:表空间(包含表数据和表索引文件)
为mysql打开innodb每表建立一个独立表空间文件的设置:
编辑配置文件/etc/my.cnf添加一行:
vim /etc/my.cnf
....
innoda_file_per_table = 1 #1为启用,0为禁用
MYSQL数据库功能:
1、数据库创建、删除
2、创建表、删除表、修改表
3、索引的创建、删除
4、用户和权限
5、数据增、删、改
6、查询
MySQL二进制程序:(mysql数据库的database存放路径为/var/lib/mysql目录中)
客户端命令:mysql
参数:
-u USERNAME #以那个用户连接mysql (不指定默认用户为root)
-p #指定输入用户密码 (不指定默认是空密码)
-h MYSQL_SERVER #指定mysql服务器主机 (不指定默认为localhost本机)
-D DATA #连接mysql的时候指定默认的数据库
Usage: mysql -D mydb -uroot -p -h 10.109.134.249 -uroot 指定用户
-p:输入用户密码 -h:指定连接那台mysql服务器(ip地址)
-D: 指定登录mysql默认的数据库(相当于在客户端内输入USE mydb;命令)
MySQL客户端:
交互式模式:一步一步的手动输入执行
批处理模式:执行mysql脚本,批处理执行
* MySQL客户端工具:
mysql
mysqldump:常见的mysql备份工具
mysqladmin:mysql管理工具
mysqladmin extended-status:显示状态变量
mysqladmin status
--sleep N:显示频率
--count N:显示多个状态
mysqladmin variables: 显示服务器变量;
mysqladmin flush-privileges:让mysqld重读授权表,等同于reload;
mysqladmin flush-status:重置大多数服务器状态变量;
mysqladmin flush-logs:二进制和中继日志滚动;
mysqladmin flush-hosts:刷新主机列表;
mysqladmin refresh:相当于同时执行flush-hosts和flush-logs;
mysqladmin shutdown: 关闭mysql服务器进程;
mysqladmin version: 服务器版本及当前状态信息;
mysqladmin start-slave:启动复制,启动从服务器复制线程;
mysqladmin stop-slave: 关闭复制,关闭从服务器复制线程。
格式:mysqladmin [option] command [arg] [command [arg]] ...
Usage:mysqladmin -uroot -p password ‘NEW_PASS‘
[[email protected] ~]# mysqladmin create hellodb;
[[email protected] ~]# mysql
.........
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hellodb | #通过mysqladmin可以直接管理mysql
| mydb |
| mysql |
| performance_schema |
| test |
| testdb |
+--------------------+
7 rows in set (0.00 sec)
mysqlcheck:检查工具
mysqlimport:接口工具
MySQL非客户端工具:
myisamchk
myisampack
交互式模式中的命令类别:
客户端命令:?(获取帮助) quit(退出mysql客户端)
\c:提前终止语句执行,但是必须在结束符前,否则该条指令依旧执行。
\g:无论语句结束符是什么,直接将此语句送至服务器端执行;
\G:无论语句结束符是什么,直接将此语句送至服务器端执行,而且结果以竖排显示;
\! COMMAND:执行shell命令;
\W:语句执行结束后显示警告信息;
服务器端命令:都必须使用语句结束符,默认结束符为分号;SHOW DATABASES; #查看数据库
\d:定义语句结束符 (默认语句结束符为分号,可以设定为其他)
help KEYWORD:获取关键字命令的帮助信息;
mysql>help SELECT; #获取SELECT命令的相关帮助信息;
Name: ‘SELECT‘
Description:
Syntax:
SELECT
在mysql客户端中,名称补全命令: rehash
修改/etc/my.cnf配置文档,使得命令补全永久有效;
mysql> rehash #输入命令rehash以后可以直接通过tab键补全命令 (重新打开mysql失效,需重新设置)
mysql> U 输入一个大U然后按tab键就有很多以U开头的命令出来
UCASE UNION UPDATE USING
UNCOMMITTED UNIQUE UPGRADE UTC_DATE
UNCOMPRESS UNIQUE_USERS UPPER UTC_TIME
UNCOMPRESSED_LENGTH UNIX_TIMESTAMP USAGE UTC_TIMESTAMP
UNDEFINED UNKNOWN USE UUID
UNDO UNLOCK USER
UNHEX UNSIGNED USER_RESOURCES
UNICODE UNTIL USE_FRM
关系数据库对象:库、表、索引、视图、约束、存储过程、存储函数、触发器、游标、
用户、权限、事务。
数据库最基本的对象-->表:行(row),列(field,column)
服务端命名:mysqld
Mysql常用命令不区分大小写,但是需保持一致(都大写或者都小写):
** mysql > help COMMOND #可以通过help+命令来获取命令帮助。
-----------------------------------------------------------
Usage:
mysql> help CREATE TABLE #获取命令帮助
Name: ‘CREATE TABLE‘
Description:
Syntax:
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
(create_definition,...)
[table_option] ...
Or:
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
[(create_definition,...)]
[table_option] ...
select_statement
Or:
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
{ LIKE old_tbl_name | (LIKE old_tbl_name) }
-----------------------------------------------------------------
DDL:定义数据对象,关键字:CREATE(创建)、ALTER(修改)、DROP(删除) * 这些命令主要作用的范围:数据库,表,表的字段 创建用户: CREATE USER ‘USERNAME‘@‘HOST‘ IDENTIFIED BY ‘PASSWORD‘; #可以不指定密码 USERNAME:用户名 HOST:主机 IDENTIFIED:指定密码 BY‘密码’ 删除用户: DROP USER ‘USERNAME‘@‘HOST‘; 删除用户(需具体指定删除那个用户) 为用户设定密码方法:(建议用1和3这两种方法设定或修改密码) *1、mysql>SET PASSWORD FOR ‘USERNAME‘@‘HOST‘=PASSWORD(‘PASSWORD‘) Usage: SET PASSWORD FOR ‘root‘@‘localhost‘=PASSWORD(‘123456‘) #设定 [email protected]用户名密码为123456 2、直接在linux下输入,不需要进入mysql客户端输入(此方法容易导致修改相同用户的密码): # mysqladmin -uUSERNAME -hHOST -p password ‘password‘ #设定用户USERNAME 主机HOST 密码为password Usage: [[email protected] ~]#mysqladmin -uroot -h10.109.134.249 -p password ‘123456‘ #设定10.109.134.249主机的root用户密码为123456 *3、直接修改表中的内容:
mysql>UPDATE user SET password=PASSWORD(‘password‘) WHERE USER=‘root‘ AND Host=‘10.109.134.249‘; #设定 主机10.109.134.249的数据库root用户密码 AND与关系(需同时满足) OR或关系(两者满足一个即可) NOT非关系(取反)
HOST可以使用的格式: IP , HOSTNAME(主机名),NETWORK(网络地址),通配符(需用‘通配符‘引号) 通配符包含: _:匹配任意单个字符,172.16.0._ %:匹配任意字符 Usage: 创建数据库:CREATE DATABASE IF NOT EXISTS db_name; 如果数据库不存在 创建db_name数据库 创建表:CREATE TABLE tb_name(col1,col2,...); 创建tb_name表 col表示字段 查看库中的表:SHOW TABLES FROM db_name 查看db_name数据库中的表 查看表的结构:DESC tb_name 查看tb_name表的结构 删除表:DROP TABLE IF EXISTS tb_name 如果表存在删除tb_name表 修改表:ALTER TABLE tb_name
MODIFY:修改某字段(变更属性,权限) CHANGE: 改变某字段 (变更字段名称) ADD:增加某字段 DROP : 删除某字段 Usage: ALTER TABLE students ADD course VARCHAR(100); #对表students新增course字段且VARCHAR(100)空间长度为100 DML:数据操纵语言,关键字:INSERT(插入)、DELETE(删除)、UPDATE(更新、修改) * 这些命令主要作用的范围:针对表中字段的值进行操作 插入数据: INSERT INTO tb_name (col1,col2,...) VALUES|VALUE (‘STRING‘,NUM,...), (‘STRING‘,NUM,...); col表示字段 更新、修改数据: UPDATE tb_name SET column=value WHERE ..... ; WHERE表示修改那个 具体数据,并不是把column整个这个字段的值修改为value. Usage: UPDATE students SET Course=‘Jiuyinzhenjing‘ WHERE Name=‘LHC‘; #表示修改表students中Name值为LHC的Course字段的值变为‘Jiuyinzhenjing’ DCL:数据库控制语言 ,关键字:GRANT(允许、授权)、REVOKE(取消权限) ALL PRIVILEGES:表示所有权限 GRANT给用户授权: Usage: GRANT pri1,pri2,... ON DB_NAME.TB_NAME TO ‘USERNAME‘@‘HOST‘ IDENTIFIED BY ‘PASSWORD‘ #给DB_NAME数据库中的TB_NAME表中的[email protected]‘HOST‘用户授予pri1,pri2等权限也可以设定密码IDENTIFIED BY ‘PASSWORD‘(如果该用户不存在,会直接新建该用户并授权) REVOKE取消用户权限: Usage: REVOKE pri1,pri2,... ON DB_NAME.TB_NAME FROM ‘USERNAME‘@‘HOST‘; #取消DB_NAME数据库中的TB_NAME表中的‘USERNAME‘@‘HOST‘用户的pri1,pri2等权限
查看用户的授权: SHOW GRANTS FOR ‘USERNAME‘@‘HOST‘;
刷新用户权限列表: FLUSH PRIVILEGES;
DQL:数据库查询语言,关键字:SELECT(挑选,选择)
SELECT 字段 FROM tb_name WHERE CONDITION; *:表示所有字段 WHERE:没有条件表示显示所有行;
Usage: SELECT Name,Course FROM students WHERE Gender=‘M‘; #表示只选择表students中的Gender字段值为M的Name和Course字段的信息
一、编译安装前准备
安装前需确认开发环境组是否安装:Development Tools(开发工具)和Development Libraries(开发库)
[[email protected] ~]# yum groupinstall "Development Tools"
Loaded plugins: refresh-packagekit, rhnplugin
This system is not registered with RHN.
RHN support will be disabled.
Setting up Group Process
[[email protected] ~]# yum groupinstall "Development Libraries"
** 由于mysql数据库随着时间的增长,数据会越来越大,所以应该把数据库数据放置在一个单独的可扩展的分区卷上,以便后期管理和备份,挂载在逻辑卷是比较好的方法如下:
[[email protected] ~]# fdisk /dev/sdb #新建分区
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xef7e9b34.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won‘t be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
WARNING: DOS-compatible mode is deprecated. It‘s strongly recommended to
switch off the mode (command ‘c‘) and change display units to
sectors (command ‘u‘).
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xef7e9b34
Device Boot Start End Blocks Id System
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-2610, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +10G
Command (m for help): T
Selected partition 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xef7e9b34
Device Boot Start End Blocks Id System
/dev/sdb1 1 1306 10490413+ 8e Linux LVM
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[[email protected] ~]# partprobe /dev/sdb #通知内核重读sdb分区
[[email protected] ~]# fdisk -l #查看分区
Disk /dev/sda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000149e0
Device Boot Start End Blocks Id System
/dev/sda1 * 1 66 524288 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 66 1341 10240000 83 Linux
/dev/sda3 1341 2106 6144000 83 Linux
/dev/sda4 2106 3917 14547968 5 Extended
/dev/sda5 2106 2361 2048000 83 Linux
/dev/sda6 2361 2552 1536000 82 Linux swap / Solaris
/dev/sda7 2552 3917 10960896 83 Linux
Disk /dev/sdb: 64.4 GB, 64424509440 bytes
255 heads, 63 sectors/track, 7832 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0002d296
Device Boot Start End Blocks Id System
/dev/sdb1 1 2612 20980858+ 8e Linux LVM
[[email protected] ~]# pvcreate /dev/sdb1 #先把/dev/sdb1分区建立一个pv物理卷
Physical volume "/dev/sdb1" successfully created
[[email protected] ~]# pvs #查看物理卷
PV VG Fmt Attr PSize PFree
/dev/sdb1 lvm2 a- 10.00g 10.00g
[[email protected] ~]# vgcreate myvg /dev/sdb1 #以/dev/sdb1分区创建myvg卷组
Volume group "myvg" successfully created
[[email protected] ~]# vgs #查看卷组
VG #PV #LV #SN Attr VSize VFree
myvg 1 0 0 wz--n- 10.00g 10.00g
[[email protected] ~]# lvcreate -n mydata -L 10G myvg #在卷组myvg中建立一个大小为10G,
名称为mydata 的逻辑卷
Logical volume "mydata" created
[[email protected] ~]# lvs #查看逻辑卷
LV VG Attr LSize Origin Snap% Move Log Copy% Convert
mydata myvg -wi-a- 5.00g
[[email protected] ~]# mke2fs -t ext4 /dev/myvg/mydata #格式化mydata逻辑卷
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=1342177280
40 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736
正在写入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
[[email protected] ~]# mkdir /mydata #建立一个目录作为挂载点
[[email protected] ~]# vim /etc/fstab #设定逻辑卷开机自动挂载
# /etc/fstab
# Created by anaconda on Tue Feb 14 01:11:01 2017
#
# Accessible filesystems, by reference, are maintained under ‘/dev/disk‘
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
/dev/myvg/mydata /mydata ext4 defaults 0 0 #新增该行内容
[[email protected] ~]# mount -a #重读/etc/fstab硬盘挂载文件,使得新增的分区挂载成功
[[email protected] ~]# mount #查看已经挂载的分区
/dev/sda2 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
/dev/sda5 on /home type ext4 (rw)
/dev/sda3 on /usr/local type ext4 (rw)
/dev/sda6 on /var type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
/dev/mapper/myvg-mydata on /mydata type ext4 (rw) #逻辑卷mydata已经挂载到/mydata目录
[[email protected] ~]# mkdir /mydata/data
[[email protected] ~]# ll /mydata
总用量 20
drwxr-xr-x. 2 root root 4096 2月 17 17:30 data
drwx------. 2 root root 16384 2月 17 15:11 lost+found
[[email protected] ~]# groupadd -r mysql #-r建立一个系统组
groupadd: group ‘mysql‘ already exists
[[email protected] ~]# useradd -r -g mysql -s /sbin/nologin mysql #建立一个系统用户不能登录
useradd: user ‘mysql‘ already exists
[[email protected] ~]# id mysql #查看用户信息
uid=27(mysql) gid=27(mysql) groups=27(mysql)
[[email protected] ~]# chown -R mysql.mysql /mydata/data/ #更改文件夹属主和属组-R递归
[[email protected] ~]# ll /mydata
总用量 20
drwxr-xr-x. 2 mysql mysql 4096 2月 17 17:30 data
drwx------. 2 root root 16384 2月 17 15:11 lost+found
[[email protected] ~]# chmod o-rx /mydata/data/ #删除/data组其他人的读和执行权限
[[email protected] ~]# ll /mydata
总用量 20
drwxr-x---. 2 mysql mysql 4096 2月 17 17:30 data
drwx------. 2 root root 16384 2月 17 15:11 lost+found
二、编译安装cmake-2.8.8.tar.gz:
[[email protected] ~]# tar vxf cmake-2.8.8.tar.gz
[[email protected] ~]# cd cmake-2.8.8
[[email protected] cmake-2.8.8]# ./configure
[[email protected] cmake-2.8.8]# make && make install
...........
-- Installing: /usr/local/doc/cmake-2.8/ccmake.docbook
-- Installing: /usr/local/share/aclocal/cmake.m4 #编译安装cmake完成
三、使用cmake编译安装mysql5.5.28:
清理此前的编译所生成的文件,则需使用如下命令:make clean
[[email protected] ~]# tar vxf mysql-5.5.28.tar.gz
[[email protected] ~]# cd mysql-5.5.28
[[email protected] mysql-5.5.28]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/mydata/data
-DSYSCONFDIR=/etc -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DWITH_SSL=system -DWITH_ZLIB=system
-DWITH_LIBWRAP=0 -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8
-DDEFAULT_COLLATION=utf8_general_ci #-DCMKE_INSTALL_PREFIX=安装路径,-DMYSQL_DATADIR=数据存放路径,-DSYSCONFDIR=配置文件路径,-DWITH_INNOBASE_STORAGE_ENGINE=1(是否包含INNOBASE引擎1表示包含0表示不包含),-DWITH_ARCHIVE_STORAGE_ENGINE=1(是否包含ARCHIVE引擎1表示包含0表示不包含),
-DWITH_BLACKHOLE_STORAGE_ENGINE=1(是否包含BLACKHOLE空洞引擎1表示包含0表示不包含),
.........
-- Performing Test HAVE_PEERCRED - Success
-- Configuring done
-- Generating done
-- Build files have been written to: /root/mysql-5.5.28 #编译完成
[[email protected] mysql-5.5.28]# make
.........
Linking CXX static library libsql.a
[ 98%] Built target sql
Scanning dependencies of target mysqld
[ 99%] Building CXX object sql/CMakeFiles/mysqld.dir/main.cc.o
Linking CXX executable mysqld
[ 99%] Built target mysqld
Scanning dependencies of target udf_example
[ 99%] Building C object sql/CMakeFiles/udf_example.dir/udf_example.c.o
Linking C shared module udf_example.so
[ 99%] Built target udf_example
Scanning dependencies of target my_safe_process
[100%] Building CXX object mysql-test/lib/My/SafeProcess/CMakeFiles/my_safe_process.dir/safe_process.cc.o
Linking CXX executable my_safe_process
[100%] Built target my_safe_process #make完成,执行make install
[[email protected] mysql-5.5.28]# make install
.......
-- Installing: /usr/local/mysql/man/man1/mysql.1
-- Installing: /usr/local/mysql/man/man1/mysql-test-run.pl.1
-- Installing: /usr/local/mysql/man/man8/mysqld.8 #安装mysql完成
四、配置mysql脚本让其开机自动启动及运行:
[[email protected] mysql-5.5.28]# chown -R :mysql /usr/local/mysql #更改组为mysql
[[email protected] mysql-5.5.28]# cd /usr/local/mysql
[[email protected] mysql]# scripts/mysql_install_db --user=mysql --datadir=/mydata/data #初始化mysql,指定用户和数据目录
Installing MySQL system tables...
OK
Filling help tables...
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
./bin/mysqladmin -u root password ‘new-password‘
./bin/mysqladmin -u root -h lamp password ‘new-password‘
Alternatively you can run:
./bin/mysql_secure_installation
which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the manual for more instructions.
You can start the MySQL daemon with:
cd . ; ./bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd ./mysql-test ; perl mysql-test-run.pl
Please report any problems with the ./bin/mysqlbug script! #初始化完成
[[email protected] mysql]# mv /etc/my.cnf /etc/my.cnf.back #把原系统中配置文档改名
[[email protected] mysql]# cp support-files/my-large.cnf /etc/my.cnf #复制配置文档至/etc目录
[[email protected] mysql]# cp support-files/mysql.server /etc/init.d/mysqld #复制启动脚本至/etc/init.d目录中
[[email protected] mysql]# chkconfig --add mysqld #把mysqld启动脚本加到开机启动列表
[[email protected] mysql]# chkconfig --list mysqld #查看mysqld脚本是否开机启动
mysqld 0:off1:off2:on3:on4:on5:on6:off
[[email protected] mysql]# service mysqld start #启动mysql脚本
Starting MySQL... [ OK ]
[[email protected] mysql]# vim /etc/profile.d/mysql.sh #编辑环境变量使得mysql命令在PATH变量中
export PATH=$PATH:/usr/local/mysql/bin #新增该行内容
[[email protected] mysql]# . /etc/profile.d/mysql.sh #读取mysql.sh脚本的内容
[[email protected] mysql]# echo $PATH #输出PATH变量内容
/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin #刚才新增到PATH变量中的路径已生效
五、mysql的启动及相关应用解析:
[[email protected] mysql]# mysql #启动mysql客户端程序
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.28-log Source distribution
Copyright (c) 2000, 2012, 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客户端程序
mysql> SHOW DATABASES; #查看基本数据
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql> SHOW ENGINES; #查看支持的引擎
| Engine | Support | Comment | Transactions | XA | Savepoints |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO |
| CSV | YES | CSV storage engine
| MRG_MYISAM | YES | Collection of identical MyISAM tables |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables | NO | NO | NO |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
8 rows in set (0.00 sec)
mysql> \q #退出mysql
Bye
[[email protected] mysql]# vim /etc/my.cnf #修改mysql的配置文档
[[email protected] mysql]# mysql
mysql> USE mysql; #设定mysql数据库为默认database
Database changed
mysql> SELECT User,Host,Password FROM user; #查询user表中关于User,Host,密码三个字段的信息
+------+-----------+----------+
| User | Host | Password |
+------+-----------+----------+
| root | localhost | |
| root | lamp | |
| root | 127.0.0.1 | |
| root | ::1 | |
| | localhost | |
| | lamp | |
+------+-----------+----------+
6 rows in set (0.00 sec)
mysql> DROP USER ‘‘@localhost; #删除初始的匿名登录用户
Query OK, 0 rows affected (0.00 sec)
mysql> DROP USER ‘‘@lamp; #删除初始的匿名登录用户
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT User,Host,Password FROM user; #再次查看,匿名用户已经删除成功
+------+-----------+----------+
| User | Host | Password |
+------+-----------+----------+
| root | localhost | |
| root | lamp | |
| root | 127.0.0.1 | |
| root | ::1 | |
+------+-----------+----------+
4 rows in set (0.00 sec)
mysql> UPDATE user SET Password=PASSWORD(‘123456‘) WHERE user=‘root‘; #设定root用户的密码
Query OK, 4 rows affected (0.02 sec)
Rows matched: 4 Changed: 4 Warnings: 0
mysql> SELECT User,Host,Password FROM user; #再次查看,所有root用户已经设置好密码
+------+-----------+-------------------------------------------+
| User | Host | Password |
+------+-----------+-------------------------------------------+
| root | localhost | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | lamp | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | 127.0.0.1 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
| root | ::1 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+------+-----------+-------------------------------------------+
4 rows in set (0.00 sec)
mysql> FLUSH PRIVILEGES; #使设置立即生效
Query OK, 0 rows affected (0.00 sec)
[[email protected] mysql]# mysql #再次登录mysql,提示错误,因为已经设定了密码。
ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO)
[[email protected] mysql]# mysql -uroot -p #从本机登录mysql,输入刚设定的密码
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.5.28-log Source distribution
Copyright (c) 2000, 2012, 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
如何设定本机本地登录mysql不用输入密码:
在家目录下,新建一个.my.cnf文档,里面添加以下内容:
[[email protected] ~]# vim .my.cnf
[client]
user=root
host=localhost
password=123456
[[email protected] ~]# mysql #这样即使root用户设定了密码,也可以不用输入密码直接读取家目录下面的.my.cnf的隐藏文件进行登录认证。
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.5.28-log Source distribution
Copyright (c) 2000, 2012, 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> \q
以上三个文件是针对MyISAM引擎而言;
mysql> SHOW VARIABLES LIKE ‘%innodb%‘; #查看关于innodb引擎的相关变量参数
+---------------------------------+------------------------+
| Variable_name | Value |
+---------------------------------+------------------------+
| have_innodb | YES |
| ignore_builtin_innodb | OFF |
| innodb_adaptive_flushing | ON |
| innodb_adaptive_hash_index | ON |
| innodb_additional_mem_pool_size | 8388608 |
| innodb_autoextend_increment | 8 |
| innodb_autoinc_lock_mode | 1 |
| innodb_buffer_pool_instances | 1 |
| innodb_buffer_pool_size | 134217728 |
| innodb_change_buffering | all |
| innodb_checksums | ON |
| innodb_commit_concurrency | 0 |
| innodb_concurrency_tickets | 500 |
| innodb_data_file_path | ibdata1:10M:autoextend |
| innodb_data_home_dir | |
| innodb_doublewrite | ON |
| innodb_fast_shutdown | 1 |
| innodb_file_format | Antelope |
| innodb_file_format_check | ON |
| innodb_file_format_max | Antelope |
| innodb_file_per_table | OFF | #该项表示innodb引擎是否每个表都开启独立空间
| innodb_flush_log_at_trx_commit | 1 |
| innodb_flush_method |
[[email protected] mysql]# vim /etc/my.cnf #编辑mysql配置文件,开启innodb引擎对于每个表的独立空间
[[email protected] mysql]# service mysqld restart #重启mysql服务
Shutting down MySQL. [ OK ]
Starting MySQL.. [ OK ]
[[email protected] mysql]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.28-log Source distribution
Copyright (c) 2000, 2012, 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> SHOW VARIABLES LIKE ‘%innodb%‘;
+---------------------------------+------------------------+
| Variable_name | Value |
+---------------------------------+------------------------+
| have_innodb | YES |
| ignore_builtin_innodb | OFF |
| innodb_adaptive_flushing | ON |
| innodb_adaptive_hash_index | ON |
| innodb_additional_mem_pool_size | 8388608 |
| innodb_autoextend_increment | 8 |
| innodb_autoinc_lock_mode | 1 |
| innodb_buffer_pool_instances | 1 |
| innodb_buffer_pool_size | 134217728 |
| innodb_change_buffering | all |
| innodb_checksums | ON |
| innodb_commit_concurrency | 0 |
| innodb_concurrency_tickets | 500 |
| innodb_data_file_path | ibdata1:10M:autoextend |
| innodb_data_home_dir | |
| innodb_doublewrite | ON |
| innodb_fast_shutdown | 1 |
| innodb_file_format | Antelope |
| innodb_file_format_check | ON |
| innodb_file_format_max | Antelope |
| innodb_file_per_table | ON | #已经为innodb引擎设置了每表都单独为一个空间
| innodb_flush_log_at_trx_commit | 1 |
| innodb_flush_method | |
mysql> CREATE DATABASE mydb; #创建新的数据库
Query OK, 1 row affected (0.00 sec)
mysql> USE mydb; #设定mydb为默认数据库
Database changed
mysql> CREATE TABLE testdb(id INT NOT NULL,name CHAR(30)); #建立testdb表2行
Query OK, 0 rows affected (0.03 sec)
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
mysql> SHOW TABLES FROM mydb; #mydb数据库中包含的表
+----------------+
| Tables_in_mydb |
+----------------+
| testdb |
+----------------+
1 row in set (0.00 sec)
[[email protected] ~]# cd /mydata/data/mydb
[[email protected] mydb]# ll -h
total 112K
-rw-rw----. 1 mysql mysql 61 Apr 18 15:25 db.opt
-rw-rw----. 1 mysql mysql 8.4K Apr 18 15:27 testdb.frm #test表的结构文件存放
-rw-rw----. 1 mysql mysql 96K Apr 18 15:27 testdb.ibd #test表的数据和索引文件都保存在这个文件中
[[email protected] ~]# vim test.sql
CREATE DATABASE testdb; #创建一个testdb数据库
CREATE TABLE testdb.tb1(id INT,name CHAR(20)); #在testdb库中建立tb1表。
mysql> \. /root/test.sql #把test.sql中的内容导入到mysql内执行,
或者直接执行mysql < /root/test.sql 输入重定向一样。
Query OK, 1 row affected (0.00 sec)
Query OK, 0 rows affected (0.02 sec)
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydb |
| mysql |
| performance_schema |
| test |
| testdb |
+--------------------+
6 rows in set (0.00 sec)
mysql> USE testdb
Database changed
mysql> SHOW TABLES;
+------------------+
| Tables_in_testdb |
+------------------+
| tb1 |
+------------------+
1 row in set (0.00 sec)
mysql> DROP DATABASE testdb; #删除testdb库
Query OK, 1 row affected (0.01 sec)
mysql5.5.28.tar.gz下载地址:http://down.51cto.com/data/700556
cmake-2.8.8.tar.gz下载地址:http://vdisk.weibo.com/s/usonnBN1894A3