先来张图
在编译安装前先看下centos6.8系统自带的yum源上面的rpm包装的mysql-server都有哪些内容
[root@localhost ~]#yum info mysql-server Loaded plugins: fastestmirror, refresh-packagekit, security Loading mirror speeds from cached hostfile Installed Packages Name : mysql-server Arch : x86_64 Version : 5.1.73 Release : 7.el6 Size : 25 M Repo : installed From repo : base Summary : The MySQL server and related files URL : http://www.mysql.com License : GPLv2 with exceptions Description : MySQL is a multi-user, multi-threaded SQL database server. MySQL is a : client/server implementation consisting of a server daemon (mysqld) : and many different client programs and libraries. This package contains : the MySQL server and some accompanying files and directories.
看下都安装了哪些文件
[root@localhost ~]#rpm -ql mysql-server /etc/logrotate.d/mysqld /etc/rc.d/init.d/mysqld /usr/bin/innochecksum /usr/bin/myisam_ftdump /usr/bin/myisamchk /usr/bin/myisamlog /usr/bin/myisampack /usr/bin/mysql_convert_table_format /usr/bin/mysql_fix_extensions /usr/bin/mysql_fix_privilege_tables /usr/bin/mysql_install_db /usr/bin/mysql_secure_installation /usr/bin/mysql_setpermission /usr/bin/mysql_tzinfo_to_sql /usr/bin/mysql_upgrade /usr/bin/mysql_zap /usr/bin/mysqlbug /usr/bin/mysqld_multi /usr/bin/mysqld_safe /usr/bin/mysqldumpslow /usr/bin/mysqlhotcopy /usr/bin/mysqltest /usr/bin/perror /usr/bin/replace /usr/bin/resolve_stack_dump /usr/bin/resolveip /usr/lib64/mysql/plugin /usr/lib64/mysql/plugin/ha_archive.so /usr/lib64/mysql/plugin/ha_archive.so.0 /usr/lib64/mysql/plugin/ha_archive.so.0.0.0 /usr/lib64/mysql/plugin/ha_blackhole.so /usr/lib64/mysql/plugin/ha_blackhole.so.0 /usr/lib64/mysql/plugin/ha_blackhole.so.0.0.0 /usr/lib64/mysql/plugin/ha_example.so /usr/lib64/mysql/plugin/ha_example.so.0 /usr/lib64/mysql/plugin/ha_example.so.0.0.0 /usr/lib64/mysql/plugin/ha_federated.so /usr/lib64/mysql/plugin/ha_federated.so.0 /usr/lib64/mysql/plugin/ha_federated.so.0.0.0 /usr/lib64/mysql/plugin/ha_innodb_plugin.so /usr/lib64/mysql/plugin/ha_innodb_plugin.so.0 /usr/lib64/mysql/plugin/ha_innodb_plugin.so.0.0.0 /usr/libexec/mysqld /usr/libexec/mysqlmanager /usr/share/mysql/mysql_test_data_timezone.sql /var/lib/mysql /var/log/mysqld.log /var/run/mysqld
mysql客户端文件列表
[root@localhost ~]#rpm -ql mysql /usr/bin/msql2mysql /usr/bin/my_print_defaults /usr/bin/mysql /usr/bin/mysql_config /usr/bin/mysql_find_rows /usr/bin/mysql_waitpid /usr/bin/mysqlaccess /usr/bin/mysqladmin /usr/bin/mysqlbinlog /usr/bin/mysqlcheck /usr/bin/mysqldump /usr/bin/mysqlimport /usr/bin/mysqlshow /usr/bin/mysqlslap /usr/lib64/mysql/mysql_config /usr/lib64/mysql/mysqlbug
查看日志滚动配置
[root@localhost ~]#cat /etc/logrotate.d/mysqld # This logname can be set in /etc/my.cnf # by setting the variable "err-log" # in the [safe_mysqld] section as follows: # # [safe_mysqld] # err-log=/var/log/mysqld.log # # If the root user has a password you have to create a # /root/.my.cnf configuration file with the following # content: # # [mysqladmin] # password = <secret> # user= root # # where "<secret>" is the password. # # ATTENTION: This /root/.my.cnf should be readable ONLY # for root ! # Then, un-comment the following lines to enable rotation of mysql's log file: #/var/log/mysqld.log { # create 640 mysql mysql # notifempty # daily # rotate 3 # missingok # compress # postrotate # # just if mysqld is really running # if test -x /usr/bin/mysqladmin && \ # /usr/bin/mysqladmin ping &>/dev/null # then # /usr/bin/mysqladmin flush-logs # fi # endscript #}
查看service文件,真多
[root@localhost ~]#cat /etc/init.d/mysqld #!/bin/sh # # mysqld This shell script takes care of starting and stopping # the MySQL subsystem (mysqld). # # chkconfig: - 64 36 # description: MySQL database server. # processname: mysqld # config: /etc/my.cnf # pidfile: /var/run/mysqld/mysqld.pid ### BEGIN INIT INFO # Provides: mysqld # Required-Start: $local_fs $remote_fs $network $named $syslog $time # Required-Stop: $local_fs $remote_fs $network $named $syslog $time # Short-Description: start and stop MySQL server # Description: MySQL database server ### END INIT INFO # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network exec="/usr/bin/mysqld_safe" prog="mysqld" # Set timeouts here so they can be overridden from /etc/sysconfig/mysqld STARTTIMEOUT=120 STOPTIMEOUT=60 [ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog lockfile=/var/lock/subsys/$prog # extract value of a MySQL option from config files # Usage: get_mysql_option SECTION VARNAME DEFAULT # result is returned in $result # We use my_print_defaults which prints all options from multiple files, # with the more specific ones later; hence take the last match. get_mysql_option(){ result=`/usr/bin/my_print_defaults "$1" | sed -n "s/^--$2=//p" | tail -n 1` if [ -z "$result" ]; then # not found, use default result="$3" fi } get_mysql_option mysqld datadir "/var/lib/mysql" datadir="$result" get_mysql_option mysqld socket "$datadir/mysql.sock" socketfile="$result" get_mysql_option mysqld_safe log-error "/var/log/mysqld.log" errlogfile="$result" get_mysql_option mysqld_safe pid-file "/var/run/mysqld/mysqld.pid" mypidfile="$result" start(){ [ -x $exec ] || exit 5 # check to see if it's already running MYSQLDRUNNING=0 if [ -f "$mypidfile" ]; then MYSQLPID=`cat "$mypidfile" 2>/dev/null` if [ -n "$MYSQLPID" ] && [ -d "/proc/$MYSQLPID" ] ; then MYSQLDRUNNING=1 fi fi RESPONSE=`/usr/bin/mysqladmin --socket="$socketfile" --user=UNKNOWN_MYSQL_USER ping 2>&1` if [ $MYSQLDRUNNING = 1 ] && [ $? = 0 ]; then # already running, do nothing action $"Starting $prog: " /bin/true ret=0 elif [ $MYSQLDRUNNING = 1 ] && echo "$RESPONSE" | grep -q "Access denied for user" then # already running, do nothing action $"Starting $prog: " /bin/true ret=0 else # prepare for start touch "$errlogfile" 2>/dev/null if [ $? -ne 0 ]; then # failed to touch log file, probably insufficient permissions action $"Starting $prog: " /bin/false return 4 fi chown mysql:mysql "$errlogfile" chmod 0640 "$errlogfile" [ -x /sbin/restorecon ] && /sbin/restorecon "$errlogfile" if [ ! -d "$datadir/mysql" ] ; then # First, make sure $datadir is there with correct permissions if [ ! -e "$datadir" -a ! -h "$datadir" ] then mkdir -p "$datadir" || exit 1 fi chown mysql:mysql "$datadir" chmod 0755 "$datadir" [ -x /sbin/restorecon ] && /sbin/restorecon "$datadir" # Now create the database action $"Initializing MySQL database: " /usr/bin/mysql_install_db --datadir="$datadir" --user=mysql ret=$? chown -R mysql:mysql "$datadir" if [ $ret -ne 0 ] ; then return $ret fi fi chown mysql:mysql "$datadir" chmod 0755 "$datadir" # We check if there is already a process using the socket file, # since otherwise this init script could report false positive # result and mysqld_safe would remove the socket file, which # actually uses a different daemon. if fuser "$socketfile" &>/dev/null ; then echo "Socket file $socketfile exists. Is another MySQL daemon already running with the same unix socket?" action $"Starting $prog: " /bin/false return 1 fi # Pass all the options determined above, to ensure consistent behavior. # In many cases mysqld_safe would arrive at the same conclusions anyway # but we need to be sure. (An exception is that we don't force the # log-error setting, since this script doesn't really depend on that, # and some users might prefer to configure logging to syslog.) # Note: set --basedir to prevent probes that might trigger SELinux # alarms, per bug #547485 $exec --datadir="$datadir" --socket="$socketfile" \ --pid-file="$mypidfile" \ --basedir=/usr --user=mysql >/dev/null 2>&1 & safe_pid=$! # Spin for a maximum of N seconds waiting for the server to come up; # exit the loop immediately if mysqld_safe process disappears. # Rather than assuming we know a valid username, accept an "access # denied" response as meaning the server is functioning. ret=0 TIMEOUT="$STARTTIMEOUT" while [ $TIMEOUT -gt 0 ]; do RESPONSE=`/usr/bin/mysqladmin --socket="$socketfile" --user=UNKNOWN_MYSQL_USER ping 2>&1` mret=$? if [ $mret -eq 0 ]; then break fi # exit codes 1, 11 (EXIT_CANNOT_CONNECT_TO_SERVICE) are expected, # anything else suggests a configuration error if [ $mret -ne 1 -a $mret -ne 11 ]; then echo "$RESPONSE" echo "Cannot check for MySQL Daemon startup because of mysqladmin failure." ret=1 break fi echo "$RESPONSE" | grep -q "Access denied for user" && break if ! /bin/kill -0 $safe_pid 2>/dev/null; then echo "MySQL Daemon failed to start." ret=1 break fi sleep 1 let TIMEOUT=${TIMEOUT}-1 done if [ $TIMEOUT -eq 0 ]; then echo "Timeout error occurred trying to start MySQL Daemon." ret=1 fi if [ $ret -eq 0 ]; then action $"Starting $prog: " /bin/true chmod o+r $mypidfile >/dev/null 2>&1 touch $lockfile else action $"Starting $prog: " /bin/false fi fi return $ret } stop(){ if [ ! -f "$mypidfile" ]; then # not running; per LSB standards this is "ok" action $"Stopping $prog: " /bin/true return 0 fi MYSQLPID=`cat "$mypidfile" 2>/dev/null` if [ -n "$MYSQLPID" ]; then /bin/kill "$MYSQLPID" >/dev/null 2>&1 ret=$? if [ $ret -eq 0 ]; then TIMEOUT="$STOPTIMEOUT" while [ $TIMEOUT -gt 0 ]; do /bin/kill -0 "$MYSQLPID" >/dev/null 2>&1 || break sleep 1 let TIMEOUT=${TIMEOUT}-1 done if [ $TIMEOUT -eq 0 ]; then echo "Timeout error occurred trying to stop MySQL Daemon." ret=1 action $"Stopping $prog: " /bin/false else rm -f $lockfile rm -f "$socketfile" action $"Stopping $prog: " /bin/true fi else action $"Stopping $prog: " /bin/false fi else # failed to read pidfile, probably insufficient permissions action $"Stopping $prog: " /bin/false ret=4 fi return $ret } restart(){ stop start } condrestart(){ [ -e $lockfile ] && restart || : } # See how we were called. case "$1" in start) start ;; stop) stop ;; status) status -p "$mypidfile" $prog ;; restart) restart ;; condrestart|try-restart) condrestart ;; reload) exit 3 ;; force-reload) restart ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}" exit 2 esac exit $?
上面文件简单介绍
首先由一个函数:get_mysql_option函数,作用是找出定义的数据目录,socket文件,错误日志,pid文件,如果没有定义的话,就使用后面的默认的文件
get_mysql_option mysqld datadir "/var/lib/mysql" datadir="$result" get_mysql_option mysqld socket "$datadir/mysql.sock" socketfile="$result" get_mysql_option mysqld_safe log-error "/var/log/mysqld.log" errlogfile="$result" get_mysql_option mysqld_safe pid-file "/var/run/mysqld/mysqld.pid" mypidfile="$result"
下面是定义的设置的文件
[root@localhost ~]#/usr/bin/my_print_defaults mysqld --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --user=mysql --symbolic-links=0 [root@localhost ~]#/usr/bin/my_print_defaults mysqld_safe --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid
=======================================================================
下面是编译安装mysqld,之后会将其自动化为脚本文件,
操作系统:centos6.8
mysql版本:mysql-5.5.55
说明:除了日志目录和数据目录之外,所有mysql相关的文件都在一个目录下面,便于管理,包括配置文件,socket文件,pid文件,
1、编译安装cmake
下载页面,这个版本是现在的最新的版本:https://cmake.org/download/
[root@localhost ~]#wget https://cmake.org/files/v3.8/cmake-3.8.0.tar.gz [root@localhost ~]#tar xf cmake-3.8.0.tar.gz [root@localhost ~]#ls cmake-3.8.0 cmake-3.8.0.tar.gz [root@localhost ~]#cd cmake-3.8.0 [root@localhost ~/cmake-3.8.0]#ls Auxiliary CMakeGraphVizOptions.cmake CompileFlags.cmake CTestConfig.cmake Help README.rst Utilities bootstrap CMakeLists.txt configure CTestCustom.cmake.in Licenses Source CMakeCPack.cmake CMakeLogo.gif CONTRIBUTING.rst DartConfig.cmake Modules Templates CMakeCPackOptions.cmake.in cmake_uninstall.cmake.in Copyright.txt doxygen.config Packaging Tests [root@localhost ~/cmake-3.8.0]#./bootstrap --help #查看帮助,可以指定文件目录位置等参数 [root@localhost ~/cmake-3.8.0]#yum install gcc gcc-c++ ncurses-devel perl -y #安装一些必要的依赖包 [root@localhost ~/cmake-3.8.0]#./bootstrap --prefix=/usr/local/cmake && make && make install [root@localhost /usr/local/cmake]#ll #安装后的文件 total 12 drwxr-xr-x 2 root root 4096 Apr 15 15:32 bin drwxr-xr-x 3 root root 4096 Apr 15 15:32 doc drwxr-xr-x 4 root root 4096 Apr 15 15:32 share [root@localhost ~]#vim path.sh #将路径添加到环境变量中,就可以直接使用命令网络 #!/bin/bash cat > /etc/profile.d/cmake.sh << EOF PATH=/usr/local/cmake/bin:\$PATH export PATH EOF source /etc/profile.d/cmake.sh [root@localhost ~]#cmake a CMake Error: The source directory "/root/a" does not exist. Specify --help for usage, or press the help button on the CMake GUI.
如果感觉上面的麻烦,yum源里面默认有cmake软件,直接安装就可以使用
2、编译安装mysqld
安装版本:mysql-5.5.55
下载页面:https://dev.mysql.com/downloads/mysql/5.5.html#downloads
准备工作
[root@localhost ~]#useradd -r mysql -s /sbin/nologin #系统用户 [root@localhost ~]#id mysql uid=495(mysql) gid=491(mysql) groups=491(mysql) [root@localhost ~]#yum -y install gcc gcc-c++ gcc-g77 autoconf automake zlib* fiex* libxml* ncurses-devel libmcrypt* \ libtool-ltdl-devel* make cmake curl freetype libjpeg-turbo libjpeg-turbo-devel \ openjpeg-libs libpng gd ncurses git bison openssl-devel libaio-devel #安装依赖的devel包,如果显示缺少什么包,安装什么包
下载
[root@localhost ~]#wget https://dev.mysql.com/get/Downloads/MySQL-5.5/mysql-5.5.55.tar.gz [root@localhost ~]#tar xf mysql-5.5.55.tar.gz [root@localhost ~]#cd mysql-5.5.55 [root@localhost ~/mysql-5.5.55]#ls BUILD CMakeLists.txt COPYING include libmysqld mysys regex sql-common tests win BUILD-CMAKE cmd-line-utils dbug INSTALL-SOURCE libservices packaging scripts storage unittest zlib client config.h.cmake Docs INSTALL-WIN-SOURCE man plugin sql strings VERSION cmake configure.cmake extra libmysql mysql-test README sql-bench support-files vio
这里设置配置文件,具体的含义见下面。pid文件,socket文件都在安装的目录里面,便于统一管理,另外还有就是数据库文件目录,日志文件目录这两个单独的文件目录
这里是官方的说明:https://dev.mysql.com/doc/refman/5.5/en/source-configuration-options.html
[root@localhost ~/mysql-5.5.55]#/usr/local/cmake/bin/cmake \ -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ -DMYSQL_UNIX_ADDR=/usr/local/mysql/run/mysql.sock \ -DMYSQL_DATADIR=/data/mysql \ -DSYSCONFDIR=/usr/local/mysql/conf \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_MYISAM_STORAGE_ENGINE=1 \ -DWITH_READLINE=1 \ -DWITH_SSL=system \ -DWITH_ZLIB=system \ -DWITH_LIBWRAP=0 \ -DMYSQL_TCP_PORT=3306 \ -DENABLED_LOCAL_INFILE=1 \ -DWITH_PARTITION_STORAGE_ENGINE=1 \ -DEXTRA_CHARSETS=all \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci
开始编译安装,指定编译核心数4,提高速度
[root@localhost ~/mysql-5.5.55]#make -j 4 && make install #安装复制
如果编译过程中出现错误,清理中间文件,根据错误提示,通常是缺乏相关的devel包导致,解决问题后,重新编译
[root@localhost ~/mysql-5.5.55]#make clean make: *** No rule to make target `clean'. Stop. [root@localhost ~/mysql-5.5.55]#rm CMakeCache.txt rm: remove regular file `CMakeCache.txt'? y
编译完成后生成执行文件目录
[root@bogon /usr/local/mysql]#ll #编译后目录 total 72 drwxr-xr-x 2 root root 4096 Apr 17 14:47 bin -rw-r--r-- 1 root root 17987 Mar 18 12:41 COPYING drwxr-xr-x 3 root root 4096 Apr 17 14:47 data drwxr-xr-x 2 root root 4096 Apr 17 14:47 docs drwxr-xr-x 3 root root 4096 Apr 17 14:47 include -rw-r--r-- 1 root root 301 Mar 18 12:41 INSTALL-BINARY drwxr-xr-x 3 root root 4096 Apr 17 14:47 lib drwxr-xr-x 4 root root 4096 Apr 17 14:47 man drwxr-xr-x 10 root root 4096 Apr 17 14:47 mysql-test -rw-r--r-- 1 root root 2496 Mar 18 12:41 README drwxr-xr-x 2 root root 4096 Apr 17 14:47 scripts drwxr-xr-x 27 root root 4096 Apr 17 14:47 share drwxr-xr-x 4 root root 4096 Apr 17 14:47 sql-bench drwxr-xr-x 2 root root 4096 Apr 17 14:47 support-files
将执行文件路径添加到系统路径中,并重读此文件
[root@localhost ~]#cat /etc/profile.d/mysql.sh PATH=/usr/local/mysql/bin:/usr/local/mysql/lib/:$PATH export PATH [root@localhost ~]#source /etc/profile.d/mysql.sh
将系统提供的默认配置文件复制修改为自己需要的配置文件,上面编译的时候设置的参数最后都是写到了这些配置文件中了,比如各个文件路径等信息。配置文件里面的内容,根据自己需要修改,这里只是安装相关的内容,具体的参数配置见相关文章
[root@localhost /usr/local/mysql]#mkdir conf [root@localhost /usr/local/mysql]#cp support-files/my-large.cnf conf/my.cnf [root@localhost /usr/local/mysql/conf]#cat my.cnf #下面是部分参数设置,设置的时候都要设置 [client] #password = your_password port = 3306 socket = /usr/local/mysql/run/mysql.sock # Here follows entries for some specific programs # The MySQL server [mysqld] port = 3306 socket = /usr/local/mysql/run/mysql.sock datadir = /data/mysql user = mysql skip-external-locking key_buffer_size = 256M max_allowed_packet = 1M table_open_cache = 256 sort_buffer_size = 1M read_buffer_size = 1M read_rnd_buffer_size = 4M myisam_sort_buffer_size = 64M thread_cache_size = 8 query_cache_size= 16M # Try number of CPU's*2 for thread_concurrency thread_concurrency = 8 [mysqld_safe] log-error=/var/log/mysqld/mysqld.log pid-file=/usr/local/mysql/run/mysqld.pid
修改读取配置的顺序,
[root@localhost ~]#/usr/local/mysql/bin/my_print_defaults #默认的配置读取顺序,如果/etc/my.cnf里面的配置和设置的不同,就比较麻烦 Default options are read from the following files in the given order: /etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/conf/my.cnf ~/.my.cnf [root@localhost ~]#/usr/local/mysql/bin/my_print_defaults #读取的结果,不是想要的,是读的/etc/my.cnf里面的配置 --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --user=mysql --symbolic-links=0 [root@localhost ~]#/usr/local/mysql/bin/my_print_defaults --defaults-file=/usr/local/mysql/conf/my.cnf #设置只读自定义的这个配置文件 Default options are read from the following files in the given order: /usr/local/mysql/conf/my.cnf
发现重启一个终端后还是原来的读取顺序,如果一个变量在两个文件中都设置了,以后面文件中设置的变量为准,这里可以从service文件中看到执行过程
为了安全,如果/etc/目录下面有my.cnf文件,将其修改为其他文件名,会干扰程序识别正常的配置文件,比如后面配置没有设置datadir,则前面定义的就生效了
[[ -e /etc/my.cnf ]] && mv /etc/my.cnf{,.bak}
上面设置好后,下面重复的变量,以后面文件定义的为准,相当于重复赋值,后面设置的生效
[root@localhost ~]#my_print_defaults mysqld_safe --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --log-error=/var/log/mysqld/mysqld.log --pid-file=/usr/local/mysql/run/mysqld.pid [root@localhost ~]#my_print_defaults mysqld --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --user=mysql --symbolic-links=0 --port=3306 --socket=/usr/local/mysql/run/mysql.sock --datadir=/data/mysql --user=mysql
数据目录和一些初始化的表结构现在还没有,添加数据目录。利用下面的可执行文件生成数据目录和所需要的各个数据表结构,必须指定basedir,它会以basedir目录为基准,找bin目录下的可执行文件,find ./bin/my_print_defaults
猜测执行过程,mysql_install_db会根据my_print_defaults里面设置的默认配置文件路径,找到配置文件,(上面编译完成后是否会改变此默认配置)根据配置文件里面的设置内容去创建数据库,和一些表结构,并将配置文件里面的某些配置(比如各个配置路径)写入到创建的数据库文件中,包括文件的权限也会自动修改为mysql,这个动作不用手动执行,启动数据库脚本的时候,会检测,没有的话,会自动创建目录,创建初始化数据库文件
[root@localhost ~]#/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --user=mysql --datadir=/data/mysql [root@localhost /usr/local/mysql]#cd /data/ [root@localhost /data]#ll total 4 drwx------. 5 mysql root 4096 Jun 10 17:30 mysql [root@localhost /data]#cd mysql/ [root@localhost /data/mysql]#ll total 1144 drwx------. 2 mysql root 4096 Jun 10 17:30 mysql -rw-rw----. 1 mysql mysql 27735 Jun 10 17:30 mysql-bin.000001 -rw-rw----. 1 mysql mysql 1123841 Jun 10 17:30 mysql-bin.000002 -rw-rw----. 1 mysql mysql 38 Jun 10 17:30 mysql-bin.index drwx------. 2 mysql mysql 4096 Jun 10 17:30 performance_schema drwx------. 2 mysql root 4096 Jun 10 17:30 test
配置日志滚动
[root@localhost /usr/local/mysql/support-files]#vim /etc/logrotate.d/mysql /var/log/mysqld/*.log { # create 600 mysql mysql notifempty daily rotate 10 missingok compress postrotate # just if mysqld is really running if test -x /usr/local/mysql/bin/mysqladmin && \ /usr/local/mysql/bin/mysqladmin ping &>/dev/null then /usr/local/mysql/bin/mysqladmin flush-logs fi endscript }
添加service服务文件,添加执行权限,并开机启动
# 这里的这个文件不用,里面有些目录感觉有问题,自己造轮子 [root@localhost /usr/local/mysql]#cp support-files/mysql.server /etc/init.d/mysqld [root@localhost ~]#cat /etc/init.d/mysqld #!/bin/sh # # mysqld This shell script takes care of starting and stopping # the MySQL subsystem (mysqld). # # chkconfig: - 64 36 # description: MySQL database server. # processname: mysqld # config: /usr/local/mysql/conf/my.cnf # pidfile: /usr/local/mysql/run/mysqld.pid ### BEGIN INIT INFO # Provides: mysqld # Required-Start: $local_fs $remote_fs $network $named $syslog $time # Required-Stop: $local_fs $remote_fs $network $named $syslog $time # Short-Description: start and stop MySQL server # Description: MySQL database server ### END INIT INFO # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Set timeouts here so they can be overridden from /etc/sysconfig/mysqld STARTTIMEOUT=120 STOPTIMEOUT=60 prog="mysqld" base_dir="/usr/local/mysql" lockfile="/usr/local/mysql/run/lock/$prog" bin_dir="/usr/local/mysql/bin" install_db_dir="/usr/local/mysql/scripts" exec="/usr/local/mysql/bin/mysqld_safe" # extract value of a MySQL option from config files # Usage: get_mysql_option SECTION VARNAME DEFAULT # result is returned in $result # We use my_print_defaults which prints all options from multiple files, # with the more specific ones later; hence take the last match. get_mysql_option(){ result=`${bin_dir}/my_print_defaults "$1" | sed -n "s/^--$2=//p" | tail -n 1` if [ -z "$result" ]; then # not found, use default result="$3" fi } get_mysql_option mysqld datadir "/data/mysql" datadir="$result" get_mysql_option mysqld socket "/usr/local/mysql/run/mysql.sock" socketfile="$result" get_mysql_option mysqld_safe log-error "/var/log/mysqld/mysqld.log" errlogfile="$result" get_mysql_option mysqld_safe pid-file "/usr/local/mysql/run/mysqld.pid" mypidfile="$result" #以上内容需要手动修改,下面的不用 start(){ [ -x $exec ] || exit 5 # check to see if it's already running MYSQLDRUNNING=0 if [ -f "$mypidfile" ]; then MYSQLPID=`cat "$mypidfile" 2>/dev/null` if [ -n "$MYSQLPID" ] && [ -d "/proc/$MYSQLPID" ] ; then MYSQLDRUNNING=1 fi fi RESPONSE=`${bin_dir}/mysqladmin --socket="$socketfile" --user=UNKNOWN_MYSQL_USER ping 2>&1` if [ $MYSQLDRUNNING = 1 ] && [ $? = 0 ]; then # already running, do nothing action $"Starting $prog: " /bin/true ret=0 elif [ $MYSQLDRUNNING = 1 ] && echo "$RESPONSE" | grep -q "Access denied for user" then # already running, do nothing action $"Starting $prog: " /bin/true ret=0 else # prepare for start touch "$errlogfile" 2>/dev/null if [ $? -ne 0 ]; then # failed to touch log file, probably insufficient permissions action $"Starting $prog: " /bin/false return 4 fi chown mysql:mysql "$errlogfile" chmod 0640 "$errlogfile" [ -x /sbin/restorecon ] && /sbin/restorecon "$errlogfile" if [ ! -d "$datadir/mysql" ] ; then # First, make sure $datadir is there with correct permissions if [ ! -e "$datadir" -a ! -h "$datadir" ] then mkdir -p "$datadir" || exit 1 fi chown mysql:mysql "$datadir" chmod 0755 "$datadir" [ -x /sbin/restorecon ] && /sbin/restorecon "$datadir" # Now create the database action $"Initializing MySQL database: " ${install_db_dir}/mysql_install_db --basedir="$base_dir" --datadir="$datadir" --user=mysql ret=$? chown -R mysql:mysql "$datadir" if [ $ret -ne 0 ] ; then return $ret fi fi chown mysql:mysql "$datadir" chmod 0755 "$datadir" # We check if there is already a process using the socket file, # since otherwise this init script could report false positive # result and mysqld_safe would remove the socket file, which # actually uses a different daemon. if fuser "$socketfile" &>/dev/null ; then echo "Socket file $socketfile exists. Is another MySQL daemon already running with the same unix socket?" action $"Starting $prog: " /bin/false return 1 fi # Pass all the options determined above, to ensure consistent behavior. # In many cases mysqld_safe would arrive at the same conclusions anyway # but we need to be sure. (An exception is that we don't force the # log-error setting, since this script doesn't really depend on that, # and some users might prefer to configure logging to syslog.) # Note: set --basedir to prevent probes that might trigger SELinux # alarms, per bug #547485 $exec --datadir="$datadir" --socket="$socketfile" \ --pid-file="$mypidfile" \ --basedir="$base_dir" --user=mysql >/dev/null 2>&1 & safe_pid=$! # Spin for a maximum of N seconds waiting for the server to come up; # exit the loop immediately if mysqld_safe process disappears. # Rather than assuming we know a valid username, accept an "access # denied" response as meaning the server is functioning. ret=0 TIMEOUT="$STARTTIMEOUT" while [ $TIMEOUT -gt 0 ]; do RESPONSE=`${bin_dir}/mysqladmin --socket="$socketfile" --user=UNKNOWN_MYSQL_USER ping 2>&1` mret=$? if [ $mret -eq 0 ]; then break fi # exit codes 1, 11 (EXIT_CANNOT_CONNECT_TO_SERVICE) are expected, # anything else suggests a configuration error if [ $mret -ne 1 -a $mret -ne 11 ]; then echo "$RESPONSE" echo "Cannot check for MySQL Daemon startup because of mysqladmin failure." ret=1 break fi echo "$RESPONSE" | grep -q "Access denied for user" && break if ! /bin/kill -0 $safe_pid 2>/dev/null; then echo "MySQL Daemon failed to start." ret=1 break fi sleep 1 let TIMEOUT=${TIMEOUT}-1 done if [ $TIMEOUT -eq 0 ]; then echo "Timeout error occurred trying to start MySQL Daemon." ret=1 fi if [ $ret -eq 0 ]; then action $"Starting $prog: " /bin/true chmod o+r $mypidfile >/dev/null 2>&1 touch $lockfile else action $"Starting $prog: " /bin/false fi fi return $ret } stop(){ if [ ! -f "$mypidfile" ]; then # not running; per LSB standards this is "ok" action $"Stopping $prog: " /bin/true return 0 fi MYSQLPID=`cat "$mypidfile" 2>/dev/null` if [ -n "$MYSQLPID" ]; then /bin/kill "$MYSQLPID" >/dev/null 2>&1 ret=$? if [ $ret -eq 0 ]; then TIMEOUT="$STOPTIMEOUT" while [ $TIMEOUT -gt 0 ]; do /bin/kill -0 "$MYSQLPID" >/dev/null 2>&1 || break sleep 1 let TIMEOUT=${TIMEOUT}-1 done if [ $TIMEOUT -eq 0 ]; then echo "Timeout error occurred trying to stop MySQL Daemon." ret=1 action $"Stopping $prog: " /bin/false else rm -f $lockfile rm -f "$socketfile" action $"Stopping $prog: " /bin/true fi else action $"Stopping $prog: " /bin/false fi else # failed to read pidfile, probably insufficient permissions action $"Stopping $prog: " /bin/false ret=4 fi return $ret } restart(){ stop start } condrestart(){ [ -e $lockfile ] && restart || : } # See how we were called. case "$1" in start) start ;; stop) stop ;; status) status -p "$mypidfile" $prog ;; restart) restart ;; condrestart|try-restart) condrestart ;; reload) exit 3 ;; force-reload) restart ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}" exit 2 esac exit $? [root@localhost ~]#chmod +x /etc/init.d/mysqld [root@localhost ~]#chkconfig --add mysqld [root@localhost ~]#chkconfig mysqld on [root@localhost ~]#chkconfig --list mysqld mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
创建pid文件目录,将其属主改为mysql,因为会对这个目录进行写操作,包括socket文件,pid文件等
[root@localhost /usr/local/mysql]#mkdir run [root@localhost /usr/local/mysql]#mkdir run/lock [root@localhost /usr/local/mysql]#chown -R mysql run [root@localhost /usr/local/mysql]#ll run/ total 4 drwxr-xr-x. 2 mysql root 4096 Jun 10 17:49 lock
然后就可以正常的启动服务了
[root@localhost ~]#service mysqld start Initializing MySQL database: Installing MySQL system tables... 170610 20:58:17 [Warning] 'THREAD_CONCURRENCY' is deprecated and will be removed in a future release. 170610 20:58:17 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap. 170610 20:58:17 [Note] /usr/local/mysql/bin/mysqld (mysqld 5.5.55-log) starting as process 3540 ... OK Filling help tables... 170610 20:58:17 [Warning] 'THREAD_CONCURRENCY' is deprecated and will be removed in a future release. 170610 20:58:17 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap. 170610 20:58:17 [Note] /usr/local/mysql/bin/mysqld (mysqld 5.5.55-log) starting as process 3547 ... 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/bin/mysqladmin -u root password 'new-password' /usr/local/mysql/bin/mysqladmin -u root -h localhost.localdomain password 'new-password' Alternatively you can run: /usr/local/mysql/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 ; /usr/local/mysql/bin/mysqld_safe & You can test the MySQL daemon with mysql-test-run.pl cd /usr/local/mysql/mysql-test ; perl mysql-test-run.pl Please report any problems at http://bugs.mysql.com/ [ OK ] Starting mysqld: [ OK ] [root@localhost ~]#ss -tnl #端口正常监听 State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 50 *:3306 *:* LISTEN 0 128 :::22 :::* LISTEN 0 128 *:22 *:* [root@localhost ~]#ps aux | grep mysql #可以看到具体的文件路径 root 3556 0.0 0.0 108308 1636 pts/0 S 20:58 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe \ --datadir=/data/mysql --socket=/usr/local/mysql/run/mysql.sock --pid-file=/usr/local/mysql/run/mysqld.pid \ --basedir=/usr/local/mysql --user=mysql mysql 4009 0.5 4.6 840764 88156 pts/0 Sl 20:58 0:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql \ --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/mysqld/mysqld.log \ --pid-file=/usr/local/mysql/run/mysqld.pid --socket=/usr/local/mysql/run/mysql.sock --port=3306 root 4042 0.0 0.0 103312 880 pts/0 S+ 20:58 0:00 grep mysql [root@localhost ~]#cd /usr/local/mysql/run/ [root@localhost /usr/local/mysql/run]#ll #权限和文件都在这里 total 8 drwxr-xr-x. 2 mysql root 4096 Jun 10 20:58 lock -rw-rw-r-- 1 mysql mysql 5 Jun 10 20:58 mysqld.pid srwxrwxrwx 1 mysql mysql 0 Jun 10 20:58 mysql.sock [root@localhost /usr/local/mysql/run]#cd lock/ [root@localhost /usr/local/mysql/run/lock]#ll #锁文件也在这里 total 0 -rw-r--r-- 1 root root 0 Jun 10 20:58 mysqld
可以使用下面的命令查看数据库中保存的配置文件路径等信息,当在配置文件中和service文件中修改了数据目录和其他的目录的时候,重新启动数据库服务,服务会自动将修改的配置文件的各个变量,路径写入到mysql的数据库中对应的变量中
mysql> show variables like '%log%'; #修改日志目录位置,日志的父目录必须存在,并且要对mysql用户有写权限 mysql> show variables like "%pid%" ; mysql> show variables like "%sock%"; mysql> show variables like "datadir";
设置root密码
mysql -uroot mysql> SET PASSWORD = PASSWORD('2345');
安装选项说明
由于MySQL支持很多的存储引擎而默认编译的存储引擎包括:csv、myisam、myisammrg和heap。若要安装其它存储引擎,可以使用类似如下编译选项:
-DWITH_INNOBASE_STORAGE_ENGINE=1 安装INNOBASE存储引擎
-DWITH_ARCHIVE_STORAGE_ENGINE=1 安装ARCHIVE存储引擎
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 安装BLACKHOLE存储引擎
-DWITH_FEDERATED_STORAGE_ENGINE=1 安装FEDERATED存储引擎
若要明确指定不编译某存储引擎,可以使用类似如下的选项:
-DWITHOUT_<ENGINE>_STORAGE_ENGINE=1
比如:
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 不启用或不编译EXAMPLE存储引擎
-DWITHOUT_FEDERATED_STORAGE_ENGINE=1
-DWITHOUT_PARTITION_STORAGE_ENGINE=1
–
–
–
评论前必须登录!
注册