Mysql单实例数据库安装

第1章 MySQL数据库安装

在当今的大中型互联网企业里,MySQL数据库服务几乎都是运行在Linux系列操作系统上,当然,你在可以运行在Windows/Unix等商业操作系统上,大中型互联网企业使用开源领域的产品MySQL数据库的目的是,软件代码的开放性和无版权免费应用节约成本。推荐中小型企业采用LAMP/LNMP网站架构,后面的数据库知识在Linux(CentOS6.5)平台为例讲述如何安装及使用MySQL软件的

不同场景不同版本的MySQL的安装有多种方法

1.1 yum/rpm方式安装MySQL

MySQL官方网站提供不同版本的RPM安装包,并且针对不同硬件或操作系统平台,又分为不同类型的安装包,大家可以在官网瞎咋页面进行选择,国内也有一些互联网公司提供了镜像,比如网易、阿里云公司提供的镜像资源

注意:yum/rpm方式安装MySQL适合所有MySQL软件产品系列

1)rpm包方式安装MySQL

rpm包的安装非常简单,以mysql-5.1.40为例,我们可以光盘或者网上下载到四个安装包:

 

其中,我们必须安装MySQL-server*和MySQLclient*软件包,对于另外两个则视实际需要,但是建议一起安装,为此我们可以把这四个RPM包至于某个目录中,然后执行shell命令:rpm -ivh MySQL-* 则可以完成MySQL软件安装任务,剩下的就是如何配置的问题了

2)yum安装方式mysql

yum方式安装mysql极其简单,只要执行yum install mysql-server即可

yum/rpm方式安装MySQL应用场景

yum/rpm安装适合对数据库要求不太高的场合,例如并发不大,公司内部,企业内部的一些应用场景

大的门户把源码根据企业的需求制作成rpm,搭建yum仓库,yum install xxx -y即可,yum方式的安装原理是在执行yum安装命令后,会自动从yum源地址下载相应名称的MySQL数据库rpm包,然后到系统上安装,并自动解决各种软件包之间的以来问题。

yum安装方式最大优点是超级简单,但是也有自身的问题,例如他继承了rpm包的无法定制化安装的问题,另一个缺点就是采用默认的yum安装时,一般yum贷的软件版本都比较低

1.2 常规方式编译安装MySQL

常规方式编译安装MySQL时,适合用第一条最正宗的MySQL产品线5.2及以前版本

所谓常规方式编译安装MySQL就是延续早期MySQL的3部曲安装方式,即./configure;make;make install ,生产场景的具体命令及参数为:

tar xf mysql-5.1.73.tar.gz

cd mysql-5.1.73

./configure

--prefix=/application/mysql-5.1.73

--with-unix-socket-path=/application/mysql-5.1.73/tmp/mysql.sock

--enable-assembler

--enable-thread-safe-client

--with-mysqld-user=mysql

--with-big-tables

--without-debug

--with-pthread 

--enable-assembler 

--with-extra-charsets=complex 

--with-readline 

--with-ssl 

--with-embedded-server 

--enable-local-infile 

--with-plugins=partition,innobase 

--with-mysqld-ldflags=-all-static 

--with-client-ldflags=-all-static

make

make install

ln -s /application/mysql-5.1.73 /application/mysql

安装到这里,MySQL数据库还不能正常启动工作,还需要进行初始化数据库等工作

此种方式适合所有MySQL5.2.xx以前的产品系列,时最常规的编译方式,在当下互联网企业中,此种编译安装的当打很少用,原因是第一条产品线的产品用的越来越少,备第二条产品逐渐带替

1.3 采用cmake方式编译安装MySQL

考虑到MySQL5.4.xx及以后产品的特殊性,起便宜方式和早期的产品线的的有所不同,这里采用cmake和gmake方式的编译安装。即./cmake;make;make install生产场景的具体命令如下

### mysql-5.6.34 编译参数
cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.6.34 
-DMYSQL_DATADIR=/application/mysql-5.6.34/data 
-DMYSQL_UNIX_ADDR=/application/mysql-5.6.34/tmp/mysql.sock 
-DDEFAULT_CHARSET=utf8 
-DDEFAULT_COLLATION=utf8_general_ci 
-DWITH_EXTRA_CHARSETS=all 
-DWITH_INNOBASE_STORAGE_ENGINE=1 
-DWITH_FEDERATED_STORAGE_ENGINE=1 
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 
-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 
-DWITH_ZLIB=bundled 
-DWITH_SSL=bundled 
-DENABLED_LOCAL_INFILE=1 
-DWITH_EMBEDDED_SERVER=1 
-DENABLE_DOWNLOADS=1 
-DWITH_DEBUG=0

make

make install

ln -s /application/mysql-5.6.34 /application/mysql

MySQL数据库的优化-笔者近六年来一线城市工作实战经验

http://www.xuliangwei.com/xubusi/213.html

安装到这里,NySQL数据库人无法正常启动使用,还需要进行初始化数据库等工作,另外cmake等用于编译的工具也需要提前进行安装

如果上述操作未出现错误,则MySQL5.6.34软件cmake方式的安装就算陈成功了

1.4 采用二进制方式免编译安装MySQL

采用二进制免编译方式安装MySQL的方法和yum/rpm包安装方式类似,适合各类MySQL产品系列,不需要复杂的编译设置及编译时间等待,直接解压下载的软件包,就相当于编译方式的make install步骤完成了。然后只要进行初始化数据库的操作,即可能完成并启动,此方式的MySQL软件包一般都比较大,最大可达300MB,采用二进制方式免编译安装MySQL的方法后文讲解

1.5 如何正确选择MySQL的安装方式

若是对数据库要求不太高的场景,采用yum/rpm方式安装MySQL,例如并发不打,只是在公司内部(wiki系统)、企业内部的应用(zabbix监控系统,OpenStack后台管理)等需要数据库的一些应用场景,当然,生产场景下也是可以选择yum/rpm方式进行安装的

但是,有很多大型网站或门户网站,往往在安装MySQL时,会有各种定制化、初始化的需求,要根据企业的需求先把源码包制作成rpm包,然后搭建自己的yum仓库,最终采用yum install mysql-server -y的方式安装,这样的优点是既兼顾了yum/rpm安装方式简单的优点,又用到了源码包安装方式的可定制性,但是,使用这个方法需要一定的技术能力

二进制免编译安装方式很简单方便,切适合5.0-5.1和5.5-5.7系列,是不少专业DBA的选择,普通运维人员一半多采用编译的方式安装,对应到MySQL5.0-5.1系列就是常规编译方式,对应到MySQL5.5-5.7系列就是cmake编译方式

所以综合来讲,这些安装方式都是可以使用的,只是不同层次的人习惯不同,实际应用的性能差距不是很大

建议:首先选择MySQL5.5或以上的数据库版本,当数据库服务器机器数量少的话,可采用cmake编译方式安装。当数据库服务器机器数量多的话,可用二进制免编译方式安装,这是某些DBA的偏爱,若是数据库服务器机器数量特别大,且对定制化要求很高,可以选择源码定制rpm包,搭建yum仓库的当时安装

第2章 安装并配置MySQL数据库

2.1 安装MySQL数据库

1)安装MySQL需要的依赖包

安装MySQL之前,最好先安装MySQL需要的依赖包,不然后面问题

[root@lb01 tools]# yum install ncurses-devel libaio-devel -y

[root@lb01 tools]# rpm -qa ncurses-devel libaio-devel

ncurses-devel-5.7-4.20090207.el6.x86_64

libaio-devel-0.3.107-10.el6.x86_64

2)安装编译MySQL需要的软件

由于MySQL5.5及以上系列的产品要采用特殊的编译方式安装,因此,需要安装用的编译MySQL的工具cmake软件包,命令为:

[root@lb01 tools]# yum install -y cmake

[root@lb01 tools]# rpm -qa cmake

cmake-2.8.12.2-4.el6.x86_64

此外,也有网友采用源码包的方式安装cmake的,单比较复杂,所以用yum安装cmake

2.2 开始安装MySQL

为了让大家学习更多的MySQL技术,本文选择了相对复杂的源代码安装方式为例来讲解MySQL多实例安装,大型公司一般都会讲MySQL软件定制成rpm包,然后放到yum仓库里,使用yum安装,中小企业里的二进制和编译安装的区别不打

1)建立MySQL用户账号

首先以root身份登录到Linux系统中,然后执行如下命令创建mysql用户账号

[root@mobanji ~]# useradd -s /sbin/nologin -M mysql

[root@mobanji ~]# id mysql

uid=501(mysql) gid=501(mysql) groups=501(mysql)

根据上述结果,可以看到MySQL用户和组已经创建成功

2)获取MySQL软件包

MySQL软件包可以从http://mirrors.sohu.com/mysql下载到客户端电脑,使用rz上传的Linux里,或者找到网络下载地址后,直接在Liunx里使用wget下载

提示:本例以MySQL编译的方式来讲解,使用二进制方式安装的完整在本文结尾。在生产场景中,二进制和源码包两种安装方法都是可以用的,其应用场景一般没什么太大区别,不同之处在与二进制的安装包较大,名字和源码包也有区别,二进制的安装比源码更快

MySQL二进制和源码包

MySQL软件

软件名

MySQL源码安装包

mysql-5.6.34.tar.gz

MySQL二进制安装包

mysql-5.6.35-linux-glibc2.5-x86_64.tar

3)采用编译方式安装MySQL

配置及编译安装的步骤如下:

第一步,下载mysql软件包

[root@mobanji ~]# mkdir /home/oldboy/tools -p

[root@mobanji ~]# cd /home/oldboy/tools/

[root@mobanji tools]# wget -q http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.34.tar.gz

[root@mobanji tools]# ll /home/oldboy/tools/ -h

total 31M

-rw-r--r-- 1 root root 31M Sep 30 17:55 mysql-5.6.34.tar.gz

第二步,解压配置mysql,命令如下:

[root@mobanji tools]# tar xf mysql-5.6.34.tar.gz

[root@mobanji tools]# cd mysql-5.6.34

第三步:编译mysql,命令如下

[root@mobanji mysql-5.6.34]# cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.6.34

-DMYSQL_DATADIR=/application/mysql-5.6.34/data

-DMYSQL_UNIX_ADDR=/application/mysql-5.6.34/tmp/mysql.sock

-DDEFAULT_CHARSET=utf8

-DDEFAULT_COLLATION=utf8_general_ci

-DWITH_EXTRA_CHARSETS=all

-DWITH_INNOBASE_STORAGE_ENGINE=1

-DWITH_FEDERATED_STORAGE_ENGINE=1

-DWITH_BLACKHOLE_STORAGE_ENGINE=1

-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1

-DWITH_ZLIB=bundled

-DWITH_SSL=bundled

-DENABLED_LOCAL_INFILE=1

-DWITH_EMBEDDED_SERVER=1

-DENABLE_DOWNLOADS=1

-DWITH_DEBUG=0

 

[root@mobanji tools]# make   ###如果是多核cpu,可指定make -j cpu核数,加快编译参数

[root@mobanji tools]# make install

提示:

1)编译时可配置的选项很多,常见的参数选项见本庄最后

2)编译MySQL需要安装gcc等工具,此部分在安装操作系统是已经安装上了

yum install gcc gcc-c++ ncurses-devel perl

第四步,为MySQL安装路径设置不带版本号的软链接/application/mysql,操作命令如下

[root@mobanji tools]# ln -s /application/mysql-5.6.34/ /application/mysql

##补充:如果系统里有曾经安装的数据库文件和启动程序最好停掉或删除,以免冲突

[root@mobanji mysql-5.6.34]# ll /application/

total 4

lrwxrwxrwx  1 root root   26 Mar 15 23:17 mysql -> /application/mysql-5.6.34/

drwxr-xr-x 13 root root 4096 Mar 15 23:17 mysql-5.6.34

[root@mobanji mysql-5.6.34]# ls /application/mysql

bin  COPYING  data  docs  include  lib  man  mysql-test  README  scripts  share  sql-bench  support-files

如果上述操作未出现错误(每个步骤结束后,都可以使用echo $?看返回值是否为0,为0表示正确),查看/application/mysql/目录,若其下有内容,则表示mysql-5.6.34源码包采用cmake方式安装成功

2.3 创建MySQL数据库配置文件并对数据库目录授权

mysql-5.5数据库默认为用户提供了多个配置文件模板,但是mysql-.的support-files目录下已经没有配置文件模板了

[root@mobanji mysql-5.6.34]# ll support-files/*.cnf

-rw-r--r-- 1 root root 1126 Mar 15 22:59 support-files/my-default.cnf

[root@mobanji mysql-5.6.34]# mv /etc/my.cnf.bak /etc/my.cnf

##提示:在CentOS 6.8 版操作系统最小化安装完成后,在/etc/目录下会存在一个my.cnf,需要将此文件更名为其他名字,如:/etc/my.cnf.bak,否则,该文件会干扰源码安装的mysql的正确配置,造成无法启动

##在启动mysql服务时,会按照一定的顺序搜索my.cnf,先在/etc/目录下找,找不到则会搜索“$basedir/my.cnf”,在本例中就是/application/mysql-5.6.34/my.cnf,就是最新mysql的配置文件的默认位置

[root@mobanji mysql-5.6.34]# cp support-files/*.cnf /etc/my.cnf

#提示:慈航操作可以省略,在下文初始化musql时会自动生成my.cnf模板文件如果已经执行上述命令,则初始化后会生成my-new.cnf文件,my.cnf和my-new.cnf除了注释意外是一致的

[root@mobanji mysql-5.6.34]# chown -R mysql.mysql /application/mysql/

#授权mysql用护肤案例mysql的安装目录

[root@mobanji mysql-5.6.34]# ll /application/mysql/

total 68

drwxr-xr-x  2 mysql mysql  4096 Mar 15 23:17 bin

-rw-r--r--  1 mysql mysql 17987 Sep 30 19:41 COPYING

drwxr-xr-x  3 mysql mysql  4096 Mar 15 23:16 data

drwxr-xr-x  2 mysql mysql  4096 Mar 15 23:16 docs

drwxr-xr-x  3 mysql mysql  4096 Mar 15 23:16 include

drwxr-xr-x  3 mysql mysql  4096 Mar 15 23:16 lib

drwxr-xr-x  4 mysql mysql  4096 Mar 15 23:16 man

drwxr-xr-x 10 mysql mysql  4096 Mar 15 23:17 mysql-test

-rw-r--r--  1 mysql mysql  2496 Sep 30 19:41 README

drwxr-xr-x  2 mysql mysql  4096 Mar 15 23:17 scripts

drwxr-xr-x 28 mysql mysql  4096 Mar 15 23:17 share

drwxr-xr-x  4 mysql mysql  4096 Mar 15 23:17 sql-bench

drwxr-xr-x  2 mysql mysql  4096 Mar 15 23:17 support-files

2.4 初始化MySQL数据库文件

上述配置完毕后,就可以初始化数据库文件了,这个步骤其实也可以在编译安装MySQL之后就操作,只不过放到这里更合理一些

1)初始化MySQL数据库

初始化数据库的核心命令为:

/application/mysql/scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/application/mysql/data --user=mysql

提示:--basedir=/application/mysql/为MySQL的安装路径,--datadir为数据文件目录。另注意,mysql_install_db和mysql 5.1 的路径不同,mysql 5.1不在mysql bin路径下了

整个初始化的操作过程为:

[root@mobanji mysql-5.6.34]# /application/mysql/scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/application/mysql/data --user=mysql

###初始化mysql数据库文件,会有很多信息提示,如果没有ERROR级别的错误,有两个OK的字样,表示初始化成功,否则就要解决初始化的问题

Installing MySQL system tables...2017-03-15 23:52:51 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).

2017-03-15 23:52:51 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.

2017-03-15 23:52:51 0 [Note] /application/mysql//bin/mysqld (mysqld 5.6.34) starting as process 16046 ...

2017-03-15 23:52:51 16046 [Note] InnoDB: Using atomics to ref count buffer pool pages

2017-03-15 23:52:51 16046 [Note] InnoDB: The InnoDB memory heap is disabled

2017-03-15 23:52:51 16046 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins

2017-03-15 23:52:51 16046 [Note] InnoDB: Memory barrier is not used

2017-03-15 23:52:51 16046 [Note] InnoDB: Compressed tables use zlib 1.2.3

2017-03-15 23:52:51 16046 [Note] InnoDB: Using Linux native AIO

2017-03-15 23:52:51 16046 [Note] InnoDB: Using CPU crc32 instructions

2017-03-15 23:52:51 16046 [Note] InnoDB: Initializing buffer pool, size = 128.0M

2017-03-15 23:52:51 16046 [Note] InnoDB: Completed initialization of buffer pool

2017-03-15 23:52:51 16046 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!

2017-03-15 23:52:51 16046 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB

2017-03-15 23:52:51 16046 [Note] InnoDB: Database physically writes the file full: wait...

2017-03-15 23:52:52 16046 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB

2017-03-15 23:52:53 16046 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB

2017-03-15 23:52:54 16046 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0

2017-03-15 23:52:54 16046 [Warning] InnoDB: New log files created, LSN=45781

2017-03-15 23:52:54 16046 [Note] InnoDB: Doublewrite buffer not found: creating new

2017-03-15 23:52:54 16046 [Note] InnoDB: Doublewrite buffer created

2017-03-15 23:52:55 16046 [Note] InnoDB: 128 rollback segment(s) are active.

2017-03-15 23:52:55 16046 [Warning] InnoDB: Creating foreign key constraint system tables.

2017-03-15 23:52:55 16046 [Note] InnoDB: Foreign key constraint system tables created

2017-03-15 23:52:55 16046 [Note] InnoDB: Creating tablespace and datafile system tables.

2017-03-15 23:52:55 16046 [Note] InnoDB: Tablespace and datafile system tables created.

2017-03-15 23:52:55 16046 [Note] InnoDB: Waiting for purge to start

2017-03-15 23:52:55 16046 [Note] InnoDB: 5.6.34 started; log sequence number 0

2017-03-15 23:52:55 16046 [Note] Binlog end

2017-03-15 23:52:55 16046 [Note] InnoDB: FTS optimize thread exiting.

2017-03-15 23:52:55 16046 [Note] InnoDB: Starting shutdown...

2017-03-15 23:52:56 16046 [Note] InnoDB: Shutdown completed; log sequence number 1625977

OK

 

Filling help tables...2017-03-15 23:52:56 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).

2017-03-15 23:52:56 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.

2017-03-15 23:52:56 0 [Note] /application/mysql//bin/mysqld (mysqld 5.6.34) starting as process 16068 ...

2017-03-15 23:52:56 16068 [Note] InnoDB: Using atomics to ref count buffer pool pages

2017-03-15 23:52:56 16068 [Note] InnoDB: The InnoDB memory heap is disabled

2017-03-15 23:52:56 16068 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins

2017-03-15 23:52:56 16068 [Note] InnoDB: Memory barrier is not used

2017-03-15 23:52:56 16068 [Note] InnoDB: Compressed tables use zlib 1.2.3

2017-03-15 23:52:56 16068 [Note] InnoDB: Using Linux native AIO

2017-03-15 23:52:56 16068 [Note] InnoDB: Using CPU crc32 instructions

2017-03-15 23:52:56 16068 [Note] InnoDB: Initializing buffer pool, size = 128.0M

2017-03-15 23:52:56 16068 [Note] InnoDB: Completed initialization of buffer pool

2017-03-15 23:52:56 16068 [Note] InnoDB: Highest supported file format is Barracuda.

2017-03-15 23:52:56 16068 [Note] InnoDB: 128 rollback segment(s) are active.

2017-03-15 23:52:56 16068 [Note] InnoDB: Waiting for purge to start

2017-03-15 23:52:57 16068 [Note] InnoDB: 5.6.34 started; log sequence number 1625977

2017-03-15 23:52:57 16068 [Note] Binlog end

2017-03-15 23:52:57 16068 [Note] InnoDB: FTS optimize thread exiting.

2017-03-15 23:52:57 16068 [Note] InnoDB: Starting shutdown...

2017-03-15 23:52:59 16068 [Note] InnoDB: Shutdown completed; log sequence number 1625987

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:

 

  /application/mysql//bin/mysqladmin -u root password 'new-password'

  /application/mysql//bin/mysqladmin -u root -h mobanji password 'new-password'

 

Alternatively you can run:

 

  /application/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 . ; /application/mysql//bin/mysqld_safe &

 

You can test the MySQL daemon with mysql-test-run.pl

 

  cd mysql-test ; perl mysql-test-run.pl

 

Please report any problems at http://bugs.mysql.com/

 

The latest information about MySQL is available on the web at

 

  http://www.mysql.com

 

Support MySQL by buying support/licenses at http://shop.mysql.com

####请注意如下几行英文的说明

New default config file was created as /application/mysql//my.cnf and

will be used by default by the server when you start it.

You may edit this file to change server settings

###从上文说明可以知道mysql的默认配置文件已经变到了/application/mysql/my.cnf

WARNING: Default config file /etc/my.cnf exists on the system

This file will be read by default by the MySQL server

If you do not want to use this, either remove it, or use the

--defaults-file argument to mysqld_safe when starting the server

#从上文说明可以看到数据库启动会读取/etc/my.cnf,因此有可能会导致无法启动,避免的方法就是使用mysql_safe启动服务时采用--defaulte-file参数指定配置文件,前文已将/etc/my.cnf改名了,所以就不需要指定参数了

此步骤必须要初始化成功,否则,后面会出现登录不了数据库等各种问题

2)初始化数据库的原理及结果说明

初始化数据库的实质就是创建基础的数据库系统的库文件,例如:生成MySQL库表等

初始化数据库后,查看数据目录,可以看到了如下文件:

[root@mobanji mysql-5.6.34]# ll /application/mysql/data/

total 110604

-rw-rw---- 1 mysql mysql 12582912 Mar 15 23:52 ibdata1

-rw-rw---- 1 mysql mysql 50331648 Mar 15 23:52 ib_logfile0

-rw-rw---- 1 mysql mysql 50331648 Mar 15 23:52 ib_logfile1

drwx------ 2 mysql mysql     4096 Mar 15 23:52 mysql  ##用于存放管理mysql的数据

drwx------ 2 mysql mysql     4096 Mar 15 23:52 performance_schema  ##5.5及以上增加的内部性能库

drwxr-xr-x 2 mysql mysql     4096 Mar 15 23:16 test  ##如果没有tree,可以yum install tree -y

2.5 经MySQL相关命令加入全局路径

如果不为Mysql的命令配置全局路径,就无法直接在命令行输入mysql这样的命令只能用全路径命令(/application/mysql/bin/mysql),这样呆着路径输入命令的方式很麻烦

##环境变量设置

[root@lb02 mysql-5.6.34]# echo 'PATH=/application/mysql/bin/:$PATH' >>/etc/profile

[root@lb02 mysql-5.6.34]# source /etc/profile

[root@lb02 mysql-5.6.34]# mysql

Welcome to the MySQL monitor.  Commands end with ; or g.

Your MySQL connection id is 2

Server version: 5.6.34 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> quit

Bye

单实例安装完整步骤

安装依赖

yum install ncurses-devel libaio-devel cmake -y

rpm -qa ncurses-devel libaio-devel cmake

yum install cmake -y

rpm -qa cmake

##创建数据库用户

useradd -s /sbin/nologin -M mysql

id mysql

##下载源码包

cd /home/oldboy/tools/

wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.34.tar.gz

ls -l mysql-5.6.34.tar.gz

###解压

tar xf mysql-5.6.34.tar.gz

cd mysql-5.6.34

 

###编译参数

cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.6.34

-DMYSQL_DATADIR=/application/mysql-5.6.34/data

-DMYSQL_UNIX_ADDR=/application/mysql-5.6.34/tmp/mysql.sock

-DDEFAULT_CHARSET=utf8

-DDEFAULT_COLLATION=utf8_general_ci

-DWITH_EXTRA_CHARSETS=all

-DWITH_INNOBASE_STORAGE_ENGINE=1

-DWITH_FEDERATED_STORAGE_ENGINE=1

-DWITH_BLACKHOLE_STORAGE_ENGINE=1

-DWITHOUT_EXAMPLE_STORAGE_ENGINE=1

-DWITH_ZLIB=bundled

-DWITH_SSL=bundled

-DENABLED_LOCAL_INFILE=1

-DWITH_EMBEDDED_SERVER=1

-DENABLE_DOWNLOADS=1

-DWITH_DEBUG=0

 

make && make install

##做软链接

ln -s /application/mysql-5.6.34/ /application/mysql

[root@mobanji 3306]# ll /application/

total 4

lrwxrwxrwx  1 root  root    26 Mar 15 23:17 mysql -> /application/mysql-5.6.34/

drwxr-xr-x 14 mysql mysql 4096 Mar 16 11:09 mysql-5.6.34

 

初始化

/application/mysql/scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/application/mysql/data --user=mysql

 

cp support-files/mysql.server /etc/init.d/mysqld

chmod 700 /etc/init.d/mysqld

chkconfig mysqld on

chkconfig --list mysqld

 

[root@lb02 mysql-5.6.34]# /etc/init.d/mysqld start

Starting MySQL.. SUCCESS!

 

[root@lb02 mysql-5.6.34]# ss -lntup|grep 3306

tcp    LISTEN     0      80                    :::3306                 :::*      users:(("mysqld",19769,10))

[root@lb02 mysql-5.6.34]# /application/mysql/bin/mysql

 

排错:看日志

[root@lb02 mysql-5.6.34]# tail -100 /application/mysql/data/lb02.err

##环境变量设置

[root@lb02 mysql-5.6.34]# echo 'PATH=/application/mysql/bin/:$PATH' >>/etc/profile

[root@lb02 mysql-5.6.34]# source /etc/profile

[root@lb02 mysql-5.6.34]# mysql

Welcome to the MySQL monitor.  Commands end with ; or g.

Your MySQL connection id is 2

Server version: 5.6.34 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> quit

Bye

2.6 配置并启动MySQL数据库

1)设置MySQL启动脚本

[root@mobanji mysql-5.6.34]# pwd

/home/oldboy/tools/mysql-5.6.34

[root@mobanji mysql-5.6.34]# cp support-files/mysql.server /etc/init.d/mysqld  ##拷贝mysql启动脚本到mysql的命令路径

[root@mobanji mysql-5.6.34]# chmod 700 /etc/init.d/mysqld   #是脚本可执行

[root@mobanji mysql-5.6.34]# ll /etc/init.d/mysqld

-rwx------ 1 root root 10929 Mar 16 00:06 /etc/init.d/mysqld

2)启动MySQL数据库

##这时启动数据库规范方法之一,还可以使用/application/mysql/bin/mysql_safe --user=mysql &启动。这个命令结尾“&”符号作用是,在后台执行mysql服务,这条命令执行完,还需要按下回车才能进入到命令行状态

##注意,如果以执行上面/etc/init.d/mysqld start启动命令,还行尝试下/application/mysql/bin/mysql_safe --user=mysql & 命令,先执行/einit.d/mysqld stop结束mysql进程

[root@mobanji ~]# /etc/init.d/mysqld start

Starting MySQL.. SUCCESS!

提示:禁止使用pkill、kill -9、killall -9等命令强制杀死数据库这会引起数据库无法启动等故障

3)检查MySQL数据库是否启动

[root@mobanji ~]# ss -lntup|grep mysql

tcp    LISTEN     0      80                    :::3306                 :::*      users:(("mysqld",16273,10))

4)设置环境变量

[root@mobanji ~]# echo 'PATH=/application/mysql/bin/:$PATH' >>/etc/profile

[root@mobanji ~]# source /etc/profile

[root@mobanji ~]# mysql

Welcome to the MySQL monitor.  Commands end with ; or g.

Your MySQL connection id is 1

Server version: 5.6.34 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>

5)开机启动

[root@mobanji ~]# chkconfig mysqld on

 

[root@mobanji ~]# chkconfig --list mysqld

mysqld           0:off 1:off 2:on  3:on  4:on  5:on  6:off

若MySQL安装及使用出现故障,可根据下面的分析思路进行检查

l  细看所有执行命令返回的屏幕输出,不要忽略关键的输出内容

l  辅助查看系统日志/var/log/messages

l  仔细阅读操作步骤

2.6.1 为root用户设置密码

MySQL管理员的账号root密码默认为空,既不安全,可以通过mysqladmin命令为imysql不同的实例的数据库设置独立的密码

[root@mobanji ~]# mysqladmin -u root password 'oldboy123'  #为用户设置密码oldboy123

Warning: Using a password on the command line interface can be insecure.  ##这是一个警告,提醒用户命令行防止面膜是既不安全的,采取交互式的密码输入方式

[root@mobanji ~]# mysql   ###无法直接输入命令登陆

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

[root@mobanji ~]# mysql -uroot -p    ##新的登陆方式也可以直接带密码mysql -uroot -poldboy123  登录

Enter password:    ###输入新密码oldboy123,交互式输入密码不会记录在命令记录里,因此更安全

Welcome to the MySQL monitor.  Commands end with ; or g.

Your MySQL connection id is 8

Server version: 5.6.34 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>

也可以执行mysql_secure_installation命令交互式的设置系统的用户密码

2.6.2 清理mysql服务器内无用的用户

mysql> select user,host from mysql.user;

+------+-----------+

| user | host      |

+------+-----------+

| root | 127.0.0.1 |

| root | ::1       |

|      | localhost |

| root | localhost |

|      | mobanji   |

| root | mobanji   |

+------+-----------+

6 rows in set (0.00 sec)

 

mysql> drop user 'root'@'::1';

Query OK, 0 rows affected (0.00 sec)

 

mysql> drop user ''@'localhost';

Query OK, 0 rows affected (0.00 sec)

 

mysql> drop user ''@'mobanji';

Query OK, 0 rows affected (0.00 sec)

 

mysql> drop user 'root'@'mobanji';

Query OK, 0 rows affected (0.00 sec)

 

mysql> select user,host from mysql.user;

+------+-----------+

| user | host      |

+------+-----------+

| root | 127.0.0.1 |

| root | localhost |

+------+-----------+

2 rows in set (0.00 sec)

提示:对于drop命令及数据库安全,后文讲,此处不执行也行

优势drop命令可能无法删除对应用户。比如,当数据库内的host等字段为大写及特殊Linux主机名时,删除用户就会遇到问题,例如:

mysql> drop user ‘’@’MySQL’;

ERROR 1396 (HY000):Operation DROP USER failed for ‘’@’MySQL’

可使用DML预计,并采用delete命令删除来解决此问题,具体命令如下:

mysql> delete from mysql.user where user=’’ and host=’MySQL’;

Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

2.6.3 删除mysql数据库内无用的test库

mysql> drop database test;

Query OK, 0 rows affected (0.01 sec)

 

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

+--------------------+

3 rows in set (0.04 sec)

2.7 MySQL安装FAQ

实例2-1  在配置mysql时遇到错误

出现的错误如下:

checking for tgetent in-ltinfo… no

checking for termcap functions library… configure:error:No curses/termcap library found

原因:缺少ncurses安装包

解决方法:使用yum install -y ncurses-devel命令

实例2-2  初始化MySQL数据库时出现错误

故障1 :给出了警告信息“WARNING:The host ‘oldboy’ could not be looked up with resolveip”

       警告是可以忽略的,如果非要解决则需修改对主机名的解析,使其和uname -n 命令的结果一样

[root@mobanji ~]# grep "`uname -n`" /etc/hosts

127.0.0.1       mobanji

故障2 : 错误提示ERROR:1004 Can’t create file ‘/tmp/#sql300e_1_0.frm’ (erno:13)

在执行初始化数据库命令时可能会遇到这样的错误,错误提示如下:

ERROR:1004 Can’t create file ‘/tmp/#sql300e_1_0.frm’ (erno:13)

120406 15:47:02 [ERROR] Aborting

2017-03-16 00:16:26 0 [Note] /application/mysql-5.6.34/bin/mysqld : Shutdown complete

Installation of system tables failed! Eamine the logs in

/application/mysql/data for more information

根据提示可知,/tmp目录下不能创建文件,所以解决办法为给/tmp目录增加权限,如下

解决办法:还原/tmp目录权限,操作命令如下:

[root@mobanji ~]# chmod 1777 /tmp/   ##1777时/tmp的默认权限,除非曾经改动过此目录权限才会报错

[root@mobanji ~]# ll -d /tmp/

drwxrwxrwt. 3 root root 4096 Mar 16 00:16 /tmp/

本例的故障必须要解除,否则,后面会出现登陆不了MySQL数据库等各种问题

实例2-3  登陆数据库时提示“Access denied fprr user ‘root’@’localhost’”

[root@mobanji ~]# mysql

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

解决办法:输入密码才能登陆

[root@mobanji ~]# mysql

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

[root@mobanji ~]# mysql -uroot -poldboy123

Warning: Using a password on the command line interface can be insecure.

Welcome to the MySQL monitor.  Commands end with ; or g.

Your MySQL connection id is 5

Server version: 5.6.34 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> quit

Bye

                                            

原文地址:https://www.cnblogs.com/Nayears/p/12107620.html