Linux上MySQL搭建

安装包

        官网下载,或者这里

Linux OS版本

        CentOS-6.8-x86_64,下载地址

------------------

虚拟机IP:192.168.160.130

-------------------

1:下载

        下载安装包,mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz。

         并把安装包放到Linux的/usr/local/software目录。

         PS、这个目录纯属个人习惯。

2:解压

 cd /usr/local/software/
 tar -zxvf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz

3:修改mysql文件夹名字

 mv mysql-5.7.20-linux-glibc2.12-x86_64 mysql

4:检查库文件是否有删除,若有便删除(linux系统自带的)

[root@localhost software]# rpm -qa | grep mysql            注释:检查文件
mysql-libs-5.1.73-7.el6.x86_64
[root@localhost software]# rpm -e --nodeps mysql-libs-5.1.73-7.el6.x86_64         注释:删除文件
[root@localhost software]# rpm -qa | grep mysql            注释:再次检查
[root@localhost software]# 
5:检查mysql组和用户是否存在,如无创建

        检查:

[root@localhost software]# cat /etc/group | grep mysql
[root@localhost software]# cat /etc/passwd |grep mysql
[root@localhost software]#

        创建:

[root@localhost software]# groupadd mysql
[root@localhost software]# useradd -r -g mysql mysql
[root@localhost software]#

        再次检查

[root@localhost software]# cat /etc/group | grep mysql
mysql:x:502:
[root@localhost software]# cat /etc/passwd |grep mysql
mysql:x:496:502::/home/mysql:/bin/bash
[root@localhost software]#

6:在mysql下添加data目录

[root@localhost software]# cd /usr/local/software/mysql
[root@localhost mysql]# mkdir data
[root@localhost mysql]# ll
total 56
drwxr-xr-x.  2 root root   4096 Jun 25 19:43 bin
-rw-r--r--.  1 7161 31415 17987 Sep 13  2017 COPYING
drwxr-xr-x.  2 root root   4096 Jun 25 19:57 data
drwxr-xr-x.  2 root root   4096 Jun 25 19:43 docs
drwxr-xr-x.  3 root root   4096 Jun 25 19:42 include
drwxr-xr-x.  5 root root   4096 Jun 25 19:43 lib
drwxr-xr-x.  4 root root   4096 Jun 25 19:43 man
-rw-r--r--.  1 7161 31415  2478 Sep 13  2017 README
drwxr-xr-x. 28 root root   4096 Jun 25 19:43 share
drwxr-xr-x.  2 root root   4096 Jun 25 19:43 support-files
[root@localhost mysql]# 

7:更改mysql目录下所有的目录及文件夹所属组合用户

[root@localhost mysql]# cd /usr/local/software/
[root@localhost software]# chown -R mysql mysql/
[root@localhost software]# chgrp -R mysql mysql/
[root@localhost software]# cd mysql
[root@localhost mysql]# ls -l
total 56
drwxr-xr-x.  2 mysql mysql  4096 Jun 25 19:43 bin
-rw-r--r--.  1 mysql mysql 17987 Sep 13  2017 COPYING
drwxr-xr-x.  2 mysql mysql  4096 Jun 25 19:57 data
drwxr-xr-x.  2 mysql mysql  4096 Jun 25 19:43 docs
drwxr-xr-x.  3 mysql mysql  4096 Jun 25 19:42 include
drwxr-xr-x.  5 mysql mysql  4096 Jun 25 19:43 lib
drwxr-xr-x.  4 mysql mysql  4096 Jun 25 19:43 man
-rw-r--r--.  1 mysql mysql  2478 Sep 13  2017 README
drwxr-xr-x. 28 mysql mysql  4096 Jun 25 19:43 share
drwxr-xr-x.  2 mysql mysql  4096 Jun 25 19:43 support-files
[root@localhost mysql]# 

8:修改mysql.server文件

        mysql.server文件位置:/usr/local/software/mysql/support-files

        修改相关配置

# If you change base dir, you must also change datadir. These may get
# overwritten by settings in the MySQL configuration files.

basedir=/usr/local/software/mysql
datadir=/usr/local/software/mysql/data

PS、这个地方。回顾头来想想为什么要改名为什么要建data文件夹,以及我安装位置。

        如果不修改,则默认的位置为/usr/local/mysql和/usr/local/mysql/data

9:安装和初始化数据库

[root@localhost mysql]# cd /usr/local/software/mysql/bin/
[root@localhost bin]# ./mysqld  --initialize --user=mysql --basedir=/usr/local/software/mysql/ --datadir=/usr/local/software/mysql/data/
2018-06-26T03:20:45.288071Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-06-26T03:20:45.714481Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-06-26T03:20:45.921467Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-06-26T03:20:46.034842Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: ea6d4b60-78ef-11e8-8c12-000c295f07f7.
2018-06-26T03:20:46.036718Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-06-26T03:20:46.037855Z 1 [Note] A temporary password is generated for root@localhost: sfPqTk1sae,c
[root@localhost bin]#
PS、这一步可以不在第8步之后。


10配置my.cnf

接下来进入/usr/local/software/mysql/support-files/目录下
查看是否存在my-default.cnf文件,如果存在直接copy到/etc/my.cnf文件中

cp /usr/local/software/mysql/support-files/my-medium.cnf /etc/my.cnf

如果不存在my-default.cnf文件,则在/etc/目录下创建my.cnf,并写入以下内容

[mysql]
basedir=/usr/local/software/mysql/
datadir=/usr/local/software/mysql/data/
11:启动服务?
[root@localhost bin]# pwd
/usr/local/software/mysql/bin
[root@localhost bin]# ./mysqld_safe --user=mysql &
[1] 5149
[root@localhost bin]# Logging to '/usr/local/software/mysql/data/localhost.localdomain.err'.
2018-06-26T03:34:20.900325Z mysqld_safe Starting mysqld daemon with databases from /usr/local/software/mysql/data

[root@localhost bin]# 
12:将mysqld服务加入开机自启动项。

        将{mysql}/ support-files/mysql.server 拷贝为/etc/init.d/mysql并设置运行权限,这样就可以使用service mysql命令启动/停止服务, 否则就只能使用{mysql}/bin/mysqld_safe &命令来启动服务 。
        还需要把mysql.server中basedir的相关路径,改为自定义的路径,默认路径是/usr/local/mysql。(第八步)

[root@localhost bin]# cd /usr/local/software/mysql/support-files/
[root@localhost support-files]# cp mysql.server /etc/init.d/mysql
[root@localhost support-files]#  chmod +x /etc/init.d/mysql
[root@localhost support-files]# chkconfig --add mysql
[root@localhost support-files]# chkconfig --list mysql
mysql          	0:off	1:off	2:on	3:on	4:on	5:on	6:off
[root@localhost support-files]#
13:启动服务
[root@localhost support-files]# cd /usr/local/software/mysql/bin/
[root@localhost bin]# service mysql start
Starting MySQL SUCCESS! 
[root@localhost bin]# 2018-06-26T03:41:44.745534Z mysqld_safe A mysqld process already exists

[root@localhost bin]#
14:登录mysql
[root@localhost bin]# pwd
/usr/local/software/mysql/bin
[root@localhost bin]# ./mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.20

Copyright (c) 2000, 2017, 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>
15:设置密码
mysql>  set password=password("root");
Query OK, 0 rows affected, 1 warning (0.00 sec)
注意不要使用单引号,为什么?你自己试试就知道了

16:  设置远程登录权限

mysql> grant all privileges on *.* to'root' @'%' identified by 'root';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql>  flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql>  quit
Bye
[root@localhost bin]#

参考资料:

        https://blog.csdn.net/zhou920786312/article/details/77750604


猜你喜欢

转载自blog.csdn.net/lz70523/article/details/80810597