二进制安装Mysql5.7

1.创建mysql用户组和用户

groupadd mysql
useradd -r -g mysql -s /sbin/nolog mysql

2.下载mysql相关版本二进制包,并解压移动至/usr/local目录

wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz 
tar xf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz #解压到当前目录
mv /usr/local/src/mysql-5.7.24-linux-glibc2.12-x86_64/ /usr/local/mysql #移动并重命名

3.在Mysql目录创建data目录,并授予相关权限

mkdir -p /usr/local/mysql/data
mkdir -p /usr/local/mysql/log/mysql-error.log ###log文件 chown -R mysql.mysql . chmod -R 755 /usr/local/mysql

4.卸载自带mysql、mariadb数据

rpm -qa | grep mysql
rpm -qa | grep mariadb
rpm -e --nodeps 包名

5.初始化mysql数据库,会生成一个随机密码,记得保存好。警告提示关系不大,可以忽略

复制代码

[root@centos7 mysql]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
2020-06-05T07:37:39.505468Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-06-05T07:37:39.855409Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-06-05T07:37:39.893978Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-06-05T07:37:39.948706Z 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: 6f20660a-a6ff-11ea-9581-000c292c3141.
2020-06-05T07:37:39.949391Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-06-05T07:37:39.949923Z 1 [Note] A temporary password is generated for root@localhost: i-MIi>gjt1od

复制代码

6.配置/etc/my.cnf文件,至关重要。数据库能不能正常启动就看这个配置文件了。如果配置不对,会给你搞一堆的错误出来....很是脑阔痛

复制代码
[root@oracle mysql]# vim /etc/my.cnf

[client]
port = 3306
socket = /tmp/mysql.sock

[mysqld]
skip-grant-tables
server_id=10
port = 3306
user = mysql
character-set-server = utf8mb4
default_storage_engine = innodb
log_timestamps = SYSTEM
socket = /tmp/mysql.sock
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
pid-file = /usr/local/mysql/data/mysql.pid
max_connections = 1000
max_connect_errors = 1000
table_open_cache = 1024
max_allowed_packet = 128M
open_files_limit = 65535
#####====================================[innodb]==============================
innodb_buffer_pool_size = 1024M
innodb_file_per_table = 1
innodb_write_io_threads = 4
innodb_read_io_threads = 4
innodb_purge_threads = 2
innodb_flush_log_at_trx_commit = 1
innodb_log_file_size = 512M
innodb_log_files_in_group = 2
innodb_log_buffer_size = 16M
innodb_max_dirty_pages_pct = 80
innodb_lock_wait_timeout = 30
innodb_data_file_path=ibdata1:1024M:autoextend

#####====================================[log]==============================
log_error = /usr/local/mysql/log/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /usr/local/mysql/log/mysql-slow.log

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

复制代码

7.启动mysql数据库

ln -vs /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld #建立软连接
/etc/init.d/mysqld start

8.登陆数据库

ln -vs /usr/local/mysql/bin/mysql /usr/bin/ #建立软链接,也就是windows说的快捷方式
mysql -uroot -p密码 #用初始化生产的随机密码登陆数据库做相应的授权操作

附加

总结一下二进制安装Mysql5.7数据库所遇到的问题

1.Cetos7操作系统不能解压带gz后缀的包,换了多少个参数都没用。不知道是哪里的问题,暂时未找到解决办法,有遇到的网友可以留言告知下。错误如下所示

gzip: stdin: not in gzip format
tar: Child returned status 1
tar: Error is not recoverable: exiting now

2.前面也说到配置文件的问题,这个真的是至关重要,就因为配置文件里面有个两个datadir导致一直出现如下错误,如果mysql.sock之前已经存在了请删除在启动Mysql,不然也会报下面的错误

[root@oracle data]# /etc/init.d/mysqld start
Starting MySQL..... ERROR! The server quit without updating PID file (/usr/local/mysql/data/mysql.pid)###删除data目录重新执行步骤5,问题解决

3.登陆数据库之后出现的问题,如下所示

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

这个问题这个主要是由一个参数控制的 default_password_lifetime,有兴趣的朋友可以去看看官方的解释

我是从一个博客中看到这个的。资料来源

解决方法如下

alter user user() identified by "密码";

4.mysql -uroot -p不能使用,这个是由于没有建立软链接导致;上文有说。

大概就这么多,如有遗漏和不足请大家留言,我会及时更正文档!

原文地址:https://www.cnblogs.com/skyhu365/p/13049802.html