一、环境
系统:CentOS 6.4x64最小化安装
IP:192.168.3.54
二、安装基础软件包
[root@httpd conf]# yum -y install make gcc-c++ cmake bison-devel ncurses-devel
三、安装mysql
1.创建用户
[root@httpd ~]# groupadd mysql [root@httpd ~]# useradd -g mysql mysql -s /sbin/nologin
2.解压软件包
[root@httpd ~]# tar xf mysql-5.5.37.tar.gz
3.编译配置参数
#创建用来存放Mysql数据的目录 [root@httpd ~]# mkdir -p /data/mysql/data [root@httpd ~]# cd mysql-5.5.37 [root@httpd mysql-5.5.37]# cmake \ > -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.5.37 \ > -DMYSQL_DATADIR=/data/mysql/data \ > -DSYSCONFDIR=/etc \ > -DWITH_MYISAM_STORAGE_ENGINE=1 \ > -DWITH_INNOBASE_STORAGE_ENGINE=1 \ > -DWITH_MEMORY_STORAGE_ENGINE=1 \ > -DWITH_READLINE=1 \ > -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \ > -DMYSQL_TCP_PORT=3306 \ > -DENABLED_LOCAL_INFILE=1 \ > -DWITH_PARTITION_STORAGE_ENGINE=1 \ > -DEXTRA_CHARSETS=all \ > -DDEFAULT_CHARSET=utf8 \ > -DDEFAULT_COLLATION=utf8_general_ci [root@httpd mysql-5.5.37]# make && make install
4.数据目录初始化
[root@httpd mysql-5.5.37]# cd /usr/local/mysql-5.5.37/ [root@httpd mysql-5.5.37]# scripts/mysql_install_db --datadir=/data/mysql/data/ --user=mysql --basedir=/usr/local/mysql-5.5.37/ Installing MySQL system tables... OK Filling help tables... OK #到这里显示的有2个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: /usr/local/mysql-5.5.37//bin/mysqladmin -u root password 'new-password' /usr/local/mysql-5.5.37//bin/mysqladmin -u root -h httpd password 'new-password' Alternatively you can run: /usr/local/mysql-5.5.37//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. You can start the MySQL daemon with: cd /usr/local/mysql-5.5.37/ ; /usr/local/mysql-5.5.37//bin/mysqld_safe & You can test the MySQL daemon with mysql-test-run.pl cd /usr/local/mysql-5.5.37//mysql-test ; perl mysql-test-run.pl Please report any problems at http://bugs.mysql.com/
5.复制mysql配置文件
[root@httpd mysql-5.5.37]# cp -rf support-files/my-large.cnf /etc/my.cnf
6.创建启动脚本
[root@httpd mysql-5.5.37]# cp support-files/mysql.server /etc/init.d/mysqld [root@httpd mysql-5.5.37]# chmod +x /etc/init.d/mysqld [root@httpd mysql-5.5.37]# /etc/init.d/mysqld start Starting MySQL.. SUCCESS! [root@httpd mysql-5.5.37]# netstat -anpt |grep mysql tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 21814/mysqld
7.连接到数据库
[root@httpd ~]# which mysql /usr/bin/which: no mysql in (/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin) #配置软连接 [root@httpd ~]# ln -s /usr/local/mysql-5.5.37/ /usr/local/mysql [root@httpd ~]# ln -s /usr/local/mysql-5.5.37/bin/* /usr/sbin/ [root@httpd ~]# which mysql /usr/sbin/mysql 连接到数据库 [root@httpd ~]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.5.37-log Source distribution Copyright (c) 2000, 2014, 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> #默认的情况下是没有密码的,设置root账号的密码,设置密码是ly36843 mysql> update user set password=password('lyao36843') where host="localhost" and user="root"; Query OK, 1 row affected (0.03 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> update user set password=password('lyao36843') where host="127.0.0.1" and user="root"; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select user,host,password from mysql.user; +------+-----------+-------------------------------------------+ | user | host | password | +------+-----------+-------------------------------------------+ | root | localhost | *B181A5BCA7C882221F5B8F6F9657AE71FF67EDDB | | root | httpd | | | root | 127.0.0.1 | *B181A5BCA7C882221F5B8F6F9657AE71FF67EDDB | | root | ::1 | | | | localhost | | | | httpd | | +------+-----------+-------------------------------------------+ 6 rows in set (0.00 sec) #清除mysql用户表中不安全的用户 mysql> delete from mysql.user where password=''; Query OK, 4 rows affected (0.03 sec) mysql> select user,host,password from mysql.user; +------+-----------+-------------------------------------------+ | user | host | password | +------+-----------+-------------------------------------------+ | root | localhost | *B181A5BCA7C882221F5B8F6F9657AE71FF67EDDB | | root | 127.0.0.1 | *B181A5BCA7C882221F5B8F6F9657AE71FF67EDDB | +------+-----------+-------------------------------------------+ 2 rows in set (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) #退出数据库重新连接 [root@httpd ~]# mysql -u root -p -h 127.0.0.1 Enter password: #输入之前设置的mysql数据库密码 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.5.37-log Source distribution Copyright (c) 2000, 2014, 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>
8.最后将mysql添加到开机自动启动
[root@httpd ~]# chkconfig --add mysqld [root@httpd ~]# chkconfig mysqld on [root@httpd ~]# chkconfig mysqld --list mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
最后附上mysql安装脚本,mysql安装文件需要实现下载好,不同的版本只需要更改变量VERSION即可
,该脚本在运行时,需要提供mysql的root账号密码。
#!/bin/bash DATADIR='/data/mysql/data' VERSION='mysql-5.5.37' export LANG=zh_CN.UTF-8 #Source function library. . /etc/init.d/functions #camke install mysql5.5.X install_mysql(){ read -p "please input a password for root: " PASSWD if [ ! -d $DATADIR ];then mkdir -p $DATADIR fi yum install cmake make gcc-c++ bison-devel ncurses-devel -y id mysql &>/dev/null if [ $? -ne 0 ];then useradd mysql -s /sbin/nologin -M fi #useradd mysql -s /sbin/nologin -M #change datadir owner to mysql chown -R mysql.mysql $DATADIR cd #wget http://mirrors.sohu.com/mysql/MySQL-5.5/mysql-5.5.38.tar.gz tar xf $VERSION.tar.gz cd $VERSION cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/$VERSION \ -DMYSQL_DATADIR=$DATADIR \ -DMYSQL_UNIX_ADDR=$DATADIR/mysql.sock \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci \ -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 make && make install if [ $? -ne 0 ];then action "install mysql is failed" /bin/false exit $? fi sleep 2 #link ln -s /usr/local/$VERSION/ /usr/local/mysql ln -s /usr/local/mysql/bin/* /usr/bin/ #copy config and start file /bin/cp /usr/local/mysql/support-files/my-small.cnf /etc/my.cnf cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld chmod 700 /etc/init.d/mysqld #init mysql /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=$DATADIR --user=mysql if [ $? -ne 0 ];then action "install mysql is failed" /bin/false exit $? fi #check mysql /etc/init.d/mysqld start if [ $? -ne 0 ];then action "mysql start is failed" /bin/false exit $? fi chkconfig --add mysqld chkconfig mysqld on /usr/local/mysql/bin/mysql -e "update mysql.user set password=password('$PASSWD') where host='localhost' and user='root';" /usr/local/mysql/bin/mysql -e "update mysql.user set password=password('$PASSWD') where host='127.0.0.1' and user='root';" /usr/local/mysql/bin/mysql -e "delete from mysql.user where password='';" /usr/local/mysql/bin/mysql -e "flush privileges;" #/usr/local/mysql/bin/mysql -e "select version();" >/dev/null 2>&1 if [ $? -eq 0 ];then echo "+---------------------------+" echo "+------mysql安装完成--------+" echo "+---------------------------+" fi #/etc/init.d/mysqld stop } install_mysql