1.下载mysql安装包

MySQL从5.3开始使用cmake的安装方式,本次实战选用mysql5.5.32

MySQL系列下载地址

http://dev.mysql.com/downloads/mysql/5.5.html#downloads

下载cmake

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

2.查看系统环境

[root@mysql ~]# cat /etc/redhat-release 
Red Hat Enterprise Linux Server release 6.7 (Santiago)
[root@mysql ~]# uname -m
x86_64
[root@mysql ~]# uname -r
 2.6.32-573.el6.x86_64

3.安装cmake

[root@mysql ~]# mkdir -p /home/linzhongniao
[root@mysql ~]# mkdir -p /home/linzhongniao/tools
[root@mysql ~]# cd /home/linzhongniao/tools
[root@mysql tools]# tar -zxf cmake-2.8.8.tar.gz 
[root@mysql tools]# cd cmake-2.8.8
[root@mysql cmake-2.8.8]# ./configure

提示用gmake安装,用make也可以

[root@mysql cmake-2.8.8]# gmake
[root@mysql cmake-2.8.8]# gmake install

安装依赖包

[root@mysql tools]# yum install ncurses-devel -y

4. 开始安装mysql

4.1 创建用户和组

[root@mysql tools]# groupadd mysql
[root@mysql tools]# useradd -g mysql mysql

4.2 解压编译mysql安装

[root@mysql tools]# tar -zxf mysql-5.5.32.tar.gz
[root@mysql tools]# cd mysql-5.5.32
[root@mysql mysql-5.5.32]#cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql         #指定安装目录
 -DMYSQL_DATADIR=/usr/local/mysql/data   #指定数据存放目录重要
 -DMYSQL_UNIX_ADDR=/usr/local/mysql/tmp/mysql.sock  #指定sock路径
 -DDEFAULT_CHARSET=utf8   #指定默认字符集
 -DDEFAULT_COLLATION=utf8_general_ci  #指定校准字符集编码
 -DEXTRA_CHARSETS=all       #安装所需字符集
 -DENABLED_LOCAL_INFILE=ON  #启用加载本地数据
 -DWITH_INNOBASE_STORAGE_ENGINE=1   #支持innode引擎 
 -DWITH_FEDERATED_STORAGE_ENGINE=1  #支持federated引擎
 -DWITH_BLACKHOLE_STORAGE_ENGINE=1  #支持黑洞存储引擎
 -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1  
 -DWITHOUT_PARTITION_STORAGE_ENGINE=1   #支持安装数据库分区 
 -DWITH_FAST_MUTEXES=1  
 -DWITH_ZLIB=bundled    #zlib压缩模式
 -DENABLED_LOCAL_INFILE=1  
 -DWITH_READLINE=1  
 -DWITH_EMBEDDED_SERVER=1  
 -DWITH_DEBUG=0     #禁用debug,开启影响性能
 [root@mysql mysql-5.5.32]# make && make install
 [root@mysql mysql-5.5.32]# ln -s /home/zbf/tools/mysql-5.5.32 /usr/local/mysql/

粘贴下面内容:

cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/data -DMYSQL_UNIX_ADDR=/usr/local/mysql/tmp/mysql.sock -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all  -DENABLED_LOCAL_INFILE=ON -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 -DWITHOUT_PARTITION_STORAGE_ENGINE=1 -DWITH_FAST_MUTEXES=1 -DWITH_ZLIB=bundled -DENABLED_LOCAL_INFILE=1 -DWITH_READLINE=1 -DWITH_EMBEDDED_SERVER=1 -DWITH_DEBUG=0

编译出现下面问题,yum安装bison重新编译

 -- Performing Test HAVE_PEERCRED - Success
Warning: Bison executable not found in PATH

特别说明:常规configure编译方式安装以及免编译方式安装等等的安装方法,只是上面的过程不同,下面初始化的内容相同相同。

4.3 初始化配置MySQL

4.3.1 查看默认模板配置文件

[root@mysql mysql-5.5.32]# ll support-files/my*cnf
 -rw-r--r--. 1 root root  4723 1月  11 19:03 support-files/my-huge.cnf
 -rw-r--r--. 1 root root 19791 1月  11 19:03 support-files/my-innodb-heavy-4G.cnf
 -rw-r--r--. 1 root root  4697 1月  11 19:03 support-files/my-large.cnf
 -rw-r--r--. 1 root root  4708 1月  11 19:03 support-files/my-medium.cnf
 -rw-r--r--. 1 root root  2872 1月  11 19:03 support-files/my-small.cnf 

4.3.2 选择配置文件

配置文件很多我们选一个小的,因为是测试环境

[root@mysql mysql-5.5.32]# cp support-files/my-small.cnf /etc/my.cnf
cp:是否覆盖"/etc/my.cnf"? y

4.3.3 配置环境变量

[root@mysql mysql-5.5.32]# echo "export PATH=/usr/local/mysql/bin:$PATH" >> /etc/profile
[root@mysql mysql-5.5.32]# tail -1 /etc/profile
export PATH=/usr/local/mysql/bin:$PATH
[root@mysql mysql-5.5.32]# source /etc/profile
[root@mysql mysql-5.5.32]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

注意一定要把/usr/local/mysql/bin放在前边,因为在以后安装的系统里很可能有yum安装的mysql客户端。如果不把/usr/local/mysql/bin放在前边,当我们输入mysql的时候它会调用系统rpm安装的mysql命令。这里有一个配置环境变量的例子,http://blog.51cto.com/oldboy/1122867。

4.3.4 初始化数据文件

已经有了,没有的话创建一个mysql数据文件目录

[root@mysql mysql-5.5.32]# ll /usr/local/mysql/data/
总用量 4
drwxr-xr-x. 2 root root 4096 1月  11 19:17 test

授权mysql用户访问mysql的安装目录

[root@mysql mysql-5.5.32]# chown -R mysql.mysql /usr/local/mysql/data/

调整/tmp权限,否则初始化会错误

[root@mysql mysql-5.5.32]# chmod -R 1777 /tmp/

注意:这个初始化和5.1不一样,在scripts下面

[root@mysql mysql-5.5.32]# cd /usr/local/mysql/scripts/
[root@mysql scripts]# ./mysql_install_db --basedir=/usr/local/
mysql/ --datadir=/usr/local/mysql/data/ --user=mysql

现在mysql就安装完成了,安装完成之后我们一定要来看一下初始化的信息,因为它给我们提示了很多信息。

Installing MySQL system tables...
######初始化成功的关键是有两个OK,一般有两个OK就说明初始化成功了,出现警告不用管
OK
Filling help tables...
OK
###这个是启动脚本,提示了copy启动脚本启动mysql
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:
#####mysql没有密码可以改密码
/usr/local/mysql//bin/mysqladmin -u root password 'new-password'
/usr/local/mysql//bin/mysqladmin -u root -h mysql password 'new-password'
Alternatively you can run:
/usr/local/mysql//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.
####如果想启动mysql,可以用下面的方法启动
You can start the MySQL daemon with:
cd /usr/local/mysql/ ; /usr/local/mysql//bin/mysqld_safe &
###如果想测试可以用这个方法测试
You can test the MySQL daemon with mysql-test-run.pl
cd /usr/local/mysql//mysql-test ; perl mysql-test-run.pl
###如果想汇报bug可以用这个方法汇报bug
Please report any problems with the /usr/local/mysql//scripts/mysqlbug script!

5. 复制mysql启动脚本,启动mysql

[root@mysql mysql-5.5.32]# cp support-files/mysql.server /etc/
init.d/mysqld
[root@mysql mysql-5.5.32]# chmod +x /etc/init.d/mysqld 
[root@mysql mysql-5.5.32]# /etc/init.d/mysqld start
Starting MySQL.. SUCCESS!
[root@mysql ~]# chkconfig mysqld on
[root@mysql ~]# chkconfig --list mysqld
mysqld 	0:关闭	1:关闭	2:启用	3:启用	4:启用	5:启用	6:关闭

6.简单优化

6.1 清楚并修改管理员用户

mysql> select user,host from mysql.user;
+------+-----------+
| user | host  |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1   |
|  | localhost |
| root | localhost |
|  | mysql |
| root | mysql |
+------+-----------+
6 rows in set (0.01 sec)

mysql> delete from mysql.user where user="";

Query OK, 2 rows affected (0.07 sec) 

mysql> delete from mysql.user where host="mysql";

Query OK, 1 row affected (0.00 sec)

mysql> delete from mysql.user where host="::1";

Query OK, 1 row affected (0.00 sec)

6.2 删除test库

mysql> show databases;
+--------------------+
| Database   |
+--------------------+
| information_schema |
| mysql  |
| performance_schema |
| test   |
+--------------------+

4 rows in set (0.01 sec)

mysql> drop database test;

Query OK, 0 rows affected (0.11 sec)

6.3 命令行修改mysql的登录密码

修改密码很简单前面的初始化提示信息就已经说明了

[root@mysql ~]# /usr/local/mysql//bin/mysqladmin -u root 
password '123456'
[root@mysql ~]# mysql -uroot -p123456