Linux下Mysql的安装步骤

Posted codebuglife

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux下Mysql的安装步骤相关的知识,希望对你有一定的参考价值。

(1)、下载安装包

https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.23-linux-glibc2.12-x86_64.tar

技术图片
[root@localhost local]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.23-linux-glibc2.12-x86_64.tar
--2018-08-02 18:04:25--  https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.23-linux-glibc2.12-x86_64.tar
Resolving dev.mysql.com (dev.mysql.com)... 137.254.60.11
Connecting to dev.mysql.com (dev.mysql.com)|137.254.60.11|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.23-linux-glibc2.12-x86_64.tar [following]
--2018-08-02 18:04:27--  https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.23-linux-glibc2.12-x86_64.tar
Resolving cdn.mysql.com (cdn.mysql.com)... 23.36.193.224
Connecting to cdn.mysql.com (cdn.mysql.com)|23.36.193.224|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 673873920 (643M) [application/x-tar]
Saving to: 鈥榤ysql-5.7.23-linux-glibc2.12-x86_64.tar鈥

100%[========================================================================>] 673,873,920  191KB/s   in 53m 15s

2018-08-02 18:57:42 (206 KB/s) - 鈥榤ysql-5.7.23-linux-glibc2.12-x86_64.tar鈥saved [673873920/673873920]

[root@localhost local]# ls
bin  etc  games  include  lib  lib64  libexec  mysql-5.7.23-linux-glibc2.12-x86_64.tar  sbin  share  src
[root@localhost local]# mv mysql-5.7.23-linux-glibc2.12-x86_64.tar src/
技术图片

(2)、解压到/usr/local/下,重命名为mysql

技术图片
[root@localhost src]# tar xvf mysql-5.7.23-linux-glibc2.12-x86_64.tar -C /usr/local/
mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
mysql-test-5.7.23-linux-glibc2.12-x86_64.tar.gz
[root@localhost src]# cd ../
[root@localhost local]# ls
bin    include  libexec                                          sbin
etc    lib      mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz       share
games  lib64    mysql-test-5.7.23-linux-glibc2.12-x86_64.tar.gz  src
[root@localhost local]# tar xvf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz 
mysql-5.7.23-linux-glibc2.12-x86_64/bin/myisam_ftdump
mysql-5.7.23-linux-glibc2.12-x86_64/bin/myisamchk
mysql-5.7.23-linux-glibc2.12-x86_64/bin/myisamlog
mysql-5.7.23-linux-glibc2.12-x86_64/bin/myisampack
.
.
.
[root@localhost local]# mv mysql-5.7.23-linux-glibc2.12-x86_64 mysql
[root@localhost local]# ls
bin games lib libexec mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz sbin src
etc include lib64 mysql mysql-test-5.7.23-linux-glibc2.12-x86_64.tar.gz share
技术图片

 (3)、在mysql下创建数据库文件目录

[root@localhost local]# mkdir mysql/data

(4)、创建mysql用户组和用户,并对mysql目录设置用户组和用户

[root@localhost local]# groupadd mysql
[root@localhost local]# useradd mysql -g mysql
[root@localhost local]# cd mysql/
[root@localhost mysql]# chown -R mysql .
[root@localhost mysql]# chgrp -R mysql .

(5)、安装初始化

由于MySQL运行需要libaio库,所以需要运行以下命令进行安装

技术图片
[root@localhost mysql]# yum install libaio
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.cn99.com
 * extras: mirrors.shu.edu.cn
 * updates: mirrors.shu.edu.cn
Package libaio-0.3.109-13.el7.x86_64 already installed and latest version
Nothing to do
技术图片
技术图片
[root@localhost bin]# ./mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
2018-08-02 19:43:41 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2018-08-02 19:43:45 [WARNING] The bootstrap log isn‘t empty:
2018-08-02 19:43:45 [WARNING] 2018-08-02T11:43:41.811985Z 0 [Warning] --bootstrap is deprecated. Please consider using --initialize instead
2018-08-02T11:43:41.819990Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5000)
2018-08-02T11:43:41.820064Z 0 [Warning] Changed limits: table_open_cache: 431 (requested 2000)
技术图片
[root@localhost support-files]# ./mysql.server start
Starting MySQL.Logging to ‘/usr/local/mysql/data/localhost.localdomain.err‘.
 SUCCESS! 

(6)、登录mysql

此版本最新版不许空密码登录,实际上有个初始化密码保存在/root/.mysql_secret这个文件里面,用这个密码第一次登录后,再修改密码。

技术图片
[root@localhost support-files]# cat /root/.mysql_secret 
# Password set for user ‘root@localhost‘ at 2018-08-02 22:49:26 
B#ipur,uyB>a
[root@localhost support-files]# mysql -uroot -p
-bash: mysql: command not found                 
[root@localhost support-files]# ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
[root@localhost support-files]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 4
Server version: 5.7.23

Copyright (c) 2000, 2018, 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>
技术图片

 改mysql的root密码,新密码在此为‘123456‘

mysql> set password=password(‘123456‘);
Query OK, 0 rows affected, 1 warning (0.00 sec)

(7)、复制配置文件

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

技术图片
[root@localhost support-files]# ls -la
total 24
drwxr-xr-x.  2 mysql mysql    90 Aug  2 19:27 .
drwxr-xr-x. 10 mysql mysql   141 Aug  2 19:32 ..
-rw-r--r--.  1 mysql mysql   773 Jun  8 17:56 magic
-rwxr-xr-x.  1 mysql mysql  1061 Jun  8 18:55 mysqld_multi.server
-rwxr-xr-x.  1 mysql mysql   894 Jun  8 18:55 mysql-log-rotate
-rwxr-xr-x.  1 mysql mysql 10576 Jun  8 18:55 mysql.server
技术图片

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

技术图片
# 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]
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
socket = /tmp/mysql.sock
key_buffer_size=16M
max_allowed_packet=8M
character-set-server=utf8
back_log = 300
max_connections = 3000
max_connect_errors = 50
table_open_cache = 4096
#binlog_cache_size = 4M
max_heap_table_size = 128M
read_rnd_buffer_size = 16M
sort_buffer_size = 16M
join_buffer_size = 16M
thread_cache_size = 16
query_cache_size = 128M
query_cache_limit = 4M
ft_min_word_len = 8
thread_stack = 512K
transaction_isolation = REPEATABLE-READ
tmp_table_size = 128M
#log-bin=mysql-bin
long_query_time = 6
server_id=1
innodb_buffer_pool_size = 1G
innodb_thread_concurrency = 16
innodb_log_buffer_size = 16M
innodb_log_file_size = 512M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
innodb_file_per_table = on

[mysqldump]
quick
max_allowed_packet = 32M

[mysql]
no-auto-rehash
connect_timeout=2

[client]
port=3306
socket = /tmp/mysql.sock
技术图片
[root@localhost support-files]# cp my-default.cnf /etc/my.cnf
[root@localhost support-files]# 
[root@localhost support-files]# ./mysql.server start
Starting MySQL. SUCCESS!

 (8)、将mysqld服务加入开机自启动项

首先需要将support-files/mysql.server服务脚本复制到/etc/init.d/,并重命名为mysqld。

技术图片
[root@localhost support-files]# cp mysql.server /etc/init.d/mysqld
[root@localhost support-files]# ls -la /etc/init.d/
total 52
drwxr-xr-x. 2 root root 84 Aug 3 01:04 .
drwxr-xr-x. 10 root root 127 Jul 31 17:23 ..
-rw-r--r--. 1 root root 18104 Jan 3 2018 functions
-rwxr-xr-x. 1 root root 10576 Aug 3 01:04 mysqld
-rwxr-xr-x. 1 root root 4334 Jan 3 2018 netconsole
-rwxr-xr-x. 1 root root 7293 Jan 3 2018 network
-rw-r--r--. 1 root root 1160 Apr 11 15:36 README
技术图片

通过chkconfig命令将mysqld服务加入到自启动服务项中

[root@localhost support-files]# chkconfig --add mysqld

查看是否添加成功

技术图片
[root@localhost support-files]# 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
技术图片

 (9)、重启系统,mysqld就会自动启动了

检查是否启动

[root@localhost ~]# netstat -anp|grep mysqld
tcp6       0      0 :::3306                 :::*                    LISTEN      1535/mysqld         
unix  2      [ ACC ]     STREAM     LISTENING     21467    1535/mysqld          /tmp/mysql.sock

如果不想重新启动,那可以直接手动启动。

[root@localhost ~]# service mysqld restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS!

 (10)、设定远程登录mysql

技术图片
mysql> use mysql;
Database changed
mysql> select host,user from user;
+-----------+---------------+
| host      | user          |
+-----------+---------------+
| localhost | mysql.session |
| localhost | mysql.sys     |
| localhost | root          |
+-----------+---------------+
3 rows in set (0.00 sec)
mysql> grant all privileges on *.* to root@‘%‘ identified by ‘123456‘;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> select host,user from user;
+-----------+---------------+
| host | user |
+-----------+---------------+
| % | root |
| localhost | mysql.session |
| localhost | mysql.sys |
| localhost | root |
+-----------+---------------+
4 rows in set (0.00 sec)

 

以上是关于Linux下Mysql的安装步骤的主要内容,如果未能解决你的问题,请参考以下文章

linux中怎么查看mysql数据库版本

linux下采用binary方式安装mysql步骤

linux下安装mysql

详细讲解Linux环境下MySQL 5.1安装步骤

linux 环境下安装mysql----ubuntu

linux安装mysql详细步骤