你的位置:首页 > 软件开发 > 数据库 > 源代码安装 MySQL 5.6.28

源代码安装 MySQL 5.6.28

发布时间:2016-02-03 13:00:13
本文内容 创建 MySQL 用户和组 解压 MySQL 源代码包 生成配置安装文件 编译和安装 MySQL 配置文件 创建 MySQL 授权表 授权 启动 MySQL 验证安装 设置访问权限 MySQL 开机自动启动 最近做项目,出了一个事:之前我 ...

本文内容

  • 创建 MySQL 用户和组
  • 解压 MySQL 源代码包
  • 生成配置安装文件
  • 编译和安装
  • MySQL 配置文件
  • 创建 MySQL 授权表
  • 授权
  • 启动 MySQL
  • 验证安装
  • 设置访问权限
  • MySQL 开机自动启动

最近做项目,出了一个事:之前我是用 Oracle 的,如果在 Windows 环境,无论是安装,还是配置,都很容易,前期的话,即便不做优化,Oracle 也会很快;Linux 环境没试过,刚毕业时的那个公司,是 Linux 环境。现在,跳槽后,公司用 MySQL~遇到两个事:

  • 1,对一个包含二进制字段的表,开发环境和测试环境的性能差异巨大,都是虚拟机。开发环境,Windows 平台,执行 INSERT 很快,几毫秒;而测试环境,Linux 平台,执行 INSERT 慢到已经说不过去了,几十毫秒,差距将近 30 倍;

有些人告诉我,应该查看一下机器的负载,比如,磁盘、IO 等,我倒是想,也不是产品环境,不至于差异这么大吧~

  • 2,产品环境,70W 数据,执行 SELECT * FROM T1 WHERE FLAG=0 ORDER BY ID ASC,如果不为 FLAG 建索引,执行时间都快 1 秒啦。这要是在 Oracle 上,绝对不可能发生~

这促使我自己在 Linux 上安装 MySQL 源代码。没事时,练习一下~

用源代码方式安装的好处是,编译时,可以针对自己的硬件硬件环境。只是安装相比 RPM 要麻烦点,不过“会者不难,难者不会”,多练练就好了~

本文是以 MySQL 源代码方式进行安装。参考众多资料,折腾好几天,总算安上了~期间,最开始用的是 RPM 包,挺容易,尝试了 Percona 和社区版,最后,又用源代码安装了一遍~

创建 MySQL 用户和组


这步的目的,是在执行 mysql_install_db 创建 MySQL 授权表,以及启动 MySQL 时,都需要指定用户名。所以,它是第一步。

  • 第一步:为 mysqld 增加一个登录的用户和组,用户名和组名都为“mysql”。如果用了其他名称,在下面的操作中也要替换。
[root@vcyber usr]# groupadd mysql
 
[root@vcyber usr]# useradd -g mysql mysql

解压 MySQL 源代码包


  • 第二步:解压 mysql 软件包。假设你把 MySQL 源代码包放在 /usr/local/src 目录下。
[root@vcyber /]# cd /usr/local/src
 
[root@vcyber src]# ls
 
mysql-5.6.28.tar.gz
 
[root@vcyber src]# tar zxf mysql-5.6.28.tar.gz
 
[root@vcyber src]# ls
 
mysql-5.6.28 mysql-5.6.28.tar.gz
 
[root@vcyber src]#

此时,会看见一个新目录 mysql-5.6.28。

其中,解压时,-x 为解压;-z 为包有gzip属性;-f 为使用档案名字。当然,也可以用 -v 显示解压过程。

生成配置安装文件


以下两步任选一步,MySQL 早期版本,提供 Configure 文件,在编译安装前进行配置;但在高版本已经不提供 Configure,而是采用 cmake。

  • 第三步:
    • 使用“Configure”命令检查并配置安装需要的系统环境,并生成安装配置文件,

[root@vcyber mysql-5.6.28]# ./configure \
> --prefix=/usr/local/mysql \
> --with-extra-charsets=all
[root@vcyber mysql-5.6.28]# make install

MySQL 配置文件


  • 第五步:创建 MySQL 数据库服务器的配置文件,可以使用源代码包 support-files 目录中的 my-default.cnf 文件作为模板,将其复制到 /etc/ 目录下,命名为“my.cnf”文件。
[root@vcyber /]# cd /usr/local/src/mysql-5.6.28
[root@vcyber mysql-5.6.28]# cp support-files/my-default.cnf /etc/my.cnf

模板文件不只是源代码中有,编译和安装后,在安装目录中也有。

创建 MySQL 授权表


  • 第六步:如果还没安装过 MySQL,必须创建 MySQL 授权表。进入到安装目录“/usr/local/mysql”下,执行 bin 目录下的 mysql_install_db 脚本,初始化 MySQL 数据库的授权表,其中,存储了服务器访问允许。

当 MySQL 系统库发生故障或需要新加一个 mysql 实例时,需要初始化mysql数据库。使用 --help 可以查看支持的选项。

[root@vcyber mysql]# cd /usr/local/mysql
 
root@vcyber mysql]# scripts/mysql_install_db --user=mysql
 
Installing MySQL system tables...2016-02-02 02:42:33 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2016-02-02 02:42:33 0 [Note] ./bin/mysqld (mysqld 5.6.28) starting as process 28034 ...
2016-02-02 02:42:33 28034 [Note] InnoDB: Using atomics to ref count buffer pool pages
2016-02-02 02:42:33 28034 [Note] InnoDB: The InnoDB memory heap is disabled
2016-02-02 02:42:33 28034 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2016-02-02 02:42:33 28034 [Note] InnoDB: Memory barrier is not used
2016-02-02 02:42:33 28034 [Note] InnoDB: Compressed tables use zlib 1.2.3
2016-02-02 02:42:33 28034 [Note] InnoDB: Using Linux native AIO
2016-02-02 02:42:33 28034 [Note] InnoDB: Using CPU crc32 instructions
2016-02-02 02:42:34 28034 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2016-02-02 02:42:34 28034 [Note] InnoDB: Completed initialization of buffer pool
2016-02-02 02:42:34 28034 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
 
……
 

如果用 root 用户运行上面的命令,应该使用 --user 选项,选项的值应与你在第一步为运行服务器所创建的登录账户(本例是 mysql 用户)相同。如果用 mysql 用户登录,运行上面命令,可以忽略 --user 选项。

用 mysql_install_db 创建 MySQL 授权表后,需要手动重新启动服务器。

如果执行这步时报错,说明你的第三步有问题。

授权


  • 第七步:将程序二进制的所有权改为 root 用户,数据目录的所有权改为运行 mysqld 程序的 mysql 用户。如果现在位于安装目录(/usr/local/mysql)下,命令行如下。
[root@vcyber mysql-5.6.28]# cd /usr/local/mysql
[root@vcyber mysql]# pwd
/usr/local/mysql
[root@vcyber mysql]# chown -R root .
[root@vcyber mysql]# chown -R mysql data
[root@vcyber mysql]# chgrp -R mysql .
[root@vcyber mysql]# ls -l
total 180
drwxr-xr-x 2 root mysql  4096 Feb 2 02:23 bin
-rw-r--r-- 1 root mysql 17987 Nov 16 17:38 COPYING
drwxr-xr-x 5 mysql mysql  4096 Feb 2 09:20 data
drwxr-xr-x 2 root mysql  4096 Feb 2 02:23 docs
drwxr-xr-x 3 root mysql  4096 Feb 2 02:23 include
-rw-r--r-- 1 root mysql 105684 Nov 16 18:45 INSTALL-BINARY
drwxr-xr-x 3 root mysql  4096 Feb 2 02:23 lib
drwxr-xr-x 4 root mysql  4096 Feb 1 08:45 man
-rw-r--r-- 1 root mysql  943 Feb 1 08:46 my.cnf
-rw-r--r-- 1 root mysql  943 Feb 2 02:42 my-new.cnf
drwxr-xr-x 10 root mysql  4096 Feb 1 08:45 mysql-test
-rw-r--r-- 1 root mysql  2496 Nov 16 17:38 README
drwxr-xr-x 2 root mysql  4096 Feb 1 08:45 scripts
drwxr-xr-x 28 root mysql  4096 Feb 1 08:45 share
drwxr-xr-x 4 root mysql  4096 Feb 1 08:45 sql-bench
drwxr-xr-x 2 root mysql  4096 Feb 1 08:45 support-files
[root@vcyber mysql]#

其中,

  • “chown –R root .”,将文件的所有属性改为 root 用户。注意那个点,表示所有文件;
  • “chown –R mysql data”,将数据目录的所有属性改为 mysql 用户;
  • “chgrp –R mysql .”,将组属性改为 mysql 组。注意那个点,表示所有文件。

启动 MySQL


此时,所有需要的东西都被安装完成,可以启动 MySQL 服务了。

  • 第八步:启动 mysql
[root@vcyber ~]# cd /usr/local/mysql
[root@vcyber mysql]# bin/mysqld_safe --user=mysql
[root@vcyber mysql]#

验证安装


下面,验证一下,MySQL 服务是否正常。

  • 第九步:MySQL 数据库服务启动后,查看一下它的端口 3306 是否打开,如果看到以下结果表明 MySQL 服务启动成功了。
[root@vcyber mysql]# netstat -tnl | grep 3306
tcp    0   0 :::3306           :::*            LIST                                                 EN
[root@vcyber mysql]#
  • 第十步:使用 mysqladmin 验证服务器是否运行中。以下命令提供了简单的测试,检查服务器是否已经启动并能响应连接。
[root@vcyber mysql]# pwd
/usr/local/mysql
[root@vcyber mysql]# bin/mysqladmin version
bin/mysqladmin Ver 8.42 Distrib 5.6.28, for Linux on x86_64
Copyright (c) 2000, 2015, 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.
 
Server version     5.6.28
Protocol version    10
Connection       Localhost via UNIX socket
UNIX socket       /tmp/mysql.sock
Uptime:         9 min 7 sec
 
Threads: 1 Questions: 9 Slow queries: 0 Opens: 67 Flush tables: 1 Open tables: 60 Queries per second avg: 0.016
[root@vcyber mysql]#
[root@vcyber mysql]# bin/mysqladmin variables
+--------------------------------------------------------+----------------------------------------------------------------------------------+
| Variable_name                     | Value                                                                                                                                                                      |
+--------------------------------------------------------+----------------------------------------------------------------------------------+
| auto_increment_increment                | 1                                                                                                                                                                        |
| auto_increment_offset                 | 1                                                                                                                                                                        |
| autocommit                       | ON                                                                                                                                                                        |
| automatic_sp_privileges                | ON                                                                                                                                                                        |
| avoid_temporal_upgrade                 | OFF                                                                                                                                                                       |
| back_log                        | 80                                                                                                                                                                        |
| basedir                        | /usr/local/mysql                                                                                                                                                                 |
| big_tables                       | OFF                                                                                                                                                                       |
| bind_address                      | *                                                                                                                                                                        |
| binlog_cache_size                   | 32768                                                                                                                                                                      |
| binlog_direct_non_transactional_updates        | OFF                                                                                                                                                                       |
| binlog_max_flush_queue_time              | 0                                                                                                                                                                        |
……                                                                                                                                                                       |
+--------------------------------------------------------+----------------------------------------------------------------------------------+
[root@vcyber mysql]#

设置访问权限


  • 第十一步:设置访问权限。在 MySQL 安装过程中,使用 mysql_install_db 安装了 MySQL 数据库授权表,表定义了初始 MySQL 用户账号和访问权限,所有初始账号均没有密码。这些账号为超级用户,可以执行任何操作。初始 root 账户的密码为空,因此,任何人可以用 root 账户不用输入任何密码来连接 MySQL 服务器,并具有所有权限,这意味着 MySQL 安装未受保护。如果你想要防止客户端不使用密码,用匿名用户连接,应该为匿名账户指定密码或删掉匿名账户,应当为 MySQL root 账户指定密码。使用“mysql –u root”启动 MySQL 客户端控制台,连接 MySQL 服务器。命令行如下。
[root@vcyber mysql]# cd /usr/local/mysql
[root@vcyber mysql]# bin/mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.6.28 Source distribution
 
Copyright (c) 2000, 2015, 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 now();
+---------------------+
| now()        |
+---------------------+
| 2016-02-02 08:02:31 |
+---------------------+
1 row in set (0.00 sec)
 
mysql>
  • 第十二步:如果匿名账户存在,它拥有全部权限,因此,删掉它可以提高安全,在 MySQL 客户端执行如下命令。
mysql> select * from mysql.user \G
*************************** 1. row ***************************
         Host: localhost
         User: root
       Password:
      Select_priv: Y
      Insert_priv: Y
      Update_priv: Y
      Delete_priv: Y
      Create_priv: Y
       Drop_priv: Y
      Reload_priv: Y
     Shutdown_priv: Y
     Process_priv: Y
       File_priv: Y
      Grant_priv: Y
    References_priv: Y
      Index_priv: Y
      Alter_priv: Y
     Show_db_priv: Y
      Super_priv: Y
 Create_tmp_table_priv: Y
   Lock_tables_priv: Y
     Execute_priv: Y
    Repl_slave_priv: Y
   Repl_client_priv: Y
   Create_view_priv: Y
    Show_view_priv: Y
  Create_routine_priv: Y
  Alter_routine_priv: Y
   Create_user_priv: Y
      Event_priv: Y
     Trigger_priv: Y
Create_tablespace_priv: Y
       ssl_type:
      ssl_cipher:
      x509_issuer:
     x509_subject:
     max_questions: 0
      max_updates: 0
    max_connections: 0
 max_user_connections: 0
        plugin: mysql_native_password
 authentication_string:
   password_expired: N
*************************** 2. row ***************************
         Host: vcyber
         User: root
       Password:
      Select_priv: Y
      Insert_priv: Y
      Update_priv: Y
      Delete_priv: Y
      Create_priv: Y
       Drop_priv: Y
      Reload_priv: Y
     Shutdown_priv: Y
     Process_priv: Y
       File_priv: Y
      Grant_priv: Y
    References_priv: Y
      Index_priv: Y
      Alter_priv: Y
     Show_db_priv: Y
      Super_priv: Y
 Create_tmp_table_priv: Y
   Lock_tables_priv: Y
     Execute_priv: Y
    Repl_slave_priv: Y
   Repl_client_priv: Y
   Create_view_priv: Y
    Show_view_priv: Y
  Create_routine_priv: Y
  Alter_routine_priv: Y
   Create_user_priv: Y
      Event_priv: Y
     Trigger_priv: Y
Create_tablespace_priv: Y
       ssl_type:
      ssl_cipher:
      x509_issuer:
     x509_subject:
     max_questions: 0
      max_updates: 0
    max_connections: 0
 max_user_connections: 0
        plugin: mysql_native_password
 authentication_string:
   password_expired: N
*************************** 3. row ***************************
         Host: 127.0.0.1
         User: root
       Password:
      Select_priv: Y
      Insert_priv: Y
      Update_priv: Y
      Delete_priv: Y
      Create_priv: Y
       Drop_priv: Y
      Reload_priv: Y
     Shutdown_priv: Y
     Process_priv: Y
       File_priv: Y
      Grant_priv: Y
    References_priv: Y
      Index_priv: Y
      Alter_priv: Y
     Show_db_priv: Y
      Super_priv: Y
 Create_tmp_table_priv: Y
   Lock_tables_priv: Y
     Execute_priv: Y
    Repl_slave_priv: Y
   Repl_client_priv: Y
   Create_view_priv: Y
    Show_view_priv: Y
  Create_routine_priv: Y
  Alter_routine_priv: Y
   Create_user_priv: Y
      Event_priv: Y
     Trigger_priv: Y
Create_tablespace_priv: Y
       ssl_type:
      ssl_cipher:
      x509_issuer:
     x509_subject:
     max_questions: 0
      max_updates: 0
    max_connections: 0
 max_user_connections: 0
        plugin: mysql_native_password
 authentication_string:
   password_expired: N
*************************** 4. row ***************************
         Host: ::1
         User: root
       Password:
      Select_priv: Y
      Insert_priv: Y
      Update_priv: Y
      Delete_priv: Y
      Create_priv: Y
       Drop_priv: Y
      Reload_priv: Y
     Shutdown_priv: Y
     Process_priv: Y
       File_priv: Y
      Grant_priv: Y
    References_priv: Y
      Index_priv: Y
      Alter_priv: Y
     Show_db_priv: Y
      Super_priv: Y
 Create_tmp_table_priv: Y
   Lock_tables_priv: Y
     Execute_priv: Y
    Repl_slave_priv: Y
   Repl_client_priv: Y
   Create_view_priv: Y
    Show_view_priv: Y
  Create_routine_priv: Y
  Alter_routine_priv: Y
   Create_user_priv: Y
      Event_priv: Y
     Trigger_priv: Y
Create_tablespace_priv: Y
       ssl_type:
      ssl_cipher:
      x509_issuer:
     x509_subject:
     max_questions: 0
      max_updates: 0
    max_connections: 0
 max_user_connections: 0
        plugin: mysql_native_password
 authentication_string:
   password_expired: N
*************************** 5. row ***************************
         Host: localhost
         User:
       Password:
      Select_priv: N
      Insert_priv: N
      Update_priv: N
      Delete_priv: N
      Create_priv: N
       Drop_priv: N
      Reload_priv: N
     Shutdown_priv: N
     Process_priv: N
       File_priv: N
      Grant_priv: N
    References_priv: N
      Index_priv: N
      Alter_priv: N
     Show_db_priv: N
      Super_priv: N
 Create_tmp_table_priv: N
   Lock_tables_priv: N
     Execute_priv: N
    Repl_slave_priv: N
   Repl_client_priv: N
   Create_view_priv: N
    Show_view_priv: N
  Create_routine_priv: N
  Alter_routine_priv: N
   Create_user_priv: N
      Event_priv: N
     Trigger_priv: N
Create_tablespace_priv: N
       ssl_type:
      ssl_cipher:
      x509_issuer:
     x509_subject:
     max_questions: 0
      max_updates: 0
    max_connections: 0
 max_user_connections: 0
        plugin: mysql_native_password
 authentication_string: NULL
   password_expired: N
*************************** 6. row ***************************
         Host: vcyber
         User:
       Password:
      Select_priv: N
      Insert_priv: N
      Update_priv: N
      Delete_priv: N
      Create_priv: N
       Drop_priv: N
      Reload_priv: N
     Shutdown_priv: N
     Process_priv: N
       File_priv: N
      Grant_priv: N
    References_priv: N
      Index_priv: N
      Alter_priv: N
     Show_db_priv: N
      Super_priv: N
 Create_tmp_table_priv: N
   Lock_tables_priv: N
     Execute_priv: N
    Repl_slave_priv: N
   Repl_client_priv: N
   Create_view_priv: N
    Show_view_priv: N
  Create_routine_priv: N
  Alter_routine_priv: N
   Create_user_priv: N
      Event_priv: N
     Trigger_priv: N
Create_tablespace_priv: N
       ssl_type:
      ssl_cipher:
      x509_issuer:
     x509_subject:
     max_questions: 0
      max_updates: 0
    max_connections: 0
 max_user_connections: 0
        plugin: mysql_native_password
 authentication_string: NULL
   password_expired: N
6 rows in set (0.00 sec)
 
mysql>
mysql> DELETE FROM mysql.user WHERE Host='localhost' and User='';
Query OK, 1 rows affected(0.08 sec)
 
mysql> FLUSH PRIVILEGES;
Query OK, 1 rows affected(0.08 sec)
  • 第十三步:可以用几种方法为 root 账户指定密码。其中之一,在 MySQL 客户端命令行上使用 SET PASSWORD 指定密码,一定要使用 PASSWORD() 函数来加密密码。例如,下面设置 localhost 域的密码为“123456”。其他域可以使用同样的语句,SQL 语句如下。
 
mysql> SET PASSWORD FOR 'root'@'localhost'=PASSWORD('123456');
Query OK, 0 rows affected (0.02 sec)
 
mysql>
  • 第十四步:如果想退出 MySQL 客户端,可以输入 exit 或 quit,还可以用组合键 Ctrl+C。因为已经设置了 root 账户的密码,所以再次登录 MySQL 客户端就要提供密码才能进入。
mysql> exit
[root@vcyber mysql]#
[root@vcyber mysql]# bin/mysql -u root –h localhost -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.6.28 Source distribution
 
Copyright (c) 2000, 2015, 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>

如果想关闭 MySQL 服务器,在命令行使用 MySQL 服务器的 mysqladmin 命令,通过 –u 选项给出数据库管理员用户 root,-p 选项给出密码,即可关闭 MySQL 服务器。

[root@vcyber mysql]# bin/mysqladmin -u root -p shutdown
Enter password:
[root@vcyber mysql]#

MySQL 开机自动启动


  • 第十五步:有必要将 MySQL 服务设置成开机自动运行。方法是,进入 MySQL 源代码目录 /usr/local/src/mysql-5.6.28 中,将子目录 support-files 下的 mysql.server 文件复制到 /etc/rc.d/init.d/ 目录中,并重命令为 mysqld。
[root@vcyber mysql]# cd /usr/local/src/mysql-5.6.28
[root@vcyber mysql-5.6.28]# pwd
/usr/local/src/mysql-5.6.28
[root@vcyber mysql-5.6.28]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@vcyber mysql-5.6.28]#

修改 /etc/rc.d/init.d/mysqld 权限,如下所示:

[root@vcyber mysql-5.6.28]# chown root.root /etc/rc.d/init.d/mysqld
[root@vcyber mysql-5.6.28]# chmod 755 /etc/rc.d/init.d/mysqld
[root@vcyber mysql-5.6.28]#

使用 chkconfig 命令设置在不同系统运行级别下的自启动策略,首先使用“chkconfig –add mysqld”命令增加所指定的 mysqld 服务,让 chkconfig 指令得以管理它,并同时在系统启动的叙述文件内增加相关数据。使用命令如下所示:

[root@vcyber mysql-5.6.28]# chkconfig --add mysqld
[root@vcyber mysql-5.6.28]#

然后,使用“chkconfig –-level 3 mysqld on”命令和“chkconfig –-level 5 mysqld on”命令,在第三等级和第五等级中开启 mysqld 服务,即在字符模式和图形模式启动时自动开启 mysqld 服务。命令如下所示:

[root@vcyber mysql-5.6.28]# chkconfig --level 3 mysqld on
[root@vcyber mysql-5.6.28]# chkconfig --level 5 mysqld on
[root@vcyber mysql-5.6.28]#

再使用“chkconfig --list”命令检查设置。命令如下所示:

[root@vcyber mysql-5.6.28]# chkconfig --list mysqld
mysqld     0:off  1:off  2:on  3:on  4:on  5:on  6:off
[root@vcyber mysql-5.6.28]#

自此,MySQL 安装配置结束。


 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

原标题:源代码安装 MySQL 5.6.28

关键词:MYSQL

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。