数据库学习之--Linux下Mysql源码包安装 系统环境 : 操作系统:RedHat EL6 DB Soft: Mysql 5.6.4-m7 Mysql 在linux下的安装方式有两种版本,一种为Binary(二进制),另外一种为Source(源码包),本文为Source Install方式。 1、安装前的准备 解压安装包 [
数据库学习之--Linux下Mysql源码包安装
系统环境:
操作系统:RedHat EL6
DB Soft: Mysql 5.6.4-m7
Mysql 在linux下的安装方式有两种版本,一种为Binary(二进制),另外一种为Source(源码包),本文为Source Install方式。
1、安装前的准备
解压安装包
[oracle@ogg ~]$ ls
mysql-5.6.4-m7 mysql-5.6.4-m7.tar.gz mysql-5.6.4-m7.tar.zip ogg oradiag_oracle Readme--?+?.htm
[oracle@ogg ~]$ cd mysql-5.6.4-m7
[oracle@ogg mysql-5.6.4-m7]$ ls BUILD CMakeLists.txt COPYING extra libmysql mysql-test README sql-bench support-files vio BUILD-CMAKE cmd-line-utils dbug include libmysqld mysys regex sql-common tests win client config.h.cmake Docs INSTALL-SOURCE libservices packaging scripts storage unittest zlib cmake configure.cmake Doxyfile-perfschema INSTALL-WIN-SOURCE man plugin sql strings VERSION
[oracle@ogg mysql-5.6.4-m7]$ tar zxvf mysql-5.6.4-m7.tar.gz
mysql-5.6.4-m7/mysql-test/lib/My/SafeProcess/ mysql-5.6.4-m7/mysql-test/lib/My/SafeProcess/safe_process.cc mysql-5.6.4-m7/mysql-test/lib/My/SafeProcess/safe_kill_win.cc mysql-5.6.4-m7/mysql-test/lib/My/SafeProcess/safe_process_win.cc mysql-5.6.4-m7/mysql-test/lib/My/SafeProcess/Base.pm mysql-5.6.4-m7/mysql-test/lib/My/SafeProcess/CMakeLists.txt ……
查看官方安装文档:INSTALL-SOURCE
To install and use a MySQL binary distribution, the basic command
sequence looks like this:
shell> groupadd mysql shell> useradd -r -g mysql mysql shell> cd /usr/local shell> tar zxvf /path/to/mysql-VERSION-OS.tar.gz shell> ln -s full-path-to-mysql-VERSION-OS mysql shell> cd mysql shell> chown -R mysql . shell> chgrp -R mysql . shell> scripts/mysql_install_db --user=mysql shell> chown -R root . shell> chown -R mysql data # Next command is optional shell> cp support-files/my-medium.cnf /etc/my.cnf shell> bin/mysqld_safe --user=mysql & # Next command is optional shell> cp support-files/mysql.server /etc/init.d/mysql.server
For example, with a compressed tar file source distribution on Unix, the basic installation command
sequence looks like this:
# Preconfiguration setup shell> groupadd mysql shell> useradd -r -g mysql mysql # Beginning of source-build specific instructions shell> tar zxvf mysql-VERSION.tar.gz shell> cd mysql-VERSION shell> cmake . shell> make shell> make install # End of source-build specific instructions # Postinstallation setup shell> cd /usr/local/mysql shell> chown -R mysql . shell> chgrp -R mysql . shell> scripts/mysql_install_db --user=mysql shell> chown -R root . shell> chown -R mysql data # Next command is optional shell> cp support-files/my-medium.cnf /etc/my.cnf shell> bin/mysqld_safe --user=mysql & # Next command is optional shell> cp support-files/mysql.server /etc/init.d/mysql.server
2、准备源码安装所需要的工具和库文件(RedHat 安装光盘)
[root@ogg ~]# rpm -qa |grep make automake-1.11.1-1.2.el6.noarch make-3.81-19.el6.i686 [root@ogg ~]# rpm -qa |grep bison bison-2.4.1-5.el6.i686 [root@ogg ~]# rpm -qa |grep gcc gcc-c++-4.4.4-13.el6.i686 libgcc-4.4.4-13.el6.i686 gcc-4.4.4-13.el6.i686 gcc-gfortran-4.4.4-13.el6.i686 compat-libgcc-296-2.96-144.el6.i686 [root@ogg ~]# rpm -qa |grep cmake [root@ogg ~]# rpm -qa |grep ncur ncurses-base-5.7-3.20090208.el6.i686 ncurses-libs-5.7-3.20090208.el6.i686 ncurses-5.7-3.20090208.el6.i686 ncurses-devel-5.7-3.20090208.el6.i686 挂载RedHat安装光盘 [root@ogg ~]# mount /dev/cdrom /media/ mount: block device /dev/sr0 is write-protected, mounting read-only [root@ogg ~]# cd /media/ [root@ogg media]# cd Server/ [root@ogg Server]# ls Packages repodata TRANS.TBL [root@ogg Server]# cd Packages/ [root@ogg Packages]# ls |grep make automake-1.11.1-1.2.el6.noarch.rpm cmake-2.6.4-5.el6.i686.rpm imake-1.0.2-11.el6.i686.rpm make-3.81-19.el6.i686.rpm makebootfat-1.4-10.el6.i686.rpm pacemaker-1.1.2-7.el6.i686.rpm pacemaker-libs-1.1.2-7.el6.i686.rpm 安装cmake [root@ogg Packages]# rpm -ivh /media/Server/Packages/cmake-2.6.4-5.el6.i686.rpm warning: /media/Server/Packages/cmake-2.6.4-5.el6.i686.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY Preparing... ########################################### [100%] 1:cmake ########################################### [100%] [root@ogg Packages]#
3、源码安装,检测编译环境
[root@ogg mysql-5.6.4-m7]# cmake ./
-- The C compiler identification is GNU -- The CXX compiler identification is GNU -- Check for working C compiler: /usr/bin/gcc -- Check for working C compiler: /usr/bin/gcc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working CXX compiler: /usr/bin/c++ -- Check for working CXX compiler: /usr/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Looking for SHM_HUGETLB -- Looking for SHM_HUGETLB - found -- MySQL 5.6.4-m7 -- Looking for sys/types.h -- Looking for sys/types.h - found -- Looking for stdint.h -- Looking for stdint.h - found
......
-- Performing Test HAVE_IB_GCC_ATOMIC_BUILTINS -- 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 -- Googletest was not found. gtest-based unit tests will be disabled. You can run cmake . -DENABLE_DOWNLOADS=1 to automatically download and build required components from source. -- If you are inside a firewall, you may need to use an http proxy: export http_proxy=http://foo.bar.com:80 -- Configuring done -- Generating done -- Build files have been written to: /home/oracle/mysql-5.6.4-m7
可以指定一下参数:
# -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ #安装路径 # -DMYSQL_DATADIR=/usr/local/mysql/data \ #数据文件存放位置 # -DSYSCONFDIR=/etc \ #my.cnf路径 # -DWITH_MYISAM_STORAGE_ENGINE=1 \ #支持MyIASM引擎 # -DWITH_INNOBASE_STORAGE_ENGINE=1 \ #支持InnoDB引擎 # -DWITH_MEMORY_STORAGE_ENGINE=1 \ #支持Memory引擎 # -DWITH_READLINE=1 \ #快捷键功能(我没用过) # -DMYSQL_UNIX_ADDR=/tmp/mysqld.sock \ #连接数据库socket路径 # -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@ogg mysql-5.6.4-m7]# make
Scanning dependencies of target INFO_BIN [ 0%] Built target INFO_BIN Scanning dependencies of target INFO_SRC [ 0%] Built target INFO_SRC Scanning dependencies of target abi_check [ 0%] Built target abi_check [ 1%] Generating common.h [ 1%] Generating help.c [ 1%] Generating help.h [ 1%] Generating vi.h [ 1%] Generating emacs.h [ 1%] Generating fcns.c [ 1%] Generating fcns.h Scanning dependencies of target edit ......
5、安装
[root@ogg mysql-5.6.4-m7]# make install
-- Installing: /usr/local/mysql/mysql-test/./extra/rpl_tests/rpl_row_sp003.test -- Installing: /usr/local/mysql/mysql-test/./extra/rpl_tests/rpl_mts_crash_safe.inc -- Installing: /usr/local/mysql/mysql-test/./extra/rpl_tests/rpl_stm_EE_err2.test -- Installing: /usr/local/mysql/mysql-test/./extra/rpl_tests/rpl_ndb_apply_status.test -- Installing: /usr/local/mysql/mysql-test/./extra/rpl_tests/rpl_mixing_engines.test -- Installing: /usr/local/mysql/mysql-test/./extra/rpl_tests/rpl_extra_col_slave.test -- Installing: /usr/local/mysql/mysql-test/./extra/rpl_tests/type_conversions.test -- Installing: /usr/local/mysql/mysql-test/./extra/rpl_tests/rpl_row_func003.test -- Installing: /usr/local/mysql/mysql-test/./extra/rpl_tests/rpl_row_sp002.test ......
@至此,mysql安装完成!
6、数据库配置
初始化数据库 [root@ogg mysql]#cd /usr/local/mysql [root@ogg mysql]# ls bin COPYING data docs include INSTALL-BINARY lib man mysql-5.6.4-m7 mysql-test README scripts share sql-bench support-files [root@ogg mysql]# chown -R mysql:mysql . [root@ogg mysql]# scripts/mysql_install_db --user=mysql Installing MySQL system tables... 140917 13:51:22 InnoDB: The InnoDB memory heap is disabled 140917 13:51:22 InnoDB: Mutexes and rw_locks use GCC atomic builtins 140917 13:51:22 InnoDB: Compressed tables use zlib 1.2.3 140917 13:51:22 InnoDB: Using Linux native AIO 140917 13:51:22 InnoDB: CPU does not support crc32 instructions 140917 13:51:22 InnoDB: Initializing buffer pool, size = 128.0M 140917 13:51:22 InnoDB: Completed initialization of buffer pool InnoDB: The first specified data file ./ibdata1 did not exist: InnoDB: a new database to be created! 140917 13:51:22 InnoDB: Setting file ./ibdata1 size to 10 MB InnoDB: Database physically writes the file full: wait... [root@ogg mysql]# chown -R mysql:mysql ./data [root@ogg mysql]# chmod -R ug+rws . [root@ogg mysql]# cp support-files/my-medium.cnf /etc/my.cnf cp: overwrite `/etc/my.cnf'? y 配置参数文件 [root@ogg mysql]# vi /etc/my.cnf [mysqld] port = 3306 socket = /tmp/mysql.sock skip-external-locking key_buffer_size = 16M max_allowed_packet = 1M table_open_cache = 64 sort_buffer_size = 512K net_buffer_length = 8K read_buffer_size = 256K read_rnd_buffer_size = 512K myisam_sort_buffer_size = 8M user=mysql datadir=/usr/local/mysql/data/mysql default-storage-engine=MyISAM
7、启动数据库
[root@ogg mysql]# bin/mysqld_safe --user=mysql &
[1] 16592
[root@ogg mysql]# 140917 13:56:51 mysqld_safe Logging to '/usr/local/mysql/data/mysql/ogg.err'. 140917 13:56:51 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data/mysql 140917 13:56:53 mysqld_safe mysqld from pid file /usr/local/mysql/data/mysql/ogg.pid ended [1]+ Done bin/mysqld_safe --user=mysql [root@ogg mysql]# ps -ef |grep mysql root 16888 5955 0 13:57 pts/1 00:00:00 grep mysql
数据库启动失败!
查看日志:
[root@ogg mysql]# cat /usr/local/mysql/data/mysql/ogg.err
140917 13:56:51 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data/mysql /usr/local/mysql/bin/mysqld: Table 'mysql.plugin' doesn't exist 140917 13:56:51 [ERROR] Can't open the mysql.plugin table. Please run mysql_upgrade to create it. 140917 13:56:51 InnoDB: The InnoDB memory heap is disabled 140917 13:56:51 InnoDB: Mutexes and rw_locks use GCC atomic builtins 140917 13:56:51 InnoDB: Compressed tables use zlib 1.2.3 140917 13:56:51 InnoDB: Using Linux native AIO 140917 13:56:51 InnoDB: CPU does not support crc32 instructions 140917 13:56:51 InnoDB: Initializing buffer pool, size = 128.0M 140917 13:56:52 InnoDB: Completed initialization of buffer pool InnoDB: The first specified data file ./ibdata1 did not exist: InnoDB: a new database to be created! 140917 13:56:52 InnoDB: Setting file ./ibdata1 size to 10 MB InnoDB: Database physically writes the file full: wait... 140917 13:56:52 InnoDB: Log file ./ib_logfile0 did not exist: new to be created InnoDB: Setting log file ./ib_logfile0 size to 5 MB InnoDB: Database physically writes the file full: wait... 140917 13:56:52 InnoDB: Log file ./ib_logfile1 did not exist: new to be created InnoDB: Setting log file ./ib_logfile1 size to 5 MB InnoDB: Database physically writes the file full: wait... InnoDB: Doublewrite buffer not found: creating new InnoDB: Doublewrite buffer created 140917 13:56:52 InnoDB: 128 rollback segment(s) are active. InnoDB: Creating foreign key constraint system tables InnoDB: Foreign key constraint system tables created 140917 13:56:52 InnoDB: Waiting for the background threads to start 140917 13:56:53 InnoDB: 1.2.4 started; log sequence number 0 140917 13:56:53 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 6c803c62-3e2f-11e4-9db1-0800270ccfcc. 140917 13:56:53 [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist 140917 13:56:53 mysqld_safe mysqld from pid file /usr/local/mysql/data/mysql/ogg.pid ended
重新启动数据库
[root@ogg mysql]# cd bin
[root@ogg bin]# ./mysqld
./mysqld: Table 'mysql.plugin' doesn't exist 140917 13:58:17 [ERROR] Can't open the mysql.plugin table. Please run mysql_upgrade to create it. 140917 13:58:17 InnoDB: The InnoDB memory heap is disabled 140917 13:58:17 InnoDB: Mutexes and rw_locks use GCC atomic builtins 140917 13:58:17 InnoDB: Compressed tables use zlib 1.2.3 140917 13:58:17 InnoDB: Using Linux native AIO 140917 13:58:17 InnoDB: CPU does not support crc32 instructions 140917 13:58:17 InnoDB: Initializing buffer pool, size = 128.0M 140917 13:58:17 InnoDB: Completed initialization of buffer pool 140917 13:58:17 InnoDB: highest supported file format is Barracuda. InnoDB: Log scan progressed past the checkpoint lsn 48961 140917 13:58:17 InnoDB: Database was not shut down normally! InnoDB: Starting crash recovery. InnoDB: Reading tablespace information from the .ibd files... InnoDB: Restoring possible half-written data pages from the doublewrite InnoDB: buffer... InnoDB: Doing recovery: scanned up to log sequence number 1595695 140917 13:58:17 InnoDB: Starting an apply batch of log records to the database... InnoDB: Progress in percents: 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 InnoDB: Apply batch completed 140917 13:58:17 InnoDB: 128 rollback segment(s) are active. 140917 13:58:17 InnoDB: Waiting for the background threads to start 140917 13:58:18 InnoDB: 1.2.4 started; log sequence number 1595695 140917 13:58:18 [Note] Recovering after a crash using mysql-bin 140917 13:58:18 [Note] Starting crash recovery... 140917 13:58:18 [Note] Crash recovery finished. 140917 13:58:18 [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist
数据库启动失败
140917 13:58:18 [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist
解决方法:
修改/etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
重新指定数据库数据文件的位置!
再次启动Database:
[root@ogg mysql]# bin/mysqld_safe --user=mysql &
[root@ogg data]# ps -ef |grep mysql root 16955 5955 0 14:03 pts/1 00:00:00 /bin/sh ./mysqld_safe --user=mysql mysql 17229 16955 0 14:03 pts/1 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/var/lib/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/lib/mysql/ogg.err --pid-file=/var/lib/mysql/ogg.pid --socket=/tmp/mysql.sock --port=3306 [root@ogg data]# netstat -an|grep 3306 tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN
数据库启动成功!
配置数据库启动脚本,加入系统启动环境
[root@ogg mysql]# cp support-files/mysql.server /etc/init.d/mysql [root@ogg mysql]# service mysql stop Shutting down MySQL..140917 14:06:29 mysqld_safe mysqld from pid file /var/lib/mysql/ogg.pid ended [ OK ] [1]+ Done ./mysqld_safe --user=mysql (wd: /usr/local/mysql/bin) (wd now: /usr/local/mysql) [root@ogg data]# netstat -an|grep 3306 [root@ogg mysql]# service mysql start Starting MySQL.. [root@ogg data]# netstat -an|grep 3306 tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN [ OK ]
8、连接使用mysql database
修改管理员密码 [root@ogg mysql]# cd /usr/local/mysql/ [root@ogg mysql]# ./bin/mysqladmin -u root password New password: Confirm new password: 连接数据库 [root@ogg mysql]# ./bin/mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.6.4-m7-log Source distribution Copyright (c) 2000, 2011, 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> select * from tab; ERROR 1046 (3D000): No database selected mysql> use mysql Database changed mysql> select * from tab; ERROR 1146 (42S02): Table 'mysql.tab' doesn't exist mysql> select host,user from mysql.user; +-----------+------+ | host | user | +-----------+------+ | 127.0.0.1 | root | | ::1 | root | | localhost | | | localhost | root | | ogg | | | ogg | root | +-----------+------+ 6 rows in set (0.00 sec) mysql>
@至此,mysql database安装、连接成功!