centos 7.3 安装 mysql-5.7.22-linux-glibc2.5-x86_64

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/eagle89/article/details/83105408

1、下载:

[root@localhost ~]#  wget  https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.22-linux-glibc2.5-x86_64.tar.gz 

2、解压 :

tar  zxvf mysql-5.7.22-linux-glibc2.5-x86_64.tar.gz -C /usr/local  --解压到指定目录 然后再进行重命名 为mysql

3、创建mysql 账户 

[root@localhost ~]#useradd -M -s /sbin/nologin  mysql   -- M 不建立使用者目录

                                       或者  groupadd mysql

                                                 useradd -g mysql mysql

[root@localhost ~]# mkdir -p /data/mysqldata

[root@localhost ~]# chown  -R mysql:mysql /data/mysqldata  --将mysqldata所属主所属组赋予mysql用户权限

4、进行初始化:

[root@localhost ~]#cd /usr/local/mysql5.7.22

[root@localhost ~]#./bin/mysqld  --initialize --user=mysql --datadir= /data/mysqldata  

注意,这一步最后一行会有一个提示
[Note] A temporary password is generated for root@localhost: -gLjbSisv1uh

5、拷贝配置文件和启动脚本

修改 /etc/my.cnf 文件。

vim /etc/my.cnf //编辑或者修改

[mysqld]   --在这里修改
basedir = /usr/local/mysql
datadir = /data/mysqldata
port = 3306
socket = /tmp/mysql.sock     -注意路径不能出错,否则启动出错 

cp support-files/mysql.server /etc/init.d/mysqld   --复制启动脚本到/etc/init.d下
vi /etc/init.d/mysqld  --编辑或者修改
basedir=/usr/local/mysql
datadir=/data/mysqldata

 

6. 启动服务
/etc/init.d/mysqld start

加入开机启动
chkconfig --add mysqld
chkconfig mysqld on

或者

mysqld --defaults-file=/usr/local/mysql5.7.22/my.cnf --user=mysql &

7、设置root密码

注:编辑  vi /etc/profile  配置环境变量
export PATH=$PATH:/usr/local/mysql5.7.22/bin

# ln -s /usr/local/mysql/bin/mysql /usr/bin 映射一个链接到/usr/bin目录下,建立一个链接文件。

mysql -uroot -p/进入mysql (密码为刚刚安装完成密码-gLjbSisv1uh)

成功登陆后 ,在输入show databases 会提示你重新设置密码,

mysql>set password = password(‘mypass‘); 退出来,再使用新的密码登录就可以了、

2>.还有一种情况,就是不知道初始化密码
vi /etc/my.cnf
在[mysqld]下面增加一行
skip-grant-tables
重启  /etc/init.d/mysqld restart

 mysql -uroot 

mysql> update user set authentication_string=password(‘123333‘) where user=‘root‘;
退出来后,更改my.cnf,去掉刚加的 skip-grant-tables
重启 /etc/init.d/mysqld restart
此时就可以使用新的密码了。

此时安装完成mysql 5.7

Mysql与Mariadb

    Mysql是一个关系型数据库,由mysql ab公司开发,在2008年被sun公司收购,2009年被oracle公司收购。mysql最新版本是5.7GA/8.0DMR。mysql 5.6版本变化比较大,5.7版本性能上有很大提升。

    Mariadb是mysql的一个分支,最新版本是10.2版本。它是由Mysql原作者带领大部分原班人马创立的SkySQL公司维护。

    Mariadb5.5版本对应MySQL的5.5版本,10.0版本对应MySQL的5.6版本。

Tips:

Community 社区版本,Enterprise 企业版,

GA(Generally Available)指通用版本,在生产环境中用的,

DMR(Development Milestone Release)开发里程碑发布版,

RC(Release Candidate)发行候选版本,Beta开放测试版本,Alpha内部测试版本。

安装Mariadb

1、下载安装文件

[root@juispan src]# wget https://downloads.mariadb.com/MariaDB/mariadb-10.2.6/bintar-linux-glibc_214-x86_64/mariadb-10.2.6-linux-glibc_214-x86_64.tar.gz

--2017-07-21 07:01:06--  https://downloads.mariadb.com/MariaDB/mariadb-10.2.6/bi             ntar-linux-glibc_214-x86_64/mariadb-10.2.6-linux-glibc_214-x86_64.tar.gz

正在解析主机 downloads.mariadb.com (downloads.mariadb.com)... 51.255.94.155, 200             1:41d0:1004:249b::

正在连接 downloads.mariadb.com (downloads.mariadb.com)|51.255.94.155|:443... 已             连接。

已发出 HTTP 请求,正在等待回应... 200 OK

长度:541295045 (516M) [application/octet-stream]

正在保存至: “mariadb-10.2.6-linux-glibc_214-x86_64.tar.gz”

5% [====>                                                                              7% [==>                                          ] 38,395,559  62.6KB/s 剩余 1h 40m

2、解压并移动

[root@juispan src]# tar zxvfmariadb-10.2.6-linux-glibc_214-x86_64.tar.gz

......

[root@juispan src]# mv mariadb-10.2.6-linux-glibc_214-x86_64 /usr/local/mariadb

3、创建用户

[root@juispan mariadb]# useradd mysql

4、初始化数据库

[root@juispan src]# cd /usr/local/mariadb

[root@juispan mariadb]# ls

bin                 data               include         mysql-test    share

COPYING             DESTINATION        INSTALL-BINARY  README.md     sql-bench

COPYING.thirdparty  docs               lib             README-wsrep  support-files

CREDITS             EXCEPTIONS-CLIENT  man             scripts

[root@server02 mariadb]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mariadb

Installing MariaDB/MySQL system tables in ‘/data/mariadb‘ ...

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 MariaDB root USER !

To do so, start the server, then issue the following commands:

‘./bin/mysqladmin‘ -u root password ‘new-password‘

‘./bin/mysqladmin‘ -u root -h server02 password ‘new-password‘

Alternatively you can run:

‘./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 MariaDB Knowledgebase at http://mariadb.com/kb or the

MySQL manual for more instructions.

You can start the MariaDB daemon with:

cd ‘.‘ ; ./bin/mysqld_safe --datadir=‘/data/mariadb‘

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

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

Please report any problems at http://mariadb.org/jira

The latest information about MariaDB is available at http://mariadb.org/.

You can find additional information about the MySQL part at:

http://dev.mysql.com

[root@juispan mariadb]# echo $?

0

初始化显示“OK”和“echo $?”校验都表明了已正常初始化。

5、拷贝配置文件

[root@juispan mariadb]# cp support-files/my-small.cnf /usr/local/mariadb/my.cnf

[root@juispan mariadb]# vi /usr/local/mariadb/my.cnf

......

basedir         =/usr/local/mariadb

datadir         =/data/mariadb

......

是选用“my-small.conf”还是“my-huge.cnf ”等这种文件和设备的配置有关,根据实际设备配置选择。

vi /etc/profile.d

export  PATH=/usr/local/mysql/bin:$PATH

source /etc/profile  使这个配置文件及时生效

6、拷贝启动脚本文件

[root@juispan mariadb]# cp support-files/mysql.server /etc/init.d/mariadb

[root@juispan mariadb]# vi /etc/init.d/mariadb

......

basedir=/usr/local/mariadb      ##定义basedir

datadir=/data/mariadb           ##定义datadir

conf=$basedir/my.cnf            ##定义conf

......

    then

      # Give extra arguments to mysqld with the my.cnf file. This script

      # may be overwritten at next upgrade.

      $bindir/mysqld_safe --defaults-file="$conf"--datadir="$datadir" --pid-file="$mysqld_pid_file_path" "$@" &        ##增加defaults-file启动参数

      wait_for_ready; return_value=$?

......

7、启动Mysql

[root@juispan mariadb]# chkconfig --add mariadb

[root@juispan mariadb]# chkconfig --list

注意:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。

      如果您想列出 systemd 服务,请执行 ‘systemctl list-unit-files‘。

      欲查看对特定 target 启用的服务请执行

      ‘systemctl list-dependencies [target]‘。

mariadb         0:关     1:关     2:开     3:开     4:开     5:开     6:关

netconsole      0:关     1:关     2:关     3:关     4:关     5:关     6:关

network         0:关     1:关     2:开     3:开     4:开     5:开     6:关

[root@juispan mariadb]# /etc/init.d/mariadb start

Reloading systemd:                                         [  确定  ]

Starting mariadb (via systemctl):  Job for mariadb.service failed because the control process exited with error code. See "systemctl status mariadb.service" and "journalctl -xe" for details.

                                                           [失败]

[root@juispan mariadb]# systemctl status mariadb.service

● mariadb.service - LSB: start and stop MySQL

   Loaded: loaded (/etc/rc.d/init.d/mariadb; bad; vendor preset: disabled)

   Active: failed (Result: exit-code) since 五 2017-07-21 08:13:42 CST; 35s ago

     Docs: man:systemd-sysv-generator(8)

  Process: 10559 ExecStart=/etc/rc.d/init.d/mariadb start (code=exited, status=1/FAILURE)

7月 21 08:13:41 juispan mariadb[10559]: Fatal error in defaults handling. Program aborted

7月 21 08:13:41 juispan mariadb[10559]: WARNING: Defaults file ‘/usr/local/mariadb/my.cnf--datadir=/var/lib/mysql‘ not found!

7月 21 08:13:41 juispan mariadb[10559]: 170721 08:13:41 mysqld_safe Logging to ‘/usr/local/mysql/data/juispan.err‘.

7月 21 08:13:41 juispan mariadb[10559]: 170721 08:13:41 mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data

7月 21 08:13:41 juispan mariadb[10559]: [147B blob data]

7月 21 08:13:42 juispan mariadb[10559]: ERROR!

7月 21 08:13:42 juispan systemd[1]: mariadb.service: control process exited, code=exited status=1

7月 21 08:13:42 juispan systemd[1]: Failed to start LSB: start and stop MySQL.

7月 21 08:13:42 juispan systemd[1]: Unit mariadb.service entered failed state.

7月 21 08:13:42 juispan systemd[1]: mariadb.service failed.

发现“ Defaults file ‘/usr/local/mariadb/my.cnf--datadir=/var/lib/mysql‘ not found!”错误。查看启动脚本,发现定义defaults-file的时候,两个参数之间没有空格导致。

[root@juispan mariadb]# /etc/init.d/mariadb start

Starting mariadb (via systemctl):                          [  确定  ]

[root@juispan mariadb]# netstat -lnpt

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address    Foreign Address         State       PID/Program name

tcp        0      0 0.0.0.0:22       0.0.0.0:*               LISTEN      1337/ssh

tcp        0      0 127.0.0.1:25     0.0.0.0:*               LISTEN      1863/master    

tcp        0      0 0.0.0.0:873      0.0.0.0:*               LISTEN      2368/rsync     

tcp6       0      0 :::3306          :::*                    LISTEN      3307/mysqld    

tcp6       0      0 :::22            :::*                    LISTEN      1337/sshd      

tcp6       0      0 ::1:25           :::*                    LISTEN      1863/master    

tcp6       0      0 :::873           :::*                    LISTEN      2368/rsync

综上所述:mariadb的安装简直和mysql一模一样。如果服务器上面只有mariadb这个服务,我们完全可以把my.cnf放在etc下,同时也就不需要定义conf以及变量了。

Mariadb二进制安装和配置说明

相关软件包介绍

mariadb-10.2.11.tar.gz   源代码包,编译用的

Galera 25.3.22   搞mariadb集群用的,单机不需要

mariadb-10.2.11-winx64.msi

mariadb-10.2.11-winx64.zip

mariadb-10.2.11-win32.zip

mariadb-10.2.11-win32.msi   //这几个就不用说了吧,Windows上面用的。

//下面这个包是包含glibc的二进制包

mariadb-10.2.11-linux-glibc_214-x86_64.tar.gz (requires GLIBC_2.14+) 

//各linux发行版二进制通用包,比如centos6

mariadb-10.2.11-linux-x86_64.tar.gz

//支持systemd的二进制包 (本例就是用这个) 比如centos7 systemd

mariadb-10.2.11-linux-systemd-x86_64.tar.gz (for systems with systemd)

//下面这几个是32位linux的包

mariadb-10.2.11-linux-i686.tar.gz

mariadb-10.2.11-linux-systemd-i686.tar.gz (for systems with systemd)

mariadb-10.2.11-linux-glibc_214-i686.tar.gz (requires GLIBC_2.14+)

//下面这两个是rpm包

Debian and Ubuntu Packages

Red Hat, Fedora, and CentOS Packages

Mariadb安装步骤

#创建mysql用户和用户组,并禁止用户登陆

[root@toydns local]# groupadd mysql

[root@toydns local]# useradd -r -g mysql -s /sbin/nologin mysql

#下载mariadb包:

wget https://downloads.mariadb.org/interstitial/mariadb-10.2.11/bintar-linux-systemd-x86_64/mariadb-10.2.11-linux-systemd-x86_64.tar.gz

#解压:

tar xf mariadb-10.2.11-linux-systemd-x86_64.tar.gz

#更改安装目录:注意不要提前建立/usr/local/mysql目录,直接放过去即可

mv mariadb-10.2.11-linux-systemd-x86_64 /usr/local/mysql/

#进入/usr/local/mysql目录

[root@toydns mysql]#cd  /usr/local/mysql

[root@toydns mysql]# ll

total 204

drwxr-xr-x.  2 1021 1004  4096 Oct 26 14:04 bin

-rw-r--r--.  1 1021 1004 17987 Nov 27 18:09 COPYING

-rw-r--r--.  1 1021 1004 86263 Nov 27 18:09 COPYING.thirdparty

-rw-r--r--.  1 1021 1004  2275 Nov 27 18:09 CREDITS

drwxrwxr-x.  3 1021 1004  4096 Nov 28 06:16 data

drwxr-xr-x.  2 root root  4096 Dec  4 23:06 etc

-rw-r--r--.  1 1021 1004  8245 Nov 27 18:09 EXCEPTIONS-CLIENT

drwxrwxr-x.  3 1021 1004  4096 Nov 28 06:15 include

-rw-r--r--.  1 1021 1004  8694 Nov 27 18:09 INSTALL-BINARY

drwxr-xr-x.  4 1021 1004  4096 Nov 14 22:35 lib

drwxrwxr-x.  4 1021 1004  4096 Nov 28 06:16 man

drwxrwxr-x. 11 1021 1004  4096 Nov 28 06:15 mysql-test

-rw-r--r--.  1 1021 1004  2374 Nov 27 18:09 README.md

-rw-r--r--.  1 1021 1004 19510 Nov 27 18:09 README-wsrep

drwxrwxr-x.  2 1021 1004  4096 Nov 28 06:15 scripts

drwxrwxr-x. 32 1021 1004  4096 Nov 28 06:15 share

drwxrwxr-x.  4 1021 1004  4096 Nov 28 06:15 sql-bench

drwxrwxr-x.  4 1021 1004  4096 Nov 28 06:15 support-files

相关目录介绍:

bin: 可执行的二进制程序的存放目录,客户端程序mysql就位于这个目录下。

COPYING:版权以及开源信息

COPYING.thirdparty: 版权信息

CREDITS:关于MariaDB软件基金会的一些相关信息,里面还有国内的互联网巨头阿里

data:默认的数据库存放目录,如果我们一开始没有指定数据库存放目录的话,那就会被存储到这个位置。

EXCEPTIONS-CLIENT:例外情况

include:MariaDB 所需要的一些程序文件

INSTALL-BINARY: 安装帮助文档,可以详细阅读,对安装数据库有很大的帮助

lib: 软件运行所需要的库文件

man:软件的帮助文档

mysql-test: 数据库的测试组件

scipts:mysql初始化初始化时要用到的脚本文件,通读一下脚本,可以了解Mysql 的安装过程

share: 共享的文件内容

support-files: mysql 正常运行所需要的配置文件或者文档,这一点很重要,如果我们要自定义配置文件的话,就需要参考这里面的配置文件来进行定义。

这里有一点需要注意:data目录是数据库的存放路径,我们在之前已经手动指定。在实际生产中,企业数据增长很快,数据库文件有可能会很大,因此最好将该目录指定到一个单独的磁盘上,或者大分区,或者使用逻辑卷都可以,避免因物理空间不足,导致出现故障。

#创建mysql配置文件目录

mkdir  /usr/local/mysql/etc

#在/usr/local/mysql/support-files/下的配置文件模板,已经配置好的部分参数,分别用于不同的环境,这里说明一下:

my-small.cnf 这个是为小型数据库或者个人测试使用的,不能用于生产环境

my-medium.cnf 这个适用于中等规模的数据库,比如个人项目或者小型企业项目中,

my-large.cnf 一般用于专门提供SQL服务的服务器中,即专门运行数据库服务的主机,配置要求要更高一些,适用于生产环境

my-huge.cnf 用于企业级服务器中的数据库服务,一般更多用于生产环境使用

所以根据以上几个文件,如果个人使用或者测试,那么可以使用前两个模板;企业服务器或者64G以上的高配置服务器可以使用后面两个模板,另外也可以根据自己的需求来加大参数和扩充配置获得更好的性能。

[root@toydns mysql]# ll support-files/

-rw-r--r--. 1 1021 1004  4914 Nov 27 18:32 my-huge.cnf

-rw-r--r--. 1 1021 1004 20421 Nov 27 18:32 my-innodb-heavy-4G.cnf

-rw-r--r--. 1 1021 1004  4901 Nov 27 18:32 my-large.cnf

-rw-r--r--. 1 1021 1004  4914 Nov 27 18:32 my-medium.cnf

-rw-r--r--. 1 1021 1004  2840 Nov 27 18:32 my-small.cnf

#复制my-medium.cnf到etc下并改名为my.cnf,修改数据库文件存放的目录

cp support-files/my-medium.cnf  etc/my.cnf

vi etc/my.cnf

# The MariaDB server

[mysqld]

port            = 3306  

socket          = /tmp/mysql.sock

basedir = /usr/local/mysql   //mysql目录

datadir= /usr/local/mysql/data  //数据存放位置

innodb_file_per_table = on  //每张表一个单独文件,便于管理

skip_name_resolve = on  //忽略反向解析,加快访问速度

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

:wq保存后给权限

[root@toydns local]# chown -R mysql.mysql /usr/local/mysql/

[root@toydns local]# chmod -R 755 /usr/local/mysql/

#添加MySQL到PATH环境变量里面去,省的以后敲命令麻烦

[root@toydns mysql]# vi /etc/profile.d/mysql.sh

export  PATH=/usr/local/mysql/bin:$PATH

source /etc/profile.d/mysql.sh  使这个配置文件及时生效

#初始化mysql

/usr/local/mysql/scripts/mysql_install_db --user=mysql

#创建mariadb自启动脚本,并加入开机启动

cp /usr/local/mysql/support-files/systemd/mariadb.service  /usr/lib/systemd/system/

systemctl enable mariadb

systemctl start mariadb

systemctl stop mariadb

MySQL的安全设置

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

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB

      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current

password for the root user.  If you've just installed MariaDB, 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):   //直接回车,默认为空

OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB

root user without the proper authorisation.

Set root password? [Y/n] y     //是否设置MySQL管理员root的密码,y设置,输入2次

New password:

Re-enter new password:

Password updated successfully!

Reloading privilege tables..

... Success!

By default, a MariaDB installation has an anonymous user, allowing anyone

to log into MariaDB 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    //是否删除匿名账户 y删除

... Success!

Normally, root should only ba

By default, MariaDB 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   //是否删除test测试数据库,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   //重新加载可用的数据库表  y 是

... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB

installation should now be secure.

Thanks for using MariaDB!

猜你喜欢

转载自blog.csdn.net/eagle89/article/details/83105408