源码安装MYSQL
1. MYSQL要编译安装需要cmake(MYSQL-5.58版本以后)
1.1 cmake需要make,所以确保已经安装上了make
[root@www ~]# rpm -qa make
make-3.81-20.el6.i686
1.2 安装cmake
[root@www ~]# tar xf cmake-3.3.2.tar.gz
[root@www ~]# cd cmake-3.3.2
[root@www cmake-3.3.2]# ./configure
[root@www cmake-3.3.2]# make && make install
2. 安装MYSQL
2.1 创建MYSQL数据存放的目录,这里创建一个LVM
[root@www ~]# fdisk /dev/sdb
Command (m for help): p
Device Boot Start End Blocks Id System
/dev/sdb1 1 500 4016218+ 83 Linux
/dev/sdb2 501 800 2409750 83 Linux
/dev/sdb3 801 1000 1606500 83 Linux
/dev/sdb4 1001 2610 12932325 5 Extended
Command (m for help): n
First cylinder (1001-2610, default 1001):
Using default value 1001
Last cylinder, +cylinders or +size{K,M,G} (1001-2610, default 2610): +2G
Command (m for help): t
Partition number (1-5): 5
Hex code (type L to list codes): 8e
Changed system type of partition 5 to 8e (Linux LVM)
Command (m for help): p
Device Boot Start End Blocks Id System
/dev/sdb1 1 500 4016218+ 83 Linux
/dev/sdb2 501 800 2409750 83 Linux
/dev/sdb3 801 1000 1606500 83 Linux
/dev/sdb4 1001 2610 12932325 5 Extended
/dev/sdb5 1001 1262 2104483+ 8e Linux LVM
Command (m for help): w
The partition table has been altered!
[root@www ~]# partprobe /dev/sdb
[root@www ~]# pvcreate /dev/sdb5
Physical volume "/dev/sdb5" successfully created
[root@www ~]# vgcreate myvg /dev/sdb5
Volume group "myvg" successfully created
[root@www ~]# lvcreate -L 1G -n mydata myvg
Logical volume "mydata" created
[root@www ~]# lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
mydata myvg -wi-a----- 1.00g
[root@www ~]# mke2fs -j /dev/myvg/mydata
[root@www ~]# mkdir /mysqldata
[root@www ~]# vim /etc/fstab
/dev/myvg/mydata /mysqldata ext3 defaults 0 0
[root@www ~]# mount /dev/myvg/mydata /mysqldata/
2.2 创建MYSQL用户,组
[root@www ~]# groupadd -r mysql
[root@www ~]# useradd -r -g mysql -s /sbin/nologin mysql
[root@www ~]# id mysql
uid=496(mysql) gid=493(mysql) groups=493(mysql)
[root@www ~]# mkdir /mysqldata/data
[root@www ~]# chown -R mysql.mysql /mysqldata/data/
2.3 编译安装MYSQL
[root@www ~]# tar xf mysql-5.6.11.tar.gz
[root@www ~]# cd mysql-5.6.11
可以先查看编译支持的配置选项和环境:
[root@www mysql-5.6.11]# cmake . -LH
[root@www mysql-5.6.11]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/mysqldata/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
[root@www mysql-5.6.11]# make
make之前重新运行配置,需要删除CMakeCache.txt文件(之前没有删除该文件。报错了,检查后却没有发现错误,删除该文件后make正常)
[root@www mysql-5.6.11]# make install
3. 配置MYSQL
3.1 将文件和目录的属组改为MYSQL
[root@www mysql]# chown -R :mysql /usr/local/mysql/
3.2 初始化MYSQL
[root@www mysql]# scripts/mysql_install_db --user=mysql --datadir=/mysqldata/data/
3.3 复制配置文件
[root@www mysql]# cp my.cnf /etc/my.cnf
3.4 配置启动文件并启动服务
[root@www mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@www mysql]# chkconfig --add mysqld
[root@www mysql]# chkconfig mysqld on
[root@www mysql]# service mysqld start
Starting MySQL.. SUCCESS!
3.5 将MYSQL命令加入到环境变量中
[root@www mysql]# mysql
-bash: mysql: command not found
[root@www mysql]# vim /etc/profile.d/mysql.sh
export PATH=$PATH:/usr/local/mysql/bin
[root@www profile.d]# chmod +x mysql.sh
[root@www profile.d]# ./mysql.sh
[root@www ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/mysql/bin:/root/bin
[root@www ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.11 Source distribution
Copyright (c) 2000, 2013, 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>
4. 其他配置
4.1 删除两个匿名用户
[root@www ~]# mysql
mysql> use mysql;
mysql> select user,host,password from user;
+------+-----------+----------+
| user | host | password |
+------+-----------+----------+
| root | localhost | |
| root | | |
| root | 127.0.0.1 | |
| root | ::1 | |
| | localhost | |
| | | |
+------+-----------+----------+
mysql> drop user ''@'localhost';
mysql> drop user ''@'';
4.2 为用户添加密码
mysql> update user set password=password('redhat') where user='root';
mysql> select user,host,password from user;
+------+-----------+-------------------------------------------+
| user | host | password |
+------+-----------+-------------------------------------------+
| root | localhost | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |
| root | | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |
| root | 127.0.0.1 | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |
| root | ::1 | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |
+------+-----------+-------------------------------------------+
[root@www ~]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@www ~]# mysql -u root -p
Enter password:
4.3 设置以后自己可以不填写密码访问
在当前目录下创建一个隐藏文件放置用户名,主机名和密码(注意该文件权限)
[root@www ~]# vim .my.cnf
[client]
user = 'root'
password = 'redhat'
host = 'localhost'
[root@www ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.6.11 Source distribution
Copyright (c) 2000, 2013, 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>
5. 说明
1. 表说明
[root@www data]# pwd
/mysqldata/data
[root@www data]# ls mysql/ #这里存放的是表
例如:
db.frm #表结构文件
db.MYD #表存入的数据文件
db.MYI #表的索引文件
对于MyISAM引擎,其每个表都有三个文件.frm:表结构文件 .MYD:表数据文件 .MYI:表索引文件
对于InnoDB引擎,其所有的表都放在表空间中即所有表共用一个表空间,建议让每个表独有一个表空间(该功能以打开):
[root@www data]# mysql
mysql> show variables like 'innodb_file_per_table';
+-----------------------+-------+
| Variable_name | Value |
+-----------------------+-------+
| innodb_file_per_table | ON |
+-----------------------+-------+
创建一个表:
mysql> create database mydb;
mysql> use mydb;
mysql> create table test(
-> id int not null,
-> name char(30));
[root@www data]# ll | grep mydb
drwx------ 2 mysql mysql 4096 Oct 1 09:51 mydb
[root@www data]# cd mydb
[root@www mydb]# ll
-rw-rw---- 1 mysql mysql 65 Oct 1 09:49 db.opt
-rw-rw---- 1 mysql mysql 8586 Oct 1 09:51 test.frm
-rw-rw---- 1 mysql mysql 98304 Oct 1 09:51 test.ibd
所以,对于InnoDB而言:
.frm:表结构文件
.idb:表空间(同时存储该表的表数据和表索引)
.opt:当前数据库的字符集和排序规则的定义
[root@www mydb]# file db.opt
db.opt: ASCII text
[root@www mydb]# cat db.opt
default-character-set=latin1
default-collation=latin1_swedish_ci
5.2 编译说明
指定安装文件的安装路径时常用的选项:
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql
-DMYSQL_DATADIR=/data/mysql
-DSYSCONFDIR=/etc
默认编译的存储引擎包括:csv、myisam、myisammrg和heap。若要安装其它存储引擎,可以使用类似如下编译选项:
-DWITH_INNOBASE_STORAGE_ENGINE=1
-DWITH_ARCHIVE_STORAGE_ENGINE=1
-DWITH_BLACKHOLE_STORAGE_ENGINE=1
-DWITH_FEDERATED_STORAGE_ENGINE=1
若要明确指定不编译某存储引擎,可以使用类似如下的选项:
-DWITHOUT_<ENGINE>_STORAGE_ENGINE=1
比如:
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1
-DWITHOUT_FEDERATED_STORAGE_ENGINE=1
-DWITHOUT_PARTITION_STORAGE_ENGINE=1
如若要编译进其它功能,如SSL等,则可使用类似如下选项来实现编译时使用某库或不使用某库:
-DWITH_READLINE=1
-DWITH_SSL=system
-DWITH_ZLIB=system
-DWITH_LIBWRAP=0
其它常用的选项:
-DMYSQL_TCP_PORT=3306
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock
-DENABLED_LOCAL_INFILE=1
-DEXTRA_CHARSETS=all
-DDEFAULT_CHARSET=utf8
-DDEFAULT_COLLATION=utf8_general_ci
-DWITH_DEBUG=0
-DENABLE_PROFILING=1
MYSQL5.6版本的编译参数介绍:
http://dev.mysql.com/doc/refman/5.6/en/source-configuration-options.html