Centos7 编译安装MySQL-5.6
CMAKE编译安装
编译命令
- make 读取makefile里面的指令,编译程序,makefile文件里调用gcc命令去编译源文件
- cmake命令也是一个编译命令,用于一些跨平台的编译设置
编译步骤
编译安装需要下载源码包、可以定制化编译参数,路径等信息,缺点就是对新手不友好。
# 编译参数如下 tar zxf mysql-5.6.20.tar.gz cd mysql-5.6.20 cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ -DMYSQL_DATADIR=/usr/local/mysql/data \ -DWITH_MYISAM_STORAGE_ENGINE=1 \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_MEMORY_STORAGE_ENGINE=1 \ -DEXTRA_CHARSETS=all \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci \ -DWITH_DEBUG=0 && make && make install && ln -svf /usr/local/mysql/bin/* /usr/local/bin/ #提示,编译时可配置的选项很多,具体可参考本章最后一部分的内容或官方文档。 make make install
#!/bin/bash yum install ncurses-devel libaio-devel -y yum install cmake -y yum -y install gcc-c++ groupadd mysql && useradd mysql -g mysql -M -s /sbin/nologin tar xf mysql-5.6.39.tar.gz cd mysql-5.6.39 cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ -DMYSQL_DATADIR=/usr/local/mysql/data \ -DWITH_MYISAM_STORAGE_ENGINE=1 \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_MEMORY_STORAGE_ENGINE=1 \ -DEXTRA_CHARSETS=all \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci \ -DWITH_DEBUG=0 && make && make install && ln -svf /usr/local/mysql/bin/* /usr/local/bin/ cd /usr/local/mysql chown -R mysql . chgrp -R mysql . /bin/cp -vf support-files/*.cnf /etc/my.cnf
初始化数据库
这一步得到数据库初始化的数据文件
这一步会出现两个OK选项,以及其他日志信息,不得出现error等信息,则表示初始化出问题
出现两个ok表示 初始化成功
/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data --user=mysql
检查数据文件夹
看到如下数据目录,表示mysql初始化完成
cd /usr/local/mysql/data/ ll total 110724 -rw-rw---- 1 mysql mysql 12582912 Jun 30 12:21 ibdata1 -rw-rw---- 1 mysql mysql 50331648 Jun 30 12:21 ib_logfile0 -rw-rw---- 1 mysql mysql 50331648 Jun 30 12:21 ib_logfile1 drwx------ 2 mysql mysql 4096 Jun 30 12:21 mysql //存放mysql核心数据 drwx------ 2 mysql mysql 4096 Jun 30 12:21 // 有关性能的库performance_schema drwxr-xr-x 2 mysql mysql 4096 Jun 30 09:37 test //测试数据库
启动mysql
配置mysql启动脚本
启动脚本
[ -f /etc/init.d/functions ] && source /etc/init.d/functions bindir="/usr/local/mysql/bin" datadir="/usr/local/mysql/data" mysqld_pid_file_path="/usr/local/mysql/`hostname`.pid" PATH="/sbin:/usr/sbin:/bin:/usr/bin:$basedir/bin" #此步对开机启动及定时启动及其关键。 export PATH return_value=0 # Lock directory. lockdir='/var/lock/subsys' lock_file_path="$lockdir/mysql" log_success_msg(){ echo " SUCCESS! $@" # 注意函数的缩进,下同,也是专业的表现,可放到functions里。 } log_failure_msg(){ echo " ERROR! $@" } # Start Func start(){ # Start daemon echo "Starting MySQL" if test -x $bindir/mysqld_safe # 启动文件是否可执行。 then $bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" >/dev/null & return_value=$? # 是否处理好返回值是区别脚本是否专业规范的关键。 sleep 2 # Make lock for CentOS if test -w "$lockdir" # 锁目录是否可写。 then touch "$lock_file_path" # 创建锁文件。 fi exit $return_value else log_failure_msg "Couldn't find MySQL server ($bindir/mysqld_safe)" fi } # Stop Func stop(){ if test -s "$mysqld_pid_file_path" # 是否PID文件存在并大小大于0。 then mysqld_pid=`cat "$mysqld_pid_file_path"` if (kill -0 $mysqld_pid 2>/dev/null) # 检查PID对应的进程是否存在。 then echo "Shutting down MySQL" kill $mysqld_pid # 不能带-9,否则后果自负。 return_value=$? sleep 2 else log_failure_msg "MySQL server process #$mysqld_pid is not running!" rm -f "$mysqld_pid_file_path" fi # Delete lock for Oldboy's CentOS if test -f "$lock_file_path" then rm -f "$lock_file_path" fi exit $return_value else log_failure_msg "MySQL server PID file could not be found!" fi } case "$1" in start) start ;; stop) stop ;; restart) if $0 stop; then $0 start else log_failure_msg "Failed to stop running server, so refusing to try to start." exit 1 fi ;; *) echo "Usage: $0 {start|stop|restart}" exit 1 esac exit $return_value #是否处理好返回值是区别脚本是否专业规范的关键。
启动mysql自动生成的管理脚本
cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod 700 /etc/init.d/mysqld
启动mysql
cp -a /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
/etc/init.d/mysqld status
ERROR! MySQL is not running
/etc/init.d/mysqld start Starting MySQL.Logging to '/usr/local/mysql/data/VM-7-185-centos.err'. SUCCESS! ps aux |grep mysql root 31820 0.0 0.0 106244 1420 pts/1 S 12:18 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/VM-7-185-centos.pid mysql 31928 6.0 23.4 1339780 450424 pts/1 Sl 12:18 0:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=VM-7-185-centos.err --pid-file=/usr/local/mysql/data/VM-7-185-centos.pid root 31958 0.0 0.0 103328 944 pts/1 S+ 12:18 0:00 grep --color mysql
初始化
/bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/data/mysql/mysql_master/my.cnf --basedir=/usr/local/mysql/ --datadir=/data/mysql/mysql_master/data/ --user=mysql