使用的软件
cmake-2.8.8.tar.gz
mysql-5.5.28.tar.gz
编译安装MySQL5.5的步骤介绍
1.准备一块lvm磁盘用来存储MySQL的数据,以便后期存储空间的扩展。(可选)
2.编译安装cmake环境
mysql 5.5以前的版本可以使用make进行编译安装,但是mysql5.5以后的版本需要cmake进行编译安装了
redhat5 系列的版本是没有安装cmake工具的,redhat6以上的版本安装了cmake工具
3.创建MySQL的用户和组
4.编译安装MySQL
5.编译安装MySQL之后,删除MySQL自动建立的用户,以保证MySQL的安全
6.MySQL5.5默认使用InnoDB存储引擎,InnoDB存储方式为所有表共享一个表空间文件;
建议:每表一个独立的表空间文件;否则一个表空间放了所有的表和索引文件后期管理起来非常麻烦
1.准备存储数据的空间(可选)
查看磁盘已经有了为配置lvm准备的磁盘了
[[email protected] ~]# fdisk -l
Device Boot Start End Blocks Id System
/dev/sdb1 1 2610 20964793+ 8e Linux LVM
创建lvm磁盘
[[email protected] ~]# pvcreate /dev/sdb1
Physical volume "/dev/sdb1" successfully created
[[email protected] ~]# vgcreate myvg /dev/sdb1
Volume group "myvg" successfully created
[[email protected] ~]# lvcreate -L 10G -n mydata myvg
Logical volume "mydata" created
[[email protected] ~]# lvs
LV VG Attr LSize Origin Snap% Move Log Copy% Convert
mydata myvg -wi-a- 10.00G
格式化
[[email protected] ~]# mke2fs -j /dev/myvg/mydata
设置开机自动挂载
[[email protected] ~]# vim /etc/fstab
/dev/myvg/mydata /mydata ext3 defaults 0 0
创建文件夹并且挂载
[[email protected] ~]# mkdir /mydata
[[email protected] ~]# mount -a
2.编译安装cmake环境
编译安装cmake-2.8.8
[[email protected] ~]# tar xf cmake-2.8.8.tar.gz
[[email protected] ~]#cd cmake-2.8.8
[[email protected] cmake-2.8.8]# ./configure && make && make install
3.创建MySQL的用户和组
[[email protected] ~]# groupadd -r mysql
[[email protected] ~]# useradd -r -g mysql -s /sbin/nologin mysql
创建存放数据的目录并且设置属主和属组为MySQL
[[email protected] ~]# mkdir /mydata/data
[[email protected] ~]# chown -R mysql.mysql /mydata/data/
4.编译安装mysqlmysql-5.5.28
[[email protected] ~]# tar xf mysql-5.5.28.tar.gz
[[email protected] ~]# cd mysql-5.5.28
[[email protected] mysql-5.5.28]# cmake . -LH #生成编译时的头文件库文件=./configure --help cmake . -LH
[[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_ZLIB=system -DWITH_LIBWRAP=0 -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci
[[email protected] ~]#make && make install
编译完成之后更改mysql的安装目录属组为mysql
[[email protected] ~]# 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
添加样例文件和系统服务
[[email protected] mysql]# cp support-files/my-large.cnf /etc/my.cnf
[[email protected] mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[[email protected] mysql]# chkconfig --add mysqld
[[email protected] mysql]# chkconfig --list mysqld
mysqld 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
启动mysql
[[email protected] mysql]# service mysqld start
为了能够直接使用mysql客户端工具,需要编辑profile.d文件,把mysql客户端工具添加到命令路径
[[email protected] mysql]# vim /etc/profile.d/mysql.sh
export PATH=$PATH:/usr/local/mysql/bin
[[email protected] mysql]# . /etc/profile.d/mysql.sh
[[email protected] mysql]# echo $PATH
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin
这时就可以使用客户端命令连接进来了
[[email protected] mysql]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
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 databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
5.删除MySQL安装时自带的匿名用户,然后给管理员设置密码
查看mysql的用户
mysql> SELECT User,Host,Password FROM user;
+------+-----------------------+----------+
| User | Host | Password |
+------+-----------------------+----------+
| root | localhost | |
| root | localhost.localdomain | |
| root | 127.0.0.1 | |
| root | ::1 | |
| | localhost | |
| | localhost.localdomain | |
+------+-----------------------+----------+
6 rows in set (0.00 sec)
删除两个匿名用户和给管理员设置密码
mysql> DROP USER ‘‘@localhost;
Query OK, 0 rows affected (0.00 sec)
mysql> DROP USER ‘‘@‘localhost.localdomain‘;
Query OK, 0 rows affected (0.00 sec)
mysql> UPdata user SET Password=PASSWORD(‘redhat‘) WHERE user=‘root‘;
如果不使用ipv6地址也可以把::1这个用户删除
mysql> DROP USER ‘root‘@‘::1‘;
Query OK, 0 rows affected (0.00 sec)
在查看匿名用户都被删除了,而且root都上了密码
mysql> SELECT User,Host,Password FROM user;
+------+-----------------------+-------------------------------------------+
| User | Host | Password |
+------+-----------------------+-------------------------------------------+
| root | localhost | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |
| root | localhost.localdomain | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |
| root | 127.0.0.1 | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |
+------+-----------------------+-------------------------------------------+
3 rows in set (0.00 sec)
退出时刷新权限相关的表
mysql> FLUSH PRIVILEGES;
退出mysql是在使用mysql命令就连接不进来了,要连接需要指定用户名和密码
[[email protected] mysql]# mysql
ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO)
[[email protected] mysql]# mysql -uroot -p
Enter password:
6.存储引擎设置
查看存储引擎
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ | Engine | Support | Comment | Transactions | XA | Savepoints | +--------------------+---------+----------------------------------------------------------------+--------------+------+------------+ | MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO | | InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign keys | YES | YES | YES | | CSV | YES | CSV storage engine | NO | NO | NO | | BLACKHOLE | YES | /dev/null storage engine (anything you write to it disappears) | NO | NO | NO | | MyISAM | YES | MyISAM storage engine | NO | NO | NO | | PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO | | 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)
Engine :引擎的名称
Support:是否支持,DEFAULT表示默认存储引擎
Comment:对引擎的简要描述
Transactions:是否支持事务
XA:分布式事务
Savepoints:保存点
MyISAM:
每表三个文件:
.frm: 表结构
.MYD:表数据
.MYI:表索引
InnoDB:
所有表共享一个表空间文件;
建议:每表一个独立的表空间文件;否则一个表空间放了所有的表和索引文件后期管理起来非常麻烦
.frm: 表结构
.ibd: 表空间(表数据和表索引)
设置每个表使用一个独立的表空间文件
[[email protected] mysql]# vim /etc/my.cnf
innodb_file_per_table = 1
[[email protected] mysql]# service mysqld restart
mysql查看innodb_file_per_table 是否启用
mysql> SHOW GLOBAL VARIABLES LIKE ‘%innodb%‘;
innodb_file_per_table | ON
创建一个数据库和表
mysql> CREATE DATABASE mydb;
Query OK, 1 row affected (0.00 sec)
mysql> USE mydb;
Database changed
mysql> CREATE TABLE testdb(id INT NOT NULL,name CHAR(30));
Query OK, 0 rows affected (0.01 sec)
查看数据库存放位置的表空间就会看到mydb只有一个表空间文件
[[email protected] mysql]# cd /mydata/data
[[email protected] data]# cd mydb/
[[email protected] mydb]# ls
db.opt testdb.frm testdb.ibd
db.opt定义数据库的默认排序规则和字符集
安装完mysql数据库并且初始化后,自动生成的mysql数据库是使用MyISAM存储引擎,要是自己建表取决于自己设置的默认存储引擎
查看表的属性信息
mysql> SHOW TABLE STATUS LIKE ‘user‘\G
*************************** 1. row ***************************
Name: user
Engine: MyISAM
Version: 10
Row_format: Dynamic
Rows: 3
Avg_row_length: 124
Data_length: 372
Max_data_length: 281474976710655
Index_length: 2048
Data_free: 0
Auto_increment: NULL
Create_time: 2014-11-16 19:12:16
Update_time: 2014-11-16 19:41:14
Check_time: NULL
Collation: utf8_bin
Checksum: NULL
Create_options:
Comment: Users and global privileges
1 row in set (0.00 sec)