Linux下Mysql的安裝步驟

(1)、下載安裝包html

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

[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/下,重命名爲mysqllinux

[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下建立數據庫文件目錄sql

[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)、安裝初始化bootstrap

因爲MySQL運行須要libaio庫,因此須要運行如下命令進行安裝bash

[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)、登陸mysqlsession

此版本最新版不準空密碼登陸,實際上有個初始化密碼保存在/root/.mysql_secret這個文件裏面,用這個密碼第一次登陸後,再修改密碼。app

[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'dom

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)
相關文章
相關標籤/搜索