MySQL5.4以前的版本(如MySQL5.1系列)都可以用源码通过常规的编译方式安装,即./configure;make;make install,生产场景的具体命令和参数为:
[root@Howden mysql-5.1.72]# ./configure \ --prefix=/opt/myslq \ --with-unix-socket-path=/opt/mysql/tmp/mysql.sock --localstatedir=/opt/mysql/data \ --enable-assembler \ --enable-thread-safe-client \ --with-mysqld-user=mysql \ --with-big-tables \ --without-debug \ --with-pthread \ --enable-assembler \ --with-extra-charsets=complex \ --with-readline \ --with-ssl \ --with-embedded-server \ --enable-local-infile \ --with-plugins=partition,innobase \ --with-mysqld-ldflags=-all-static \ --with-client-ldflags=-all-static [root@Howden mysql-5.1.72]# make [root@Howden mysql-5.1.72]# make install
MySQL从5.4版本开始加入了一些用于提高性能的新算法,同时编译方式也和以前不同了,要采用cmake方式编译安装。因此先把cmake装上,并安装一个依赖包ncurses-devel:
[root@Howden ~]# tar xf cmake-2.8.8.tar.gz [root@Howden ~]# cd cmake-2.8.8 [root@Howden cmake-2.8.8]# ./configure [root@Howden cmake-2.8.8]# gmake [root@Howden cmake-2.8.8]# gmake install [root@Howden cmake-2.8.8]# yum install ncurses-devel
我们需要安装的MySQL是5.5版本,在安装之前还必须创建mysql用户和用户组:
[root@Howden ~]# groupadd mysql [root@Howden ~]# useradd mysql -s /sbin/nologin -M -g mysql
其中:
-s /sbin/nologin 表示禁止该用户登录,加强安全
-M 表示不创建用户家目录
-g 指定用户属于mysql组
把MySQL的源码包解压缩之后,就可以用cmake方式安装了:
[root@Howden mysql-5.5.32]# cmake . -DCMAKE_INSTALL_PREFIX=/opt/mysql \ > -DMYSQL_DATADIR=/opt/mysql/data \ > -DMYSQL_UNIX_ADDR=/opt/mysql/tmp/mysql.sock \ > -DDEFAULT_CHARSET=utf8 \ > -DDEFAULT_COLLATION=utf8_general_ci \ > -DEXTRA_CHARSETS=gbk,gb2312,utf8,ascii \ > -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 [root@Howden mysql-5.5.32]# make && make install
安装完成之后,配置一下环境变量,把mysql执行程序所在的目录加入到PATH变量中:
[root@Howden ~]# echo 'export PATH=/opt/mysql/bin:$PATH' >> /etc/profile [root@Howden ~]# source /etc/profile
注意要把该路径放在PATH变量的最前面,因为当你使用不加前缀路径的方式执行命令时,对PATH中的路径是从前向后检索。如果把这个路径放在了PATH的最后,而系统中又有用rpm包安装的mysql,会先从/usr/bin/下找到mysql执行程序,就会导致错误。关于该问题的具体讲解参考:http://oldboy.blog.51cto.com/2561410/1122867
接下来,把mysql的主配置文件放到/etc/下。mysql的安装包中提供了多个配置文件可供选择,以适应不同的系统环境。在测试环境中可以选择最小的:
[root@Howden mysql-5.5.32]# ll support-files/*.cnf -rw-r--r-- 1 root root 4699 Nov 19 22:35 support-files/my-huge.cnf -rw-r--r-- 1 root root 19779 Nov 19 22:35 support-files/my-innodb-heavy-4G.cnf -rw-r--r-- 1 root root 4673 Nov 19 22:35 support-files/my-large.cnf -rw-r--r-- 1 root root 4684 Nov 19 22:35 support-files/my-medium.cnf -rw-r--r-- 1 root root 2848 Nov 19 22:35 support-files/my-small.cnf [root@Howden mysql-5.5.32]# /bin/cp support-files/my-small.cnf /etc/my.cnf
在初始化mysql之前的最后一步,把mysql的data目录的所有者和所属组改为mysql:
[root@Howden mysql-5.5.32]# chown -R mysql.mysql /opt/mysql/data/
然后就可以初始化mysql了:
[root@Howden mysql-5.5.32]# cd /opt/mysql/scripts/ [root@Howden scripts]# ./mysql_install_db --basedir=/opt/mysql/ --datadir=/opt/mysql/data/ --user=mysql
在输出中如果看到如下两个OK就说明初始化成功了:
Installing MySQL system tables... OK Filling help tables... OK
现在就可以启动mysql进程了,把它的守护进程脚本程序放到/etc/init.d/目录下并赋予可执行权限,然后启动:
[root@Howden mysql-5.5.32]# /bin/cp support-files/mysql.server /etc/init.d/mysqld [root@Howden mysql-5.5.32]# chmod +x /etc/init.d/mysqld [root@Howden mysql-5.5.32]# /etc/init.d/mysqld start Starting MySQL... SUCCESS! [root@Howden mysql-5.5.32]# netstat -lntup | grep 3306 tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2375/mysqld
这样就启动成功了!
在登录mysql时,如果遇到如下故障:
[root@Howden ~]# mysql ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
可以先杀掉mysql进程,并删除数据文件,然后重新初始化mysql:
[root@Howden ~]# pkill mysqld [root@Howden ~]# lsof -i :3306 [root@Howden ~]# rm -rf /opt/mysql/data/* [root@Howden ~]# /opt/mysql/scripts/mysql_install_db --basedir=/opt/mysql/ --datadir=/opt/mysql/data/ --user=mysql
成功登录mysql之后,做一些基本的优化工作:
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.00 sec) mysql> drop database test; Query OK, 0 rows affected (0.04 sec) mysql> select user,host from mysql.user; +------+-----------+ | user | host | +------+-----------+ | root | 127.0.0.1 | | root | ::1 | | | Howden | | root | Howden | | | localhost | | root | localhost | +------+-----------+ 6 rows in set (0.00 sec) mysql> delete from mysql.user; Query OK, 6 rows affected (0.00 sec) mysql> grant all privileges on *.* to system@'localhost' identified by '12345678' with grant option; Query OK, 0 rows affected (0.00 sec)