CentOS下源码安装MySQL

一、创建mysql用户与组,相关目录

useradd mysql -s /sbin/nologin
mkdir  /usr/local/mysql
chown -R mysql.mysql
mkdir -p /data/mysqldata
chown -R mysql.mysql /data/mysqldata
chown -R mysql.mysql /usr/local/mysql

二、安装所需要的库和软件包

必备的包和工具:

  • gcc/g++ :    MySQL 5.6开始,需要使用g++进行编译。
  • cmake :     MySQL 5.5开始,使用cmake进行工程管理,cmake需要2.8以上版本。
  • bison :      MySQL语法解析器需要使用bison进行编译。
  • ncurses-devel:  用于终端操作的开发包。
  • zlib:      MySQL使用zlib进行压缩

功能需要的包:

  • libxml:  用于XML输入输出方式的支持。
  • openssl:  使用openssl安全套接字方式通信。
  • dtrace:  用于诊断MySQL问题

1.下载安装所需要的软件

wget http://www.cmake.org/files/v2.8/cmake-2.8.11.1.tar.gz
wget http://ftp.gnu.org/gnu/bison/bison-2.7.tar.gz
wget wget http://ftp.gnu.org/gnu/m4/m4-1.4.17.tar.gz
#去官网下载mysql的版本:http://www.mysql.com,我的云盘:http://pan.baidu.com/s/1slaU3al

安装cmake编译器

tar -xvf cmake-2.8.11.1.tar.gz
cd cmake-2.8.11.1
./configure
make&&make install

安装m4

tar -xvf m4-1.4.17.tar.gz
cd m4-1.4.17
./configure && make && make install

安装bison

tar xf bison-2.7.tar.gz
cd bison-2.7
./configure
make
make install

2.安装所需要的库文件

yum -y install gcc gcc-c++ ncurses-devel openssl zlib-devel perl autoconf

三、安装Mysql

有关MySQL编译参数:

MAKE_BUILD_TYPE   编译的版本类型:RelWithDebInfo和Debug,不同之处是RelWithDebInfo会进行优化。

CMAKE_INSTALL_PREFIX 指定make install安装的目标路径。

SYSCONFDIR 指定配置文件的默认路径。

MYSQL_DATADIR 指定data目录的默认路径。

WITH_DEBUG 指定是否有debugging信息,一般用于源码调试时,打开WITH_DEBUG,生产环境关闭。

ENABLED_PROFILING 指定是否可以使用show profile显示操作执行的详细信息。

DEFAULT_CHARSET 指定默认字符集,可以在启动的配置文件中指定。

DEFAULT_COLLATION 指定默认字符比较、排序的规则。

WITH_EXTRA_CHARSETS 指定其他可能使用的字符集。

WITH_SSL 指定SSL的类型,从5.6.6开始默认bundled类型,此外也可以指定SSL库的路径地址。

WITH_ZLIB 指定zlib的类型,用于压缩功能。

ENABLED_LOCAL_INFILE 指定是否允许使用load data infile功能。

WITH_EMBEDDED_SERVER 指定是否编译libmysqld嵌入式库。

INSTALL_LAYOUT 指定安装的布局类型。

WITH_storage_STORAGE_ENGINE 指定编译支持的存储引擎,默认支持MyISAM,MERGE,MEMORY,CSV存储引擎。

更多详细参数可参考 http://dev.mysql.com/doc/refman/5.5/en/source-configuration-options.html

编译安装

cd /usr/local/src
tar -xvf mysql-5.6.34.tar.gz
cd mysql-5.6.34
cmake -DCMAKE_INSTLL_PREFIX=/usr/local/mysql/ -DMYSQL_DATADIR=/data/mysqldata -DSYSCONFDIR=/etc -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EXTRA_CHARSETS=utf8,gbk -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DENABLE_DOWNLOADS=1
make
make install

删除系统的默认配置文件

rm -rf /etc/my.cnf

初始化数据库

cd /usr/local/mysql/
./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysqldata/

创建配置文件的软连接文件:

ln -s /usr/local/mysql/my.cnf /etc/my.cnf

将mysqld服务程序添加到开机启动项:

cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld
chmod 755 /etc/rc.d/init.d/mysqld
chkconfig mysqld on

编辑启动项的配置文件:

vim /etc/init.d/mysqld

basedir=/usr/local/mysql
datadir=/data/mysqldata

启动mysqld服务程序:

service mysqld start

把mysql服务程序命令目录添加到环境变量中(永久生效):

echo "export PATH=$PATH:/usr/local/mysql/bin" >>/etc/profile.d/mysql.sh
source /etc/profile.d/mysql.sh

将mysqld服务程序的库文件链接到默认的位置:

mkdir /var/lib/mysql
ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql
ln -s /usr/local/mysql/include/mysql /usr/include/mysql
ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock

初始化mysqld服务程序:

[root@lnmp mysql]# mysql_secure_installation 
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we'll need the current
password for the root user.  If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none): 
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
Set root password? [Y/n] y  
New password: 输入要为root用户设置的数据库密码。
Re-enter new password: 重复再输入一次密码。
Password updated successfully!
Reloading privilege tables..
 ... Success!
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y(删除匿名帐号)
 ... Success!
Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] y(禁止root用户从远程登陆)
 ... Success!
By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y(删除test数据库并取消对其的访问权限)
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y(刷新授权表,让初始化后的设定立即生效)
 ... Success!
All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!
Cleaning up...

修改字符集和数据存储路径

[client]  
password        = 123456  
port            = 3306  
default-character-set=utf8  
[mysqld]  
port            = 3306  
character_set_server=utf8  
character_set_client=utf8  
collation-server=utf8_general_ci  
#(注意linux下mysql安装完后是默认:表名区分大小写,列名不区分大小写; 0:区分大小写,1:不区分大小写)  
lower_case_table_names=1  
#(设置最大连接数,默认为 151,MySQL服务器允许的最大连接数16384; )  
max_connections=1000  
[mysql]  
default-character-set = utf8