Linux下MySQL5.7.18二进制包安装教程(无默认配置文件my_default.cnf)_Mysql_脚本之家

最新在学习MySQL,纯新手,对Linux了解的也不多,因为是下载的最新版的MySQL二进制包,CentOS7.2下测试安装,方便以后折腾。

mariadb是一个开源数的据库管理软件,是MySQL的一个分支,是MySQL创始人在出售MySQL之后开发的一个新的数据库软件,在MySQL的基础上进行了新的优化。

大概步骤如下,安装删除反复折腾了几遍,按照以下步骤,应该没啥问题了。

准备工作

也没有想象中的复杂,大部分步骤都是参考网上的,照做就是了,出错的话,多尝试,多查资料。

安装mariadb的方式有多种,在这里介绍一下mariadb的二进制安装方式,首先要现在mariadb的二进制包,推荐官网下载https://downloads.mariadb.org/mariadb/10.2.8/,在这里下载最新的10.2.8稳定版

操作系统版本

图片 1

创建mysql组和用户

把已经下载好的二进制包mariadb-10.2.8-linux-x86_64.tar上传至虚拟机内

下载最新版的MySQL 5.7.18二进制包,上传至服务器的usr/local目录下

[root@c7 ~]#rz

[root@c7 ~]#ll
total 445180
-rw-r--r--  1 root root 455856309 Sep 21 09:43 mariadb-10.2.8-linux-x86_64.tar.gz

解压MySQL二进制包

准备mysql用户和家目录

如下是解压之后的,并且重命名文件夹之后的文件信息

[root@c7 ~]#getent passwd mysql --------------------------------------- # 查看mysql用户是否存在
[root@c7 ~]#useradd -r -m -d /app/dbdata -s /sbin/nologin mysql ------- # 创建mysql用户,指定家目录和用户类型
[root@c7 ~]#getent passwd mysql --------------------------------------- # 确认创建mysql用户
mysql:x:989:984::/app/dbdata:/sbin/nologin
[root@c7 ~]#ll /app/ -------------------------------------------------- # 确认创建mysql用户家目录
drwx------  3 mysql mysql        78 Sep 25 14:46 dbdata

更改所属的组和用户

解压二进制包并安装

解压之后的MySQL文件包含的内容

Linux下MySQL5.7.18二进制包安装教程(无默认配置文件my_default.cnf)_Mysql_脚本之家。二进制包是编译过后的程序,不是源码,在编译过程中已经指定过安装的路径,必须安装编译的路径去解压存放。mariadb默认安装在/usr
/local/mysql/文件夹下,在解压时直接指定解压在/usr/local/文件夹下

查看support-files文件夹的内容,发现并没有my_default.cnf默认的配置文件,如果没有默认的配置文件,需要手动创建一个my_default.cnf配置文件

[root@c7 ~]#tar xvf mariadb-10.2.8-linux-x86_64.tar.gz -C /usr/local/
[root@c7 ~]#ll /usr/local/
total 0
drwxr-xr-x.  2 root root   6 Nov  5  2016 bin
drwxr-xr-x.  2 root root   6 Nov  5  2016 etc
drwxr-xr-x.  2 root root   6 Nov  5  2016 games
drwxr-xr-x.  2 root root   6 Nov  5  2016 include
drwxr-xr-x.  2 root root   6 Nov  5  2016 lib
drwxr-xr-x.  2 root root   6 Nov  5  2016 lib64
drwxr-xr-x.  2 root root   6 Nov  5  2016 libexec
drwxrwxr-x  12 1021 1004 290 Aug 18 04:16 mariadb-10.2.8-linux-x86_64
drwxr-xr-x.  2 root root   6 Nov  5  2016 sbin
drwxr-xr-x.  5 root root  49 Sep  9 20:56 share
drwxr-xr-x.  2 root root   6 Nov  5  2016 src

从网上找了一个配置文件,如下,上传到supp-files文件夹中,当然这个配置文件可以根据需要自行修改

由此可见,因为解压后的文件夹名称和报名相同,而不是mariadb安装的文件夹mysql,在这里可以把文件夹名称改为mysql或者创建一个软连接

# For advice on how to change settings please see# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the# *** default location during install, and will be replaced if you# *** upgrade to a newer version of MySQL.[mysqld]sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES # 一般配置选项basedir = /usr/local/mysqldatadir = /usr/local/mysql/dataport = 3306socket = /var/run/mysqld/mysqld.sockcharacter-set-server=utf8back_log = 300max_connections = 3000max_connect_errors = 50table_open_cache = 4096max_allowed_packet = 32M#binlog_cache_size = 4Mmax_heap_table_size = 128Mread_rnd_buffer_size = 16Msort_buffer_size = 16Mjoin_buffer_size = 16Mthread_cache_size = 16query_cache_size = 128Mquery_cache_limit = 4Mft_min_word_len = 8thread_stack = 512Ktransaction_isolation = REPEATABLE-READtmp_table_size = 128M#log-bin=mysql-binlong_query_time = 6server_id=1innodb_buffer_pool_size = 1Ginnodb_thread_concurrency = 16innodb_log_buffer_size = 16Minnodb_log_file_size = 512Minnodb_log_files_in_group = 3innodb_max_dirty_pages_pct = 90innodb_lock_wait_timeout = 120innodb_file_per_table = on[mysqldump]quickmax_allowed_packet = 32M[mysql]no-auto-rehashdefault-character-set=utf8safe-updates[myisamchk]key_buffer = 16Msort_buffer_size = 16Mread_buffer = 8Mwrite_buffer = 8M[mysqlhotcopy]interactive-timeout[mysqld_safe]open-files-limit = 8192[client]/bin/bash: Q: command not found
[root@c7 local]#ln -sv mariadb-10.2.8-linux-x86_64/ mysql
‘mysql’ -> ‘mariadb-10.2.8-linux-x86_64/’
[root@c7 local]#ll
total 0
drwxr-xr-x.  2 root root   6 Nov  5  2016 bin
drwxr-xr-x.  2 root root   6 Nov  5  2016 etc
drwxr-xr-x.  2 root root   6 Nov  5  2016 games
drwxr-xr-x.  2 root root   6 Nov  5  2016 include
drwxr-xr-x.  2 root root   6 Nov  5  2016 lib
drwxr-xr-x.  2 root root   6 Nov  5  2016 lib64
drwxr-xr-x.  2 root root   6 Nov  5  2016 libexec
drwxrwxr-x  12 1021 1004 290 Aug 18 04:16 mariadb-10.2.8-linux-x86_64
lrwxrwxrwx   1 root root  28 Sep 25 15:04 mysql -> mariadb-10.2.8-linux-x86_64/
drwxr-xr-x.  2 root root   6 Nov  5  2016 sbin
drwxr-xr-x.  5 root root  49 Sep  9 20:56 share
drwxr-xr-x.  2 root root   6 Nov  5  2016 src

如下,将上述默认配置文件上传至support-files文件夹中

准备配置文件

注册和初始化MySQL服务

配置格式:类ini格式,各程序由单个配置文件提供配[prog_name]
配置文件查找次序:后面覆盖前面的配置文件
/etc/my.cnf–> /etc/mysql/my.cnf–>
–default-extra-file=/PATH/TO/CONF_FILE –> ~/.my.cnf

这一点比较坑,网上的教程大多是mysql_install_db
方式安装的,这一步也是遇到问题最最多的,折腾了好久,但是mysql_install_db
命令是不推荐的,也提示使用mysqld来安装

虽然现在还没安装配置文件,但/etc/my.cnf已经生产,在这里我们可以创建一个优先级更高的的/etc/mysql/my,cnf文件。在整理可以用support-files/my-huge.cnf为模板,复制并改名为my.cnf

在安装mysql
5.7.18的时候也是各种报错,经过各种尝试之后吧,采用如下命名方式安装,只要没有Error,警告信息Warning先不理它

[root@c7 mysql]#mkdir /etc/mysql
[root@c7 mysql]#ll /etc/mysql/
total 0
[root@c7 mysql]#cp support-files/my-huge.cnf /etc/mysql/my.cnf
[root@c7 mysql]#ll /etc/mysql/my.cnf 
-rw-r--r-- 1 root root 4901 Sep 25 15:36 /etc/mysql/my.cnf
bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ 

修改配置文件,添加必要的修改信息

复制上述的my_default.cnf和mysql.server配置文件到etc目录,之前复制过该文件到etc目录,它这里提示是否覆盖,输入Y覆盖

[root@c7 mysql]#vim /etc/mysql/my.cnf
......
# The MySQL server
[mysqld]
port            = 3306
datadir = /app/dbdata -------------- # 数据库目录
innodb_file_per_table = on --------- # 生成独立的数据库文件
skip_name_resolve = on ------------- # 禁止主机名解析

启动MySQL服务

创建日志文件

连接至MySQL,默认没有密码,直接回车后连接至MySQL

在mariadb的配置文件中,默认日志文件存在于/var/log/mariadb/maria.log,这个文件需要手动去创建,并且修改用户为mysql

修改MySQL的root密码

[root@c7 mysql]#mkdir /var/log/mariadb
[root@c7 mysql]#touch /var/log/mariadb/maria.log
[root@c7 mysql]#chown -R mysql:mysql /var/log/mariadb/
[root@c7 mysql]#ll /var/log/mariadb/
total 0
-rw-r--r-- 1 mysql mysql 0 Sep 25 17:19 maria.log
[root@c7 mysql]#ll /var/log/mariadb/ -d
drwxr-xr-x 2 mysql mysql 23 Sep 25 17:19 /var/log/mariadb/

修改可远程访问

创建系统数据库

重启MySQL服务

在系统中存在mysql数据库和text数据库,mysql数据库是系统数据库,在/app/dbdata/文件夹中还没有创建

此时本地可以正常使用MySQL

[root@c7 mysql]#ll /app/dbdata/
total 0

测试远程连接

执行脚本文件scripts/mysql_install_db创建系统数据库,这个脚本只能在/usr/local/mysql/下执行。否则会报错。在执行此脚本时需指定数据库目录和运行者身份。

[root@c7 mysql]#scripts/mysql_install_db --datadir=/app/dbdata --user=mysql
......
[root@c7 mysql]#ls /app/dbdata/
aria_log.00000001  ibdata1      mysql             mysql-bin.state
aria_log_control   ib_logfile0  mysql-bin.000001  performance_schema
ib_buffer_pool     ib_logfile1  mysql-bin.index   test

此时系统数据库mysql和text已经创建成功,一个文件夹代表一个数据库

启动数据库

复制文件夹support-files下的启动文件mysql.server到
/etc/init.d/并改名mysqld,启动mysqld,并查看启动后的进程

[root@c7 mysql]#cp support-files/mysql.server /etc/init.d/mysqld
[root@c7 mysql]#chkconfig mysqld on
[root@c7 mysql]#chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

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

[root@c7 mysql]#service mysqld start
Starting mysqld (via systemctl):                           [  OK  ]

配置环境变量

此时mysqld对应的端口3306已经打开,但是使用mysql依然无法进入数据库,再次需要配置环境变量

[root@c7 mysql]#ss -ntl
State       Recv-Q Send-Q Local Address:Port                Peer Address:Port              
LISTEN      0      128                *:111                            *:*                  
LISTEN      0      5      192.168.122.1:53                             *:*                  
LISTEN      0      128                *:22                             *:*                  
LISTEN      0      128        127.0.0.1:631                            *:*                  
LISTEN      0      100        127.0.0.1:25                             *:*                  
LISTEN      0      80                :::3306                          :::*                  
LISTEN      0      128               :::111                           :::*                  
LISTEN      0      128               :::80                            :::*                  
LISTEN      0      128               :::22                            :::*                  
LISTEN      0      128              ::1:631                           :::*                  
LISTEN      0      100              ::1:25                            :::*                  
[root@c7 mysql]#mysql
bash: mysql: command not found...

在etc下编辑变量配置文件,执行新的环境变量文件,重新登录mysql

[root@c7 bin]#vim /etc/profile.d/mysql.sh 
export PATH=/usr/local/mysql/bin:$PATH

[root@c7 bin]#. /etc/profile.d/mysql.sh 
[root@c7 bin]#mysql
Welcome to the MariaDB monitor.  Commands end with ; or g.
Your MariaDB connection id is 10
Server version: 10.2.8-MariaDB-log MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

MariaDB [(none)]>

初始化数据库

刚建立的数据库没有进行任何的加密,用户可以随意登录,起不到保护数据的作用,因此需要对数据库进行初始化设置

[root@c7 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 ---------------------------------------------------- # 设置密码
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 ---------------------------------------------- # 删除匿名用户
 ... 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 ---------------------------------------- # 不允许root远程登录
 ... skipping.

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] n ------------------------------- # 不删除test测试数据库
 ... skipping.

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

Reload privilege tables now? [Y/n] y ---------------------------------------- # 重新加载数据库
 ... Success!

Cleaning up...

此时数据库已经不能使用mysql命令直接连接,如要指定用户,并输入密码才能连接

[root@c7 ~]#mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@c7 ~]#mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or g.
Your MariaDB connection id is 25
Server version: 10.2.8-MariaDB-log MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

MariaDB [(none)]> 
网站地图xml地图