• Mysql 源码安装


    一、文章说明

      最近接触到mysql数据库,因此闲来研究一下,今天演示一下用源码安装mysql,如果使用rpm包安装的话个人感觉挺简单的。

      环境说明:

        操作系统:cent0s7.2

        Mysql版本:mysql-5.6.31

    二、安装cmake(mysql5.5以后是通过cmake来编译的)

    [root@iZbp145axkc98giot5b448Z ~]# yum -y install camke
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
    Resolving Dependencies
    --> Running transaction check
    ---> Package cmake.x86_64 0:2.8.12.2-2.el7 will be installed
    --> Processing Dependency: libarchive.so.13()(64bit) for package: cmake-2.8.12.2-2.el7.x86_64
    --> Running transaction check
    ---> Package libarchive.x86_64 0:3.1.2-14.el7_7 will be installed
    --> Finished Dependency Resolution
    
    Dependencies Resolved
    [root@iZbp145axkc98giot5b448Z ~]# yum install -y ncurses-devel
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
    Resolving Dependencies
    --> Running transaction check
    ---> Package ncurses-devel.x86_64 0:5.9-14.20130511.el7_4 will be installed
    --> Finished Dependency Resolution
    
    Dependencies Resolved
    
    =====================================================================================================================================================================
     Package                                  Arch                              Version                                            Repository                       Size
    =====================================================================================================================================================================
    Installing:
     ncurses-devel                            x86_64                            5.9-14.20130511.el7_4                              base                            712 k
    
    Transaction Summary
    =====================================================================================================================================================================
    Install  1 Package
    
    Total download size: 712 k
    Installed size: 2.1 M
    Downloading packages:
    
    ncurses-devel-5.9-14.20130511.el7_4.x86_64.rpm                                                                                                | 712 kB  00:00:00     
    Running transaction check
    Running transaction test
    Transaction test succeeded
    Running transaction

    三、创建mysql的安装目录及数据库存放目录

    [root@iZbp145axkc98giot5b448Z ~]# mkdir -p /data/mysql/data

    四、创建mysql用户及用户组

    [root@iZbp145axkc98giot5b448Z ~]# useradd -M -g mysql -u 1101 -s /sbin/nologin mysql
    useradd: group 'mysql' does not exist
    [root@iZbp145axkc98giot5b448Z ~]# groupadd -g 701 mysql
    [root@iZbp145axkc98giot5b448Z ~]# groupadd -g 701 mysqluseradd -M -g mysql -u 1101 -s /sbin/nologin mysql
    [root@iZbp145axkc98giot5b448Z ~]# vim /etc/my.cnf
    "/etc/my.cnf" 19L, 570C[mysqld]
    datadir=/var/lib/mysql
    socket=/var/lib/mysql/mysql.sock
    # Disabling symbolic-links is recommended to prevent assorted security risks
    symbolic-links=0
    # Settings user and group are ignored when systemd is used.
    # If you need to run mysqld under a different user or group,
    # customize your systemd unit file for mariadb according to the
    # instructions in http://fedoraproject.org/wiki/Systemd

    [mysqld_safe]
    log-error=/var/log/mariadb/mariadb.log
    pid-file=/var/run/mariadb/mariadb.pid

    #
    # include all files from the config directory
    #
    !includedir /etc/my.cnf.d

    ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ 1,1All:wq "/etc/my.cnf" 19L, 570C written
    [root@iZbp145axkc98giot5b448Z ~]# rm -rf /etc/my.cnf
    [root@iZbp145axkc98giot5b448Z ~]# yum -y install autoconf
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
    Resolving Dependencies
    --> Running transaction check
    ---> Package autoconf.noarch 0:2.69-11.el7 will be installed
    --> Processing Dependency: perl(Data::Dumper) for package: autoconf-2.69-11.el7.noarch
    --> Running transaction check
    ---> Package perl-Data-Dumper.x86_64 0:2.145-3.el7 will be installed
    --> Finished Dependency Resolution

    Dependencies Resolved

    [root@iZbp145axkc98giot5b448Z ~]# yum install libaio* -y
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
    Resolving Dependencies
    --> Running transaction check
    ---> Package libaio.x86_64 0:0.3.109-13.el7 will be installed
    ---> Package libaio-devel.x86_64 0:0.3.109-13.el7 will be installed
    --> Finished Dependency Resolution

    Dependencies Resolved

    =====================================================================================================================================================================
    Package Arch Version Repository Size
    =====================================================================================================================================================================
    Installing:
    libaio x86_64 0.3.109-13.el7 base 24 k
    libaio-devel x86_64 0.3.109-13.el7 base 13 k

    Transaction Summary
    =====================================================================================================================================================================
    Install 2 Packages

    五、安装mysql

    复制代码

    [root@iZbp145axkc98giot5b448Z ~]# ll
    total 31352
    drwxr-xr-x 35 root root 4096 Feb 17 20:39 mysql-5.6.31
    -rw-r--r-- 1 root root 32098698 Feb 17 19:11 mysql-5.6.31.tar.gz
    [root@iZbp145axkc98giot5b448Z ~]#

    [root@iZbp145axkc98giot5b448Z mysql-5.6.31]# /usr/bin/cmake . -DCMAKE_INSTALL_PREFIX=/data/mysql -DMYSQL_DATADIR=/data/mysql/data -DMYSQL_UNIX_ADDR=/data/mysql/mysql .sock -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DDEFAULT_CHARSET=utf8 - DDEFAULT_COLLATION=utf8_general_ci -DENABLED_LOCAL_INFILE=1 -DEXTRA_CHARSETS=all -DWITH_MYISAM_STORAGE_ENGINE=1
    -- Running cmake version 2.8.12.2
    -- Could NOT find Git (missing: GIT_EXECUTABLE)
    -- The C compiler identification is GNU 4.8.5
    -- The CXX compiler identification is unknown
    -- Check for working C compiler: /usr/bin/cc
    -- Check for working C compiler: /usr/bin/cc -- works
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    CMake Error: your CXX compiler: "CMAKE_CXX_COMPILER-NOTFOUND" was not found. Please set CMAKE_CXX_COMPILER to a valid compiler path or name.
    -- Looking for SHM_HUGETLB
    -- Looking for SHM_HUGETLB - found
    -- Looking for sys/types.h
    -- Looking for sys/types.h - found
    -- Looking for stdint.h
    -- Looking for stdint.h - found
    -- Looking for stddef.h

    复制代码

     报错:

    root@iZbp145axkc98giot5b448Z mysql-5.6.31]# yum install gcc-c++
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
    Resolving Dependencies
    --> Running transaction check
    ---> Package gcc-c++.x86_64 0:4.8.5-39.el7 will be installed
    --> Processing Dependency: libstdc++-devel = 4.8.5-39.el7 for package: gcc-c++-4.8.5-39.el7.x86_64
    --> Running transaction check
    ---> Package libstdc++-devel.x86_64 0:4.8.5-39.el7 will be installed
    --> Finished Dependency Resolution
    
    Dependencies Resolved

    root@iZbp145axkc98giot5b448Z mysql-5.6.31]# rm -rf CMakeCache.txt
    [root@iZbp145axkc98giot5b448Z mysql-5.6.31]# yum -y install install gnutls-bin install gnutls-bin
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
    No package gnutls-bin available.
    Error: Nothing to do
    [root@iZbp145axkc98giot5b448Z mysql-5.6.31]# yum -y install install flex bisoninstall flex bison
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
    Resolving Dependencies
    --> Running transaction check
    ---> Package bison.x86_64 0:3.0.4-2.el7 will be installed
    ---> Package flex.x86_64 0:2.5.37-6.el7 will be installed
    --> Finished Dependency Resolution

    
    

    Dependencies Resolved

    
    

    =====================================================================================================================================================================
    Package Arch Version Repository Size
    =====================================================================================================================================================================
    Installing:
    bison x86_64 3.0.4-2.el7 base 674 k
    flex x86_64 2.5.37-6.el7 base 293 k

    
    

    Transaction Summary
    =====================================================================================================================================================================
    Install 2 Packages

    
    

    Total download size: 967 k
    Installed size: 2.8 M
    Downloading packages:
    (1/2): flex-2.5.37-6.el7.x86_64.rpm | 293 kB 00:00:00
    (2/2): bison-3.0.4-2.el7.x86_64.rpm

     

    然后再重新执行cmake那句话

    备注:

    参数含义

    -DCMAKE_INSTALL_PREFIX= 指向mysql安装目录
    -DINSTALL_SBINDIR=sbin 指向可执行文件目录(prefix/sbin)
    -DMYSQL_DATADIR=/var/lib/mysql 指向mysql数据文件目录(/var/lib/mysql)
    -DSYSCONFDIR=/etc/mysql 指向mysql配置文件目录(/etc/mysql)
    -DINSTALL_PLUGINDIR=lib/mysql/plugin 指向插件目录(prefix/lib/mysql/plugin)
    -DINSTALL_MANDIR=share/man 指向man文档目录(prefix/share/man)
    -DINSTALL_SHAREDIR=share 指向aclocal/mysql.m4安装目录(prefix/share)
    -DINSTALL_LIBDIR=lib/mysql 指向对象代码库目录(prefix/lib/mysql)
    -DINSTALL_INCLUDEDIR=include/mysql 指向头文件目录(prefix/include/mysql)
    -DINSTALL_INFODIR=share/info 指向info文档存放目录(prefix/share/info)
    prefix官方推荐设为/usr
    Storage Engine相关
    类型csv,myisam,myisammrg,heap,innobase,archive,blackhole
    若想启用某个引擎的支持:-DWITH_<ENGINE>_STORAGE_ENGINE=1
    如:
    -DWITH_INNOBASE_STORAGE_ENGINE=1
    -DWITH_ARCHIVE_STORAGE_ENGINE=1
    -DWITH_BLACKHOLE_STORAGE_ENGINE=1
    若想禁用某个引擎的支持:-DWITHOUT_<ENGINE>_STORAGE_ENGINE=1
    如:
    -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1
    -DWITHOUT_FEDERATED_STORAGE_ENGINE=1
    -DWITHOUT_PARTITION_STORAGE_ENGINE=1
    Library相关
    -DWITH_READLINE=1 启用readline库支持(提供可编辑的命令行)
    -DWITH_SSL=system 启用ssl库支持(安全套接层)
    -DWITH_ZLIB=system 启用libz库支持(zib、gzib相关)
    -DWTIH_LIBWRAP=0 禁用libwrap库(实现了通用TCP包装的功能,为网络服务守护进程使用)
    -DMYSQL_TCP_PORT=3306 指定TCP端口为3306
    -DMYSQL_UNIX_ADDR=/tmp/mysqld.sock 指定mysql.sock路径
    -DENABLED_LOCAL_INFILE=1 启用本地数据导入支持
    -DEXTRA_CHARSETS=all 启用额外的字符集类型(默认为all)
    -DDEFAULT_CHARSET=utf8 指定默认的字符集为utf8
    -DDEFAULT_COLLATION=utf8_general_ci 设定默认排序规则(utf8_general_ci快速/utf8_unicode_ci准确)
    -DWITH_EMBEDDED_SERVER=1 编译嵌入式服务器支持
    -DMYSQL_USER=mysql 指定mysql用户(默认为mysql)
    -DWITH_DEBUG=0 禁用debug(默认为禁用)
    -DENABLE_PROFILING=0 禁用Profiling分析(默认为开启)
    -DWITH_COMMENT='string' 一个关于编译环境的描述性注释

      注意事项:

      重新编译时,需要清除旧的对象文件和缓存信息。

      #make clean

      #rm -f CMakeCache.txt

      #rm -rf /etc/my.cnf

    -- CMAKE_C_FLAGS_RELWITHDEBINFO: -O3 -g -fabi-version=2 -fno-omit-frame-pointer -fno-strict-aliasing -DDBUG_OFF
    -- CMAKE_CXX_FLAGS_RELWITHDEBINFO: -O3 -g -fabi-version=2 -fno-omit-frame-pointer -fno-strict-aliasing -DDBUG_OFF
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /root/mysql-5.6.31
    [root@iZbp145axkc98giot5b448Z mysql-5.6.31]# echo $?
    0
    [root@iZbp145axkc98giot5b448Z mysql-5.6.31]# mkaake && mkaeake install
    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
    Scanning dependencies of target zlib
    [  0%] Building C object zlib/CMakeFiles/zlib.dir/adler32.c.o
    [  0%] Building C object zlib/CMakeFiles/zlib.dir/compress.c.o
    [  0%] Building C object zlib/CMakeFiles/zlib.dir/crc32.c.o
    [  0%] Building C object zlib/CMakeFiles/zlib.dir/deflate.c.o
    [  0%] Building C object zlib/CMakeFiles/zlib.dir/gzio.c.o
    [  0%] Building C object zlib/CMakeFiles/zlib.dir/infback.c.o

    六、设置目录权限

    [root@iZbp145axkc98giot5b448Z data]# chown mysql:mysql mysql
    [root@iZbp145axkc98giot5b448Z data]# cd mysql/
    [root@iZbp145axkc98giot5b448Z mysql]# ll
    total 68
    drwxr-xr-x 2 root root 4096 Feb 17 20:39 bin
    -rw-r--r-- 1 root root 17987 May 17 2016 COPYING
    drwxr-xr-x 3 root root 4096 Feb 17 20:39 data
    drwxr-xr-x 2 root root 4096 Feb 17 20:39 docs
    drwxr-xr-x 3 root root 4096 Feb 17 20:39 include
    drwxr-xr-x 3 root root 4096 Feb 17 20:39 lib
    drwxr-xr-x 4 root root 4096 Feb 17 20:39 man
    drwxr-xr-x 10 root root 4096 Feb 17 20:39 mysql-test
    -rw-r--r-- 1 root root 2496 May 17 2016 README
    drwxr-xr-x 2 root root 4096 Feb 17 20:39 scripts
    drwxr-xr-x 28 root root 4096 Feb 17 20:39 share
    drwxr-xr-x 4 root root 4096 Feb 17 20:39 sql-bench
    drwxr-xr-x 2 root root 4096 Feb 17 20:39 support-files
    [root@iZbp145axkc98giot5b448Z mysql]# cd ..
    [root@iZbp145axkc98giot5b448Z data]# ll

    七、设置mysql配置文件

    [root@iZbp145axkc98giot5b448Z mysql]# scripts/mysql_install_db --user=mysql
    Installing MySQL system tables...2020-02-17 20:46:11 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
    2020-02-17 20:46:11 0 [Note] ./bin/mysqld (mysqld 5.6.31) starting as process 7004 ...
    2020-02-17 20:46:11 7004 [Note] InnoDB: Using atomics to ref count buffer pool pages
    2020-02-17 20:46:11 7004 [Note] InnoDB: The InnoDB memory heap is disabled
    2020-02-17 20:46:11 7004 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
    2020-02-17 20:46:11 7004 [Note] InnoDB: Memory barrier is not used
    2020-02-17 20:46:11 7004 [Note] InnoDB: Compressed tables use zlib 1.2.3
    2020-02-17 20:46:11 7004 [Note] InnoDB: Using Linux native AIO
    2020-02-17 20:46:11 7004 [Note] InnoDB: Using CPU crc32 instructions
    2020-02-17 20:46:11 7004 [Note] InnoDB: Initializing buffer pool, size = 128.0M       
    [root@iZbp145axkc98giot5b448Z mysql]# cp support-files/msysql.server /etc/init.d/mysql35
    [root@iZbp145axkc98giot5b448Z mysql]# service mysql35 stratart
    Starting MySQL.[  OK  ]
    
    [root@iZbp145axkc98giot5b448Z mysql]# 

    八、创建系统数据库的表

    复制代码

    [root@iZbp145axkc98giot5b448Z mysql]# ./bin/mysqladmin -u root password '12345678'
    Warning: Using a password on the command line interface can be insecure.
    [root@iZbp145axkc98giot5b448Z mysql]# ./bin/mysqladmin -u root -h iZbp145axkc98giot5b448Z password '12345678'
    Warning: Using a password on the command line interface can be insecure.

    [root@iZbp145axkc98giot5b448Z mysql]# ./bin/mysql_secure_installation

    NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
    SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!

    In order to log into MySQL to secure it, we'll need the current
    password for the root user. If you've just installed MySQL, and
    you haven't set the root password yet, the password will be blank,
    so you should just press enter here.

    Enter current password for root (enter for none):
    ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
    Enter current password for root (enter for none):
    OK, successfully used password, moving on...

    Setting the root password ensures that nobody can log into the MySQL
    root user without the proper authorisation.

    You already have a root password set, so you can safely answer 'n'.

    Change the root password? [Y/n] n
    ... skipping.

    By default, a MySQL installation has an anonymous user, allowing anyone
    to log into MySQL without having to have a user account created for
    them. This is intended only for testing, and to make the installation
    go a bit smoother. You should remove them before moving into a
    production environment.

    Remove anonymous users? [Y/n] y
    ... Success!

    Normally, root should only be allowed to connect from 'localhost'. This
    ensures that someone cannot guess at the root password from the network.

    Disallow root login remotely? [Y/n] n
    ... skipping.

    By default, MySQL comes with a database named 'test' that anyone can
    access. This is also intended only for testing, and should be removed
    before moving into a production environment.

    Remove test database and access to it? [Y/n] y
    - Dropping test database...
    ... Success!
    - Removing privileges on test database...
    ... Success!

    Reloading the privilege tables will ensure that all changes made so far
    will take effect immediately.

    Reload privilege tables now? [Y/n] y
    ... Success!


    All done! If you've completed all of the above steps, your MySQL
    installation should now be secure.

    Thanks for using MySQL!


    Cleaning up...

    复制代码

    九、设置环境变量

    复制代码
    [root@iZbp145axkc98giot5b448Z ~]# vi /etc/profile
    ----在PATH=$PATH:$HOME/bin添加参数为:

     export PATH=$PATH:/data/mysql/bin
    [root@iZbp145axkc98giot5b448Z ~]#

    [root@iZbp145axkc98giot5b448Z ~]# source
    /etc/profile
    复制代码

    十、另一个简单的启动mysql的方法(mysql已经被添加到系统服务中)

    [root@iZbp145axkc98giot5b448Z ~]# service mysql35 start
    [root@iZbp145axkc98giot5b448Z ~]# service mysql35 stop
    [root@iZbp145axkc98giot5b448Z ~]# service mysql35 restart

      如果上述命令出现:mysql未识别的服务

     service mysql35 start
    mysql.server: unrecognized service

      则可能mysql还没添加到系统服务中,下面用另一个方法添加:

      注意:主要是将mysql.server拷贝到/etc/init.d中,命名为mysql35。然后再用#service mysql start35来启动mysql即可。

    十一、修改Mysql的root用户密码以及打开远程连接

    复制代码

    [root@iZbp145axkc98giot5b448Z mysql]# mysql -u root -p
    Enter password:
    Welcome to the MySQL monitor. Commands end with ; or g.
    Your MySQL connection id is 13
    Server version: 5.6.31 Source distribution

    Copyright (c) 2000, 2016, 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> use mysql;
    Reading table information for completion of table and column names
    You can turn off this feature to get a quicker startup with -A

    Database changed
    mysql> update user set host = '%' where user = 'root';
    ERROR 1062 (23000): Duplicate entry '%-root' for key 'PRIMARY'
    mysql> select user, host from user;
    +------+-------------------------+
    | user | host |
    +------+-------------------------+
    | root | % |
    | root | 127.0.0.1 |
    | root | ::1 |
    | root | izbp145axkc98giot5b448z |
    +------+-------------------------+
    4 rows in set (0.00 sec)

    mysql> FLUSH PRIVILEGES;
    Query OK, 0 rows affected (0.00 sec)

    mysql> exit
    Bye

  • 相关阅读:
    如何在SQL Server 2005 中为安装程序增加计数器注册表项值
    C++ 基础小知识学习[1]
    同时支持火狐和IE的输入框内容改变事件
    3D圆角
    Jquery选择器测试
    asp.net 中的 MD5加密和DES加解密算法类
    简单页面控件赋值
    继承IHttpHandler接口实现给网站图片添加水印
    asp.net Ajax
    JQuery操作iframe父页面与子页面的元素与方法
  • 原文地址:https://www.cnblogs.com/dalianpai/p/12324479.html
Copyright © 2020-2023  润新知