Linux多种方法安装mysql

文章目录

      • 一、前言
      • 二、安装方法一:源码安装/编译安装
      • 三、安装方法二(通用二进制包):mysql-x.x.xx-linux-glibcx.x-xxx.tar.gz
        • 1.Redhat5.5 32/RedHat6.6和6.5 64位/Centos7.2 64位位下安装mysql 5.6.35:
        • 2.Centos7.2安装mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz:
      • 四、安装方法三:yum安装
      • 五、安装方法四:rpm安装

一、前言

  linux安装mysql服务分四种安装方法:

  • 源码安装:优点是安装包比较小,只有十多M,缺点是安装依赖的库多,安装编译时间长,安装步骤复杂容易出错。
  • 使用官方编译好的二进制文件安装:优点是安装速度快,安装步骤简单,缺点是安装包很大,300M左右。
  • yum安装。
  • rpm安装。

二、安装方法一:源码安装/编译安装

  请看我的另一篇文章:mysql源码脚本安装

三、安装方法二(通用二进制包):mysql-x.x.xx-linux-glibcx.x-xxx.tar.gz

1.Redhat5.5 32/RedHat6.6和6.5 64位/Centos7.2 64位位下安装mysql 5.6.35:

(1)进入安装包所在目录:

[root@localhost ~]# cd /usr/local
[root@localhost local]# tar -zxvf mysql-5.6.35-linux-glibc2.5-i686.tar.gz
[root@localhost local]# rm -rf mysql-5.6.35-linux-glibc2.5-i686.tar.gz
[root@localhost local]# mv mysql-5.6.35-linux-glibc2.5-i686/ mysql

(2)添加系统mysql组和mysql用户:

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

(3)修改当前目录拥有者为mysql用户:

[root@localhost local]# cd mysql/
[root@localhost mysql]# chown -R mysql:mysql ./

(4)安装数据库:

[root@localhost mysql]# ./scripts/mysql_install_db --user=mysql

注:RedHat6.6和6.5 64位安装时遇到了这些问题:

[root@localhost mysql]# ./scripts/mysql_install_db --user=mysql
sh: ./bin/my_print_defaults: /lib/ld-linux.so.2: bad ELF interpreter: No such file or directory
FATAL ERROR: Neither host 'h149' nor 'localhost' could be looked up with
./bin/resolveip
Please configure the 'hostname' command to return a correct
hostname.
If you want to solve this at a later stage, restart this script
with the --force option

网上有的说是版本的原因,mysql是32位的,而系统是64位。可是我后来在64位上也安装成功了啊

解决:yum -y install ld-linux.so.2 libaio.so.1 libstdc++.so.6 libncurses.so.5

补充:Linux 下查看系统是32位 还是64 位的方法:

[root@localhost ~]# uname -a (RedHat6.6 64位)
Linux localhost.localdomain 2.6.32-504.el6.x86_64 #1 SMP Tue Sep 16 01:56:35 EDT 2014 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost ~]# uname -a (RedHat5.5 32位)
Linux localhost.localdomain 2.6.18-194.el5 #1 SMP Tue Mar 16 21:52:43 EDT 2010 i686 i686 i386 GNU/Linux

注:Centos7.2 64位这步遇到的问题

[root@localhost mysql]# ./scripts/mysql_install_db --user=mysql
-bash: ./scripts/mysql_install_db: /usr/bin/perl: bad interpreter: No such file or directory

解决:[root@localhost mysql]# yum -y install perl autoconf ld-linux.so.2 libstdc++* libstdc++.so.6 libaio.so.1 libncurses.so.5

(5)修改当前目录拥有者为root用户:

[root@localhost mysql]# chown -R root:root ./

(6)修改当前data目录拥有者为mysql用户:

[root@localhost mysql]# chown -R mysql:mysql data
(这些赋权限的也不知道有啥用,我后来试了一下,即使都没给他们赋权最后mysql也安装成功了,但为了怕以后有什么问题,还是都按步骤来吧)

(7)添加开机启动:

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

(8)启动mysql服务:

[root@localhost mysql]# service mysql start

注:Centos7.2 64位这步遇到的问题

[root@localhost mysql]# service mysql start
Starting MySQL.170810 19:43:05 mysqld_safe error: log-error set to '/var/log/mariadb/mariadb.log', however file don't exists. Create writable for user 'mysql'.
 ERROR! The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid).

解决:修改/etc/my.cnf中的这两行为

[root@localhost mysql]# vi /etc/my.cnf
log-error=/var/log/mysql/mysql.log
pid-file=/var/run/mysql/mysql.pid

执行命令:ps -ef|grep mysql 看到mysql服务说明启动成功。

(9)修改mysql的root用户密码,root初始密码为空:

[root@localhost mysql]# ./bin/mysqladmin -u root password '123456'
Warning: Using a password on the command line interface can be insecure.
(翻译过来是:在命令行界面上使用密码是不安全的)

注:RedHat6.6和6.5 64位到这步的时候报这个错:

./bin/mysqladmin: connect to server at 'localhost' failed
error: 'Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)'
Check that mysqld is running and that the socket: '/tmp/mysql.sock' exists!

注意:./bin/mysqladmin -u root password '123456'//这个步骤可忽略,如果执行了这一步,等登录时mysql -u root -p就得输入该密码,如果没执行制一步登录的时候回车即可。

(10)把mysql客户端放到默认路径:

[root@localhost mysql]# ln -s /usr/local/mysql/bin/mysql /usr/local/bin/mysql

注意:建议使用软链过去,不要直接包文件复制,便于系统安装多个版本的mysql。

(11)进入命令行操作:

[root@localhost mysql]# mysql -u root -p(如果没有执行./bin/mysqladmin -u root password '123456'则默认密码为空,直接回车即可)
Enter password: 123456
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.6.35    |
+-----------+
1 row in set (0.00 sec)
如果你想修改root密码的话可执行下面的语句:
mysql> set password for root@localhost = password('bigdata');
Query OK, 0 rows affected (0.06 sec)
2.Centos7.2安装mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz:

下载安装包地址:https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
Linux多种方法安装mysql_第1张图片
(1)进入安装包所在目录:

[root@localhost ~]# cd /usr/local
[root@localhost local]# tar -zxvf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
[root@localhost local]# rm -rf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
[root@localhost local]# mv mysql-5.7.17-linux-glibc2.5-x86_64/ mysql或者ln -s mysql-5.7.17-linux-glibc2.5-x86_64 mysql

(2)创建用户和组:

groupadd mysql
useradd -g mysql -s /sbin/nologin mysql

(3)配置PATH:

echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile
source /etc/profile

(4)数据库目录规划:

文件类型 实例3306 软链
数据datadir /usr/local/mysql/data /data/mysql/data
参数文件my.cnf /usr/local/mysql/etc/my.cnf
错误日志log-error /usr/local/mysql/log/mysql_error.log
二进制日志log-bin /usr/local/mysql/binlogs/mysql-bin /data/mysql/binlogs/mysql-bin
慢查询日志slow_query_log_file /usr/local/mysql/log/mysql_slow_query.log
套接字socket文件 /usr/local/mysql/run/mysql.sock
pid文件 /usr/local/mysql/run/mysql.pid
备注:考虑到数据和二进制日志比较大,需要软链:
mkdir -p /data/mysql/{data,binlogs,log,etc,run}
ln -s /data/mysql/data    /usr/local/mysql/data
ln -s /data/mysql/binlogs    /usr/local/mysql/binlogs
ln -s /data/mysql/log    /usr/local/mysql/log
ln -s /data/mysql/etc    /usr/local/mysql/etc
ln -s /data/mysql/run    /usr/local/mysql/run
chown -R mysql.mysql     /data/mysql/
chown -R mysql.mysql     /usr/local/mysql/{data,binlogs,log,etc,run}

也可以只对数据目录和二进制日志目录软链:

mkdir -p /usr/local/mysql/{log,etc,run}
mkdir -p /data/mysql/{data,binlogs}
ln -s /data/mysql/data  /usr/local/mysql/data
ln -s /data/mysql/binlogs   /usr/local/mysql/binlogs
chown -R mysql.mysql /usr/local/mysql/{data,binlogs,log,etc,run}
chown -R mysql.mysql /data/mysql

(5)配置my.cnf参数文件:
删除系统自带的my.cnf:rm -f /etc/my.cnf

在/usr/local/mysql/etc/下创建my.cnf文件,加入如下参数,其他参数根据需要配置:

[client]
port = 3306
socket = /usr/local/mysql/run/mysql.sock

[mysqld]
port = 3306
socket = /usr/local/mysql/run/mysql.sock
pid_file = /usr/local/mysql/run/mysql.pid
datadir = /usr/local/mysql/data
default_storage_engine = InnoDB
max_allowed_packet = 512M
max_connections = 2048
open_files_limit = 65535

skip-name-resolve
lower_case_table_names=1

character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'

innodb_buffer_pool_size = 1024M
innodb_log_file_size = 2048M
innodb_file_per_table = 1
innodb_flush_log_at_trx_commit = 0

key_buffer_size = 64M

log-error = /usr/local/mysql/log/mysql_error.log
log-bin = /usr/local/mysql/binlogs/mysql-bin
slow_query_log = 1
slow_query_log_file = /usr/local/mysql/log/mysql_slow_query.log
long_query_time = 5

tmp_table_size = 32M
max_heap_table_size = 32M
query_cache_type = 0
query_cache_size = 0

server-id=1

(6)初始化数据库:

cd mysql

./bin/mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --initialize(会卡在这里一段时间,请耐心等待)

打开另一个终端在日志文件里会提示一个临时密码,记录这个密码(等待的时间会有点长)

grep 'temporary password' /usr/local/mysql/log/mysql_error.log 
2017-03-12T13:26:30.619610Z 1 [Note] A temporary password is generated for root@localhost: b#uhQy*=d7yH

(7)生成ssl,给数据库加密:

mysql_ssl_rsa_setup --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/
Generating a 2048 bit RSA private key
.....................................................................................................+++
...................................................+++
writing new private key to 'ca-key.pem'
-----
Generating a 2048 bit RSA private key
..........................................................................................+++
.....................................................................................................................................................................................................+++
writing new private key to 'server-key.pem'
-----
Generating a 2048 bit RSA private key
...............................+++
......+++
writing new private key to 'client-key.pem'
-----

(8)设置启动项:
CentOS 6

cd /usr/local/mysql
cp support-files/mysql.server /etc/init.d/mysql.server
chkconfig --add mysql.server
chkconfig  mysql.server on
chkconfig --list

CentOS 7

cd /usr/lib/systemd/system
touch mysqld.service 

vi mysqld.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target

[Service]
User=mysql
Group=mysql

Type=forking

PIDFile=/usr/local/mysql/run/mysqld.pid

# Disable service start and stop timeout logic of systemd for mysqld service.
TimeoutSec=0

# Execute pre and post scripts as root
PermissionsStartOnly=true

# Needed to create system tables
#ExecStartPre=/usr/bin/mysqld_pre_systemd

# Start main service
ExecStart=/usr/local/mysql/bin/mysqld --daemonize --pid-file=/usr/local/mysql/run/mysqld.pid $MYSQLD_OPTS

# Use this to switch malloc implementation
EnvironmentFile=-/etc/sysconfig/mysql

# Sets open_files_limit
LimitNOFILE = 65535

Restart=on-failure

RestartPreventExitStatus=1

PrivateTmp=false

加载:

systemctl daemon-reload
systemctl enable mysqld.service
systemctl is-enabled mysqld

(9)启动mysql:

systemctl start mysqld.service

(10)进入命令行操作:

mysql -u root -p
Enter password:(输入前面生成的临时密码)
mysql> select version();
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> set password=password('A123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> select version();
+------------+
| version()  |
+------------+
| 5.7.17-log |
+------------+
1 row in set (0.00 sec)

(11)设置远程访问:
在远程访问之前需先打开centos7的端口:

[root@localhost ~]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
success

重启防火墙:

[root@localhost ~]# firewall-cmd --reload
success

注:不知道这两步有什么用,一开始我没执行这两步直接进行下面操作好像也没有什么影响啊。但命令比较新,写出来以后可参考。

(12)授权:
格式:grant all privileges on *.* to 远程访问用户名@'%' identified by '用户密码';

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

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

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select host,user from user;(多出1条远程登录用户记录)
+-----------+-----------+
| host      | user      |
+-----------+-----------+
| %         | root      |
| localhost | mysql.sys |
| localhost | root      |
+-----------+-----------+
3 rows in set (0.00 sec)

参考:Centos7 安装MySQL 5.7 (通用二进制包)
去官网下载包的速度特别的慢,这里给你们一个下载快的地址:http://mirrors.sohu.com/mysql/

四、安装方法三:yum安装

Redhat5.5和6.5用yum -y install mysql*就行。

Centos7.2中利用yum命令安装mysql:
参考:在CentOS7中利用yum命令安装mysql

安装服务端:mysql-server、mysql-devel;安装客户端:mysql:

yum install mysql-server mysql-devel mysql

不仔细看你会觉得已经安装成功了,其实mysql-server没有安装成功!
Linux多种方法安装mysql_第2张图片
我们需要手动下载一下,再单独安装mysql-server,此处用到wget命令:

下载rpm包:

wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm

安装rpm包:

rpm -ivh mysql-community-release-el7-5.noarch.rpm

检查是否安装成功,如下图安装成功:

ls -1 /etc/yum.repos.d/mysql-community*

在这里插入图片描述
安装mysql-server,这次如下图就真的安装好了!!!

yum install mysql-server

Linux多种方法安装mysql_第3张图片
启动mysql:

 service mysqld start或者/etc/init.d/mysqld start

创建root管理员:

mysqladmin -u root password 123456

登录mysql:

mysql -uroot -p123456

五、安装方法四:rpm安装

参考:CentOS 7下使用RPM安装mysql5.7.13

1.卸载系统自带的 mariadb-lib:

[root@centos-linux ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.44-2.el7.centos.x86_64
[root@centos-linux ~]# rpm -e mariadb-libs-5.5.44-2.el7.centos.x86_64 --nodeps

下载 rpm 安装包:
  去官网找到最新的 rpm 集合包。类似 mysql-5.7.13-1.el7.x86_64.rpm-bundle.tar复制其下载地址,在服务器下载 (或者本地下载了上传至服务器)。

[root@centos-linux ~]# wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.13-1.el7.x86_64.rpm-bundle.tar

Linux多种方法安装mysql_第4张图片
然后解压:

[root@centos-linux ~]# ls
mysql-5.7.13-1.el7.x86_64.rpm-bundle.tar
[root@centos-linux ~]# tar xvf mysql-5.7.13-1.el7.x86_64.rpm-bundle.tar
mysql-community-test-5.7.13-1.el7.x86_64.rpm
mysql-community-embedded-5.7.13-1.el7.x86_64.rpm
mysql-community-embedded-compat-5.7.13-1.el7.x86_64.rpm
mysql-community-server-5.7.13-1.el7.x86_64.rpm
mysql-community-client-5.7.13-1.el7.x86_64.rpm
mysql-community-common-5.7.13-1.el7.x86_64.rpm
mysql-community-server-minimal-5.7.13-1.el7.x86_64.rpm
mysql-community-embedded-devel-5.7.13-1.el7.x86_64.rpm
mysql-community-devel-5.7.13-1.el7.x86_64.rpm
mysql-community-libs-compat-5.7.13-1.el7.x86_64.rpm
mysql-community-libs-5.7.13-1.el7.x86_64.rpm
mysql-community-minimal-debuginfo-5.7.13-1.el7.x86_64.rpm

安装:
  至少安装两个:server 和 client, 但是存在包的依赖关系,所以依次执行(几个包有依赖关系,所以执行有先后)下面命令安装:

[root@centos-linux ~]# rpm -ivh mysql-community-common-5.7.13-1.el7.x86_64.rpm
[root@centos-linux ~]# rpm -ivh mysql-community-libs-5.7.13-1.el7.x86_64.rpm
[root@centos-linux ~]# rpm -ivh mysql-community-client-5.7.13-1.el7.x86_64.rpm
[root@centos-linux ~]# rpm -ivh mysql-community-server-5.7.13-1.el7.x86_64.rpm

数据库初始化:
  为了保证数据库目录为与文件的所有者为 mysql 登陆用户,如果你是以 root 身份运行 mysql 服务,需要执行下面的命令初始化:

mysqld --initialize --user=mysql

如果是以 mysql 身份运行,则可以去掉 --user 选项。
另外 --initialize 选项默认以“安全”模式来初始化,则会为 root 用户生成一个密码并将该密码标记为过期,登陆后你需要设置一个新的密码,而使用 --initialize-insecure 命令则不使用安全模式,则不会为 root 用户生成一个密码。

这里演示使用的 --initialize 初始化的,会生成一个 root 账户密码,密码在log文件里:
Linux多种方法安装mysql_第5张图片
上图里的最后一行则给出了生成的密码,现在就可以启动数据库了,然后使用上面的密码登陆:

[root@centos-linux ~]# systemctl start mysqld
[root@centos-linux ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.13

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>

修改 root 密码:
  该密码被标记为过期了,如果想正常使用还需要修改密码

mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

以前的 password()函数将会被抛弃,官方建议使用下面的命令来修改密码:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';

同时,如果你设置的密码过于简单也会报错:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

具体关于设置密码的规则,可以看这一篇这里我们注意设置密码 长度要大于8,同时要有数字,大小写,特殊字符。,可以修改上面这些配置减弱密码强度验证,比如:set global validate_password_policy=LOWset global validate_password_length=4

检查进程和端口监听:

ps -ef | grep mysql
netstat -ano | grep 3306

注意:在执行mysql -uroot -p命令后可能报如下的错误:

error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory.
解决:ln -s /usr/lib64/libncurses.so.6.1 /usr/lib64/libncurses.so.5

error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such file or directory.
解决:ln -s /usr/lib64/libtinfo.so.6.1 /usr/lib64/libtinfo.so.5

你可能感兴趣的:(数据库,mysql,安装,多种方法)