2、mysql编译安装
2.1前言:
此文档介绍的是cmake编译安装的方式;
二进制的安装方式在linux运维_集群_01中有详细的安装说明(已经编译完成,进行初始操作即可)
初始化操作时需要对编译好的mysql进行一下备份,防止初始化错误而出现问题,导致需要重新编译mysql;
1、原始的编译方式编译:
在当下的互联网企业中,./configure 编译安装的方法已经很少使用了,原因是第一条产品线的产品(MySQL5.2.xx及以前的产品系列)用得
越来越少了,被第二条产品线(MySQL5.4.xx及以后的产品系列)的产品逐渐替代了,因此,不建议读者再使用第一条产品线的产品作为对外的业务库。
tar -zxf mysql-5.1.73.tar.gz
cd mysql-5.1.73
./configure \
--prefix=/application/mysql5.1.73 \
--with-unix-socket-path=/application/mysql5.1.73/tmp/mysql.sock \
--localstatedir=/application/mysql5.1.73/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
make
make install
ln -s/application/mysql-5.1.73/ /application/mysql
安装到这里,MySQL数据库还不能正常启动使用,还需要进行初始化数据库等工作;
2、为什么使用cmake编译源码:
cmake编译工作可以在另一个指定的目录中而非源码目录中进行,源码目录不受任何一次编译的影响,
因此在同一个源码树上可以进行多次不同的编译,如针对于不同平台编译,可以跨平台的编译器。
考虑到MySQL5.4.xx及以后系列产品的特殊性,其编译方式和早期的第一条产品线的有所不同,这里采用cmake或gmake方式的编译安装;
2.2、查看系统内核和版本:
[root@backup ~]# uname -r
2.6.32-754.3.5.el6.x86_64
[root@backup ~]# uname -m
x86_64
2.3、上传cmake和mysql软件包:
[root@backup ~]# mkdir /root/tools/ -p
[root@backup ~]# cd tools
[root@backup tools]# rz -y
rz waiting to receive.
Starting zmodem transfer. Press Ctrl+C to cancel.
Transferring cmake-2.8.8.tar.gz...
100% 5558 KB 5558 KB/sec 00:00:01 0 Errors
Transferring mysql-5.5.32.tar.gz...
100% 24019 KB 12009 KB/sec 00:00:02 0 Errors
[root@backup tools]# tar -xzf cmake-2.8.8.tar.gz
[root@backup tools]# tar -xzf mysql-5.5.32.tar.gz
2.4、编译安装cmake:
[root@backup tools]# cd cmake-2.8.8
[root@backup cmake-2.8.8]# ./configure
CMake has bootstrapped. Now run gmake.
[root@backup cmake-2.8.8]# gmake
[100%] Built target foo
[root@backup cmake-2.8.8]# echo $?
0
[root@backup cmake-2.8.8]# gmake install
-- Installing: /usr/local/share/aclocal/cmake.m4
2.5、安装mysql相关依赖包:
[root@backup cmake-2.8.8]# yum install ncurses-devel -y
[root@backup cmake-2.8.8]# rpm -qa ncurses-devel
ncurses-devel-5.7-4.20090207.el6.x86_64
2.5、编译安装mysql:
创建软件安装的目录:
[root@backup ~]# mkdir /application -p
1、创建虚拟用户mysql用户和mysql组:
[root@backup ~]# groupadd mysql
[root@backup ~]# useradd mysql -Ms /sbin/nologin -g mysql
2、配置参数:
[root@backup cmake-2.8.8]# cd ../mysql-5.5.32
[root@backup mysql-5.5.32]# cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.6.34 \
-DMYSQL_DATADIR=/application/mysql-5.6.34/data \
-DMYSQL_UNIX_ADDR=/application/mysql-5.6.34/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_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 \
-DWITH_SSL=bundled \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_EMBEDDED_SERVER=1 \
-DWITH_READLINE=1 \
-DWITH_DEBUG=0
-- Performing Test HAVE_IB_GCC_ATOMIC_BUILTINS - Success
-- Performing Test HAVE_IB_ATOMIC_PTHREAD_T_GCC
-- Performing Test HAVE_IB_ATOMIC_PTHREAD_T_GCC - Success
-- Check size of pthread_t
-- Check size of pthread_t - done
-- Performing Test HAVE_PEERCRED
-- Performing Test HAVE_PEERCRED - Success
-- Configuring done
-- Generating done
-- Build files have been written to: /root/tools/mysql-5.5.32
[root@backup mysql-5.5.32]# echo $?
0
4、编译:
[root@backup mysql-5.5.32]# make
[root@backup mysql-5.5.32]# echo $?
0
5、安装:
[root@backup mysql-5.5.32]# make install
[root@backup mysql-5.5.32]# echo $?
0
6、配置软件的属主和属组:
[root@backup application]# ln -s mysql-5.6.34 mysql
[root@backup application]# chown -R mysql:mysql mysql/
7、选择配置文件:
#测试环境可以选择my-small.cnf,生产环境根据硬件的需求选择,列如:my-innodb-heavy-4G.cnf
[root@backup application]# cp mysql/support-files/my-small.cnf /etc/my.cnf
cp:是否覆盖"/etc/my.cnf"? y
8、修改/tmp/目录的权限:
[root@backup application]# chmod 1777 /tmp
[root@backup application]# ls -ld /tmp
drwxrwxrwt. 3 root root 4096 2月 15 11:05 /tmp
9、初始化mysql:
[root@backup application]# cd mysql/scripts/
#如果mysql初始化有问题,可以删除/application/mysql/data/*下面的所有内容,然后初始化即可;
[root@backup scripts]# ./mysql_install_db --basedir=/application/mysql --datadir=/application/mysql/data/ --user=mysql
Installing MySQL system tables...
OK
Filling help tables...
OK
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:
/application/mysql/bin/mysqladmin -u root password 'new-password'
/application/mysql/bin/mysqladmin -u root -h backup password 'new-password'
Alternatively you can run:
/application/mysql/bin/mysql_secure_installation
You can start the MySQL daemon with:
cd /application/mysql ; /application/mysql/bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd /application/mysql/mysql-test ; perl mysql-test-run.pl
[root@backup scripts]# echo $?
0
9、配置环境变量:
[root@backup scripts]# cd /application/
[root@backup application]# echo 'export PATH=/application/mysql/bin/:$PATH' >>/etc/profile
[root@backup application]# tail -1 /etc/profile
export PATH=/application/mysql/bin/:$PATH
[root@backup application]# source /etc/profile
[root@backup application]# which mysqldump
/application/mysql/bin/mysqldump
10、启动mysql:
[root@backup application]# cp -a mysql/support-files/mysql.server /etc/init.d/mysqld
[root@backup application]# cd /etc/init.d/ &&
[root@backup init.d]# chkconfig --add mysqld
[root@backup init.d]# cd -
[root@backup application]# chmod u+x /etc/init.d/mysqld
[root@backup application]# /etc/init.d/mysqld start
Starting MySQL.... SUCCESS!
[root@backup application]# netstat -tunlp | grep "3306"
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 29800/mysqld
[root@backup init.d]# chkconfig --list mysqld
mysqld 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
10、配置mysql密码:
默认的情况下,mysql安装完成后是没有密码就可以登录的:
[root@backup application]# mysqladmin -u root password '123456'
[root@backup application]# mysql -uroot -p123456
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.32 Source distribution
mysql>
11、优化mysql:
(1)优化mysql的权限表:
mysql> select user,host from mysql.user;
+------+-----------+
| user | host |
+------+-----------+
| root | 127.0.0.1 |
| root | ::1 |
| | backup |
| root | backup |
| | localhost |
| root | localhost |
+------+-----------+
mysql>delete from mysql.user where user='';
mysql>delete from mysql.user where host='::1';
mysql>delete from mysql.user where host='backup';
mysql> select user,host from mysql.user;
+------+-----------+
| user | host |
+------+-----------+
| root | 127.0.0.1 |
| root | localhost |
+------+-----------+
授权其它的管理员:
把root管理员干掉,添加新的管理员,可以不使用;
mysql> grant all privileges on *.* to system@'localhost' identified by '123456' with grant option;
(2)删除多余的数据库:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
mysql> drop database test;
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
2.6、总结:
1、安装总结:
mysql编译安装后生成的就是二进制mysql文件包,然后进行mysql初始化的安装操作;
网上下载的mysql二进制软件包就是mysql二进制包编译后得到的,只不过自己编译生
成的二进制包可以自己配置相关的设置,然后对mysql进行初始化的操作;
mysql的编译安装和二进制安装,后期对mysql进行初始化操作时的方法都是一样的,设置mysql软件
包的属主和属组、复制配置文件、进行初始化、配置环境变量、启动mysql、设置mysql的登录密码;
2、登录mysql故障解决办法:
pkill mysql
rm -rf /application/mysql/data/*
/application/mysql/scripts/mysql_install_db --basedir=/application/mysql --datadir=/application/mysql/data/ --user=mysql
直到看到下面两行中的OK时就可以了:
Installing MySQL system tables...
OK
Filling help tables...
OK
3、mysql编译错误:
(1)ncurses-devel库没有安装:
(2)编译中没有开启ssl参数的配置: