linux下安装MySQL及遇到的坑

1、安装MySQL需要依赖包和编译软件

 

 

1、安装MySQL需要依赖包和编译软件

查询当前操作系统版本:

[root@ray /]# cat /etc/redhat-release

CentOS Linux release 7.6.1810 (Core) 

查询当前内核版本

[root@ray /]# uname -r

3.10.0-957.21.3.el7.x86_64

查询当前系统位数

[root@ray /]# uname -m

x86_64

 

1.1 安装MySQL需要的依赖包

[root@ray /]# yum install ncurses-devel libaio-devel -y  #下载

[root@ray /]# rpm -qa ncurses-devel libaio-devel #安装

1.2 安装编译MySQL需要的软件

[root@ray /]# yum -y install cmake #下载

[root@ray /]# rpm -qa cmake    #安装

1.3 建立MySQL用户账户

[root@ray /]# useradd -s /sbin/nologin -M mysql #默认创建和mysql同名的用户组

[root@ray /]# id mysql  #查询mysql的用户ID、组ID

uid=1003(mysql) gid=1003(mysql) groups=1003(mysql)

 

 

 

 

2、采用编译安装MySQL

 

 

[root@ray /]# mkdir -p /home/oldboy/tools #习惯性安装目录

[root@ray /]# cd /home/oldboy/tools/ #切换到安装目录

[root@ray tools]# rz  #用rz命令将MySQL官网下载的mysql-5.6.44.tar.gz上传到该目录下(推荐使用源码安装包、二进制包过大不利于上传下载)

[root@ray tools]# ls -lh #查询是否上传成功



 

2.1 解压并配置MySQL

[root@ray tools]# tar xf mysql-5.6.44.tar.gz #解压

[root@ray tools]# ls -lh #查询是否解压成功

total 345M

drwxr-xr-x 34 7161 31415 4.0K Jul 14 14:18 mysql-5.6.44

-r--------  1 root root  314M Jul 14 13:38 mysql-5.6.44-linux-glibc2.12-x86_64.tar.gz

-r--------  1 root root   32M Jul 14 14:13 mysql-5.6.44.tar.gz

[root@ray tools]# cd mysql-5.6.44 #进入解压后的目录

[root@ray mysql-5.6.44]# 

cmake . -DCMAKE_INSTALL_PREFIX=/application/mysql-5.6.44 \

-DMYSQL_DATADIR=/application/mysql-5.6.44/data \

-DMYSQL_UNIX_ADDR=/application/mysql-5.6.44/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

###复制上面的内容并执行###

 

 

 

 

2.2 编译并安装MySQL

[root@ray mysql-5.6.44]# make

[root@ray mysql-5.6.44]# echo $? #校验是否make成功,若返回0则成功

0

[root@ray mysql-5.6.44]# make install #这里不建议将make && make install合起来使用,若出现问题则比较难排查

[root@ray mysql-5.6.44]# echo $?

0

 

 

 

 

2.3 为MySQL安装路径设置不带版本号的软连接/application/mysql

[root@ray mysql-5.6.44]# ln -s /application/mysql-5.6.44/ /application/mysql #这里如果有安装过数据文件和启动程序,最好停掉或删除,以免发生冲突

[root@ray mysql-5.6.44]# ls -l /application/ #查询软连接是否设置成功

total 4

lrwxrwxrwx  1 root root   26 Jul 14 14:59 mysql -> /application/mysql-5.6.44/

drwxr-xr-x 13 root root 4096 Jul 14 14:58 mysql-5.6.44

[root@ray mysql-5.6.44]# ls /application/mysql #查询MySQL的安装路径

bin      data  include  man         README   share      support-files

COPYING  docs  lib      mysql-test  scripts  sql-bench

 

 


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

 

 

[root@ray mysql-5.6.44]# ll support-files/*.cnf

-rw-r--r-- 1 root root 1126 Jul 14 14:18 support-files/my-default.cnf

[root@ray mysql-5.6.44]# mv /etc/my.cnf.rpmsave /etc/my.cnf.rpmsave.bak #在启动MySQL服务时,会按照一定的顺序搜索my.cnf,先在/etc目录下找,若找不到则搜索"$basedir/my.cnf"

[root@ray mysql-5.6.44]# cp support-files/my-default.cnf /etc/my.cnf #生成新的my.cnf文件

[root@ray mysql-5.6.44]# chown -R mysql.mysql /application/mysql #授权MySQL用户管理MySQL的安装目录,此步骤不做会导致启动服务报错

 

3.1初始化MySQL数据库文件

[root@ray mysql-5.6.44]# /application/mysql/scripts/mysql_install_db --basedir=/application/mysql/ --datadir=/application/mysql/data --user=mysql #初始化MySQL核心命令

2019-07-14 15:16:09 11331 [Note] InnoDB: Shutdown completed; log sequence number 1625977

OK   #出现两个OK是初始化成功的标志

...省略若干行

2019-07-14 15:16:11 11440 [Note] InnoDB: Shutdown completed; log sequence number 1625987

OK   #出现两个OK是初始化成功的标志

...省略若干行

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,因此有可能会导致无法启动,避免方法就是使用mysqld_safe,启动服务时采用“--defaults-file”参数指定配置文件,前面已经将/etc/my.cnf改名了并进行了文件替换,所以不需要指定参数了

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

 

 

 

 

3.2初始化后查看数据目录

[root@ray mysql-5.6.44]# ls -l /application/mysql/data/

total 110604

-rw-rw---- 1 mysql mysql 12582912 Jul 14 15:16 ibdata1

-rw-rw---- 1 mysql mysql 50331648 Jul 14 15:16 ib_logfile0

-rw-rw---- 1 mysql mysql 50331648 Jul 14 15:16 ib_logfile1

drwx------ 2 mysql mysql     4096 Jul 14 15:16 mysql                #用于存放管理MySQL的数据

drwx------ 2 mysql mysql     4096 Jul 14 15:16 performance_schema   #增加内部性能库

drwxr-xr-x 2 mysql mysql     4096 Jul 14 14:57 test                 #用于测试的test库

[root@ray mysql-5.6.44]# tree /application/mysql/data/

/application/mysql/data/

├── ibdata1

├── ib_logfile0

├── ib_logfile1

├── mysql

│   ├── columns_priv.frm

│   ├── columns_priv.MYD

│   ├── columns_priv.MYI

│   ├── db.frm

│   ├── db.MYD

...省略

 

 

 


4、配置并启动MySQL数据库

 

 

4.1设置MySQL启动脚本

[root@ray mysql-5.6.44]# pwd

/home/oldboy/tools/mysql-5.6.44

[root@ray mysql-5.6.44]# cp support-files/mysql.server /etc/init.d/mysqld #将MySQL启动脚本复制到MySQL的命令路径

[root@ray mysql-5.6.44]# chmod 700 /etc/init.d/mysqld #使脚本可执行,注意权限要小一些

[root@ray mysql-5.6.44]# ls -l /etc/init.d/mysqld

-rwx------ 1 root root 10619 Jul 14 15:27 /etc/init.d/mysqld

 

 

 

 

 

4.2启动MySQL数据库

[root@ray mysql-5.6.44]# /etc/init.d/mysqld start #启动数据库

Starting MySQL.Logging to '/application/mysql-5.6.44/data/ray.err'.

190714 16:42:44 mysqld_safe Directory '/application/mysql-5.6.44/tmp' for UNIX socket file don't exists.

 ERROR! The server quit without updating PID file (/application/mysql-5.6.44/data/ray.pid).

[root@ray mysql-5.6.44]# mkdir -p /application/mysql-5.6.44/tmp  #创建编译时指定的socket路径

[root@ray mysql-5.6.44]# chown -R mysql.mysql /application/mysql/ #赋权

[root@ray mysql-5.6.44]# /etc/init.d/mysqld start #启动

Starting MySQL.Logging to '/application/mysql-5.6.44/data/ray.err'.

 SUCCESS! 

[root@ray mysql-5.6.44]# netstat -lntup|grep mysql  #查看是否启动

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

 

  

 

 

4.3查看报错日志

[root@ray mysql-5.6.44]# tail -10f /application/mysql/data/ray.err 

2019-07-14 16:44:03 32058 [Note] InnoDB: Waiting for purge to start

2019-07-14 16:44:03 32058 [Note] InnoDB: 5.6.44 started; log sequence number 1625987

2019-07-14 16:44:03 32058 [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: 8881fdd7-a613-11e9-9dd0-00163e10802e.

2019-07-14 16:44:03 32058 [Note] Server hostname (bind-address): '*'; port: 3306

2019-07-14 16:44:03 32058 [Note] IPv6 is available.

2019-07-14 16:44:03 32058 [Note]   - '::' resolves to '::';

2019-07-14 16:44:03 32058 [Note] Server socket created on IP: '::'.

2019-07-14 16:44:03 32058 [Note] Event Scheduler: Loaded 0 events

2019-07-14 16:44:03 32058 [Note] /application/mysql-5.6.44/bin/mysqld: ready for connections.

Version: '5.6.44'  socket: '/application/mysql-5.6.44/tmp/mysql.sock'  port: 3306  Source distribution

 

 

 

 

4.4设置MySQL开机自启动

[root@ray mysql-5.6.44]# chkconfig --add mysqld

[root@ray mysql-5.6.44]# chkconfig --list mysqld

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

注意:此外,将启动命令/etc/init.d/mysqld start 放到/etc/rc.local里面也可以实现开机自启动

 

 

 

5、将MySQL相关命令加入全局路径

 

 

5.1确认MySQL所在的路径

[root@ray /]# ls /application/mysql/bin/mysql

/application/mysql/bin/mysql

 

 

 

 

5.2在path变量前面增加/application/mysql/bin路径,并追加/etc/profile文件中:

[root@ray /]# echo 'export PATH=/application/mysql/bin:$PATH' >>/etc/profile  #echo命令直接添加到环境变量中

[root@ray /]# tail -1 /etc/profile # 查看最后一行是否添加到位

export PATH=/application/mysql/bin:$PATH

[root@ray /]# source /etc/profile  #source使配置生效

[root@ray /]# echo $PATH # 执行echo $PATH,若有/application/mysql/bin输出则表示配置成功

/application/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

另外一种配置方法:

ln -s /application/mysql/bin/* /usr/local/sbin

把MySQL命令所在的路径链接到全局路径/usr/local/sbin的下面

 

 


6、登陆MySQL测试

 

 

[root@ray /]# mysql

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

Your MySQL connection id is 1

Server version: 5.6.44 Source distribution

Copyright (c) 2000, 2019, 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> 

 

 

 

6.1提示:你还可以使用如下三种写法登陆MySQL(mysql安装完成后,管理员root是无密码的)

mysql -uroot -p 

mysql -uroot

mysql -uroot -p '密码'

 

 

 

 

6.2出现问题:若出现“ERROR 1045(28000):Access denied for user ‘root’@‘localhost’(using password:NO)”

解决方法:重新初始化数据库即可,此问题一般都是数据库初始化问题或者数据库文件损坏及目录权限问题。

 

 

 

 

mysql> show databases;

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

| Database           |

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

| information_schema |

| mysql              |

| performance_schema |

| test               |

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

4 rows in set (0.01 sec)

mysql> select user();

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

| user()         |

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

| root@localhost |

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

1 row in set (0.00 sec)

 

 

 

6.3设置root用户密码

[root@ray /]# mysqladmin -u root password 'oldboy123'

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

[root@ray /]# mysql

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

[root@ray /]# 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 4

Server version: 5.6.44 Source distribution

Copyright (c) 2000, 2019, 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> 

 

 

 

 

 

6.4设置远程连接

新增远程链接%用户

mysql> update mysql.user set host='%' where host='127.0.0.1';  #若没有%的host,修改一个

为远程链接用户设置密码

mysql> update user set password=password('oldboy123') where user='root' and host='%';

使配置立即生效

mysql> flush privileges;

使用远程链接Navicat工具链接,链接成功!

 

posted @ 2019-07-14 21:08  蘇曦  阅读(1348)  评论(0)    收藏  举报