Linux服务篇----构建MySql(二)

  • 下载

首先去mysql官网下载Linux下mysql安装的tar包
路径:https://dev.mysql.com/downloads/mysql/
Linux服务篇----构建MySql(二)_第1张图片
下载tab页下,选择mysql社区下载
Linux服务篇----构建MySql(二)_第2张图片
点击MySQL社区服务器
Linux服务篇----构建MySql(二)_第3张图片
寻找之前版本,也可以在当前页面下载最新版本
Linux服务篇----构建MySql(二)_第4张图片
Linux服务篇----构建MySql(二)_第5张图片
也可以进入linux后用命令下载:
wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
Linux服务篇----构建MySql(二)_第6张图片

  • 安装

下载好需要的tar包,进行源码安装MySql
1.安装前,我们可以检测系统是否自带安装 MySQL:rpm -qa | grep mysql,若有(类似:mysql-libs-5.1.73-8.el6_8.x86_64)需要卸载
rpm -e mysql-libs-5.1.73-8.el6_8.x86_64 ---- 普通删除模式
rpm -e --nodeps mysql-libs-5.1.73-8.el6_8.x86_64 ---- 强力删除模式,如果使用上面命令删除时,提示有依赖的其它文件,则用该命令可以对其进行强力删除
检查否存在 mariadb 数据库:rpm -qa | grep mariadb,如有(类似:mariadb-libs-5.5.56-2.el7.x86_64),同样卸载

[root@localhost mysql]# rpm -qa | grep mysql
mysql-libs-5.1.73-8.el6_8.x86_64
[root@localhost mysql]# rpm -e mysql-libs-5.1.73-8.el6_8.x86_64
error: Failed dependencies:
        libmysqlclient.so.16()(64bit) is needed by (installed) postfix-2:2.6.6-8.el6.x86_64
        libmysqlclient.so.16(libmysqlclient_16)(64bit) is needed by (installed) postfix-2:2.6.6-8.el6.x86_64
        mysql-libs is needed by (installed) postfix-2:2.6.6-8.el6.x86_64
[root@localhost mysql]# rpm -e --nodeps mysql-libs-5.1.73-8.el6_8.x86_64
[root@localhost mysql]# rpm -qa | grep mariadb
[root@localhost mysql]# 
[root@localhost mysql]# ll
总用量 629820
-rw-r--r--. 1 root root 644930593 2月  17 20:09 mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
[root@localhost mysql]# 

2.解压缩包tar -zxvf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz,重命名文件夹

[root@localhost mysql]# ll
总用量 629820
-rw-r--r--. 1 root root 644930593 2月  17 20:09 mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
[root@localhost mysql]# tar -zxvf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
mysql-5.7.24-linux-glibc2.12-x86_64/bin/myisam_ftdump
mysql-5.7.24-linux-glibc2.12-x86_64/bin/myisamchk
mysql-5.7.24-linux-glibc2.12-x86_64/bin/myisamlog
mysql-5.7.24-linux-glibc2.12-x86_64/bin/myisampack
mysql-5.7.24-linux-glibc2.12-x86_64/bin/mysql

......

mysql-5.7.24-linux-glibc2.12-x86_64/share/install_rewriter.sql
mysql-5.7.24-linux-glibc2.12-x86_64/share/uninstall_rewriter.sql
mysql-5.7.24-linux-glibc2.12-x86_64/support-files/magic
mysql-5.7.24-linux-glibc2.12-x86_64/support-files/mysql.server
mysql-5.7.24-linux-glibc2.12-x86_64/docs/INFO_BIN
mysql-5.7.24-linux-glibc2.12-x86_64/docs/INFO_SRC
[root@localhost mysql]# mv mysql-5.7.24-linux-glibc2.12-x86_64 mysql-5.7.24
[root@localhost mysql]# ll
总用量 629824
drwxr-xr-x. 9 root root      4096 4月   5 22:47 mysql-5.7.24
-rw-r--r--. 1 root root 644930593 2月  17 20:09 mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
[root@localhost mysql]# 

3.添加用户及用户组mysql:mysql,mysql-5-7-24文件夹改变所有者或者所属的组为mysql
检查mysql组是否存在,如无创建

[root@localhost mysql]# cat /etc/group | grep mysql
[root@localhost mysql]# groupadd mysql
[root@localhost mysql]# cat /etc/group | grep mysql
mysql:x:501:
[root@localhost mysql]# 

检查mysql用户是否存在,如无创建

[root@localhost mysql]# cat /etc/passwd | grep mysql
[root@localhost mysql]# useradd -r -g mysql mysql
[root@localhost mysql]# cat /etc/passwd | grep mysql
mysql:x:498:501::/home/mysql:/bin/bash
[root@localhost mysql]# 

mysql-5.7.24目录下创建data文件夹

[root@localhost mysql]# cd mysql-5.7.24
[root@localhost mysql-5.7.24]# mkdir data
[root@localhost mysql-5.7.24]# ll
总用量 56
drwxr-xr-x.  2 root root   4096 4月   5 22:47 bin
-rw-r--r--.  1 7161 31415 17987 10月  4 2018 COPYING
drwxr-xr-x.  2 root root   4096 4月   5 23:04 data
drwxr-xr-x.  2 root root   4096 4月   5 22:47 docs
drwxr-xr-x.  3 root root   4096 4月   5 22:47 include
drwxr-xr-x.  5 root root   4096 4月   5 22:47 lib
drwxr-xr-x.  4 root root   4096 4月   5 22:47 man
-rw-r--r--.  1 7161 31415  2478 10月  4 2018 README
drwxr-xr-x. 28 root root   4096 4月   5 22:47 share
drwxr-xr-x.  2 root root   4096 4月   5 22:47 support-files
[root@localhost mysql-5.7.24]# 

将/usr/local/mysql/mysql-5.7.24的所有者及所属组改为mysql

[root@localhost mysql-5.7.24]# pwd
/usr/local/mysql/mysql-5.7.24
[root@localhost mysql-5.7.24]# chown -R mysql:mysql /usr/local/mysql/mysql-5.7.24
[root@localhost mysql-5.7.24]# ll
总用量 56
drwxr-xr-x.  2 mysql mysql  4096 4月   5 22:47 bin
-rw-r--r--.  1 mysql mysql 17987 10月  4 2018 COPYING
drwxr-xr-x.  2 mysql mysql  4096 4月   5 23:04 data
drwxr-xr-x.  2 mysql mysql  4096 4月   5 22:47 docs
drwxr-xr-x.  3 mysql mysql  4096 4月   5 22:47 include
drwxr-xr-x.  5 mysql mysql  4096 4月   5 22:47 lib
drwxr-xr-x.  4 mysql mysql  4096 4月   5 22:47 man
-rw-r--r--.  1 mysql mysql  2478 10月  4 2018 README
drwxr-xr-x. 28 mysql mysql  4096 4月   5 22:47 share
drwxr-xr-x.  2 mysql mysql  4096 4月   5 22:47 support-files
[root@localhost mysql-5.7.24]# 

4.在/usr/local/mysql/mysql-5.7.24/support-files目录下创建my_default.cnftouch my_default.cnf或者直接写入内容vi my_default.cnf
修改basedir/datadir/log-error/pid-file为自己的路径,以及数据库字符编码为utf8,我的数据库编码为gbk

# 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]
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 
 
basedir = /usr/local/mysql/mysql-5.7.24
datadir = /usr/local/mysql/mysql-5.7.24/data
port = 3306
socket = /tmp/mysql.sock
character-set-server=gbk
 
[mysqld_safe]
log-error = /usr/local/mysql/mysql-5.7.24/data/mysqld.log
pid-file = /usr/local/mysql/mysql-5.7.24/data/mysqld.pid

[client]
default-character-set=gbk

:wq保存文件命令
拷贝support-files/my_default.cnf到/etc/my.cnf,存在文件提示是否覆盖,y

[root@localhost support-files]# cp ./my_default.cnf /etc/my.cnf
[root@localhost support-files]# cat /etc/my.cnf 
# 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]
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 
 
basedir = /opt/mysql/mysql-5.7.25
datadir = /opt/mysql/mysql-5.7.25/data
port = 3306
socket = /tmp/mysql.sock
character-set-server=gbk
 
[mysqld_safe]
log-error = /opt/mysql/mysql-5.7.25/data/mysqld.log
pid-file = /opt/mysql/mysql-5.7.25/data/mysqld.pid

[client]
default-character-set=gbk
[root@localhost support-files]# 

5.初始化 mysqld,得到临时密码,查看日志

[root@localhost mysql]# cd /usr/local/mysql/mysql-5.7.24
[root@localhost mysql-5.7.24]# 
[root@localhost mysql-5.7.24]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/mysql-5.7.24/ --datadir=/usr/local/mysql/mysql-5.7.24/data/
2020-04-05T14:41:09.709852Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-04-05T14:41:09.710252Z 0 [Warning] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
2020-04-05T14:41:09.710260Z 0 [Warning] 'NO_AUTO_CREATE_USER' sql mode was not set.
2020-04-05T14:41:10.017063Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-04-05T14:41:10.080687Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-04-05T14:41:10.156468Z 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: 7d97a287-774b-11ea-bffa-000c29003a07.
2020-04-05T14:41:10.159758Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-04-05T14:41:10.164283Z 1 [Note] A temporary password is generated for root@localhost: s+fBo*r+p1pa
[root@localhost mysql-5.7.24]# cat /usr/local/mysql/mysql-5.7.24/data/mysqld.log 
2020-04-05T14:42:11.185595Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-04-05T14:42:11.185976Z 0 [Warning] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
2020-04-05T14:42:11.185982Z 0 [Warning] 'NO_AUTO_CREATE_USER' sql mode was not set.
2020-04-05T14:42:11.495136Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-04-05T14:42:11.541794Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-04-05T14:42:11.610538Z 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: a238cc02-774b-11ea-8081-000c29003a07.
2020-04-05T14:42:11.611671Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-04-05T14:42:11.613525Z 1 [Note] A temporary password is generated for root@localhost: *siSDo5yseRi
[root@localhost mysql-5.7.24]# 

记住临时密码[Note] A temporary password is generated for root@localhost: *siSDo5yseRi
密码:*siSDo5yseRi
6.把启动脚本放到开机初始化目录,启动mysql服务
在/usr/local/mysql/mysql-5.7.24/bin目录下用临时密码登录mysql root用户

[root@localhost mysql-5.7.24]# cp support-files/mysql.server /etc/init.d/mysql
[root@localhost mysql-5.7.24]# service mysql start
Starting MySQL. SUCCESS! 
[root@localhost mysql-5.7.24]# cd /usr/local/mysql/mysql-5.7.24
[root@localhost mysql-5.7.24]# ./bin/mysql -uroot -p*siSDo5yseRi
mysql: [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 2
Server version: 5.7.24

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> 

7.修改密码,重启服务,用户名密码登录
service mysql stop##停止 service mysql start##启动
service mysql restart##重启

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

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> exit;
Bye
[root@localhost mysql-5.7.24]# service mysql restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 
[root@localhost mysql-5.7.24]# 
[root@localhost mysql-5.7.24]# ./bin/mysql -uroot -p1234
mysql: [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 3
Server version: 5.7.24 MySQL Community Server (GPL)

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> 

你可能感兴趣的:(linux,mysql)