侧边栏壁纸
  • 累计撰写 72 篇文章
  • 累计创建 36 个标签
  • 累计收到 8 条评论

目 录CONTENT

文章目录

Linux CentOS 7 安装Mysql

散漫的老何
2019-11-07 / 0 评论 / 0 点赞 / 1,295 阅读 / 6,660 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2022-02-22,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

1、下载mysql安装包

下载安装包:https://dev.mysql.com/downloads/mysql/

wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.28-el7-x86_64.tar

mysql 下载

mysql tar安装包

下载好后,解压,我这里把mysql-5.7.23-el7-x86_64.tar 改名成了mysql-5.7.23.tar.gz

[root@git opt]# mv mysql-5.7.23-el7-x86_64.tar mysql-5.7.23.tar.gz
[root@git opt]# 
[root@git opt]# tar -xcvf mysql-5.7.23.tar.gz
[root@git opt]# ll
total 697288
drwxr-xr-x. 9 root root        168 Oct 10 21:56 mysql-5.7.23
-rw-r--r--. 1 7161 31415 714022690 Jun 10 23:40 mysql-5.7.23.tar.gz

mysql tar 解压

解压后有两个解压包,只管mysql-5.7.23-el7-x86_64.tar.gz这个包,另一个是测试包不用管

2、开始安装mysql

[root@git /opt]# tar -xf mysql5.7.23.tar.gz 
[root@git /opt]# ls
mysql5.7.23.tar.gz                   
mysql-5.7.23-el7-x86_64.tar.gz  
mysql-test-5.7.23-el7-x86_64.tar.gz
[root@git /]# useradd -s /bin/false -d /usr/local/mysql/ mysql
--创建用户 
[root@git /]# mkdir -p /usr/local/mysql/data
[root@git bin]# chown -R mysql.mysql /usr/local/mysql
--创建目录 
(这里没装过数据库是没有mysql这个目录的) 
[root@git /]# tar -xf mysql-5.7.23-el7-x86_64.tar.gz 
[root@git /]# cd mysql-5.7.23-el7-x86_64/
[root@git mysql-5.7.23-el7-x86_64]# ls
bin  COPYING  docs  include  lib  man  README  share support-files 
[root@git mysql-5.7.23-el7-x86_64]# cd bin/
[root@git bin]# ll
-rwxr-xr-x. 1 7161 31415   9054592 Jun  8  2018 innochecksum
-rwxr-xr-x. 1 7161 31415    321536 Jun  8  2018 lz4_decompress
-rwxr-xr-x. 1 7161 31415   8250024 Jun  8  2018 myisamchk
-rwxr-xr-x. 1 7161 31415   7765632 Jun  8  2018 myisam_ftdump
-rwxr-xr-x. 1 7161 31415   7539840 Jun  8  2018 myisamlog
-rwxr-xr-x. 1 7161 31415   7887968 Jun  8  2018 myisampack
-rwxr-xr-x. 1 7161 31415   5521880 Jun  8  2018 my_print_defaults
-rwxr-xr-x. 1 7161 31415  10775320 Jun  8  2018 mysql
-rwxr-xr-x. 1 7161 31415   9577208 Jun  8  2018 mysqladmin
-rwxr-xr-x. 1 7161 31415  11496360 Jun  8  2018 mysqlbinlog
-rwxr-xr-x. 1 7161 31415   9872296 Jun  8  2018 mysqlcheck
-rwxr-xr-x. 1 7161 31415 203899064 Jun  8  2018 mysql_client_test_embedded
-rwxr-xr-x. 1 7161 31415      4828 Jun  8  2018 mysql_config
-rwxr-xr-x. 1 7161 31415   8459976 Jun  8  2018 mysql_config_editor
-rwxr-xr-x. 1 7161 31415 247573176 Jun  8  2018 mysqld
-rwxr-xr-x. 1 7161 31415 167088376 Jun  8  2018 mysqld-debug
-rwxr-xr-x. 1 7161 31415     26738 Jun  8  2018 mysqld_multi
-rwxr-xr-x. 1 7161 31415     28494 Jun  8  2018 mysqld_safe
-rwxr-xr-x. 1 7161 31415   9921536 Jun  8  2018 mysqldump
-rwxr-xr-x. 1 7161 31415      7464 Jun  8  2018 mysqldumpslow
-rwxr-xr-x. 1 7161 31415 202992832 Jun  8  2018 mysql_embedded
-rwxr-xr-x. 1 7161 31415   9571376 Jun  8  2018 mysqlimport
-rwxr-xr-x. 1 7161 31415  10962296 Jun  8  2018 mysql_install_db
-rwxr-xr-x. 1 7161 31415   5582544 Jun  8  2018 mysql_plugin
-rwxr-xr-x. 1 7161 31415  19090048 Jun  8  2018 mysqlpump
-rwxr-xr-x. 1 7161 31415   9520064 Jun  8  2018 mysql_secure_installation
-rwxr-xr-x. 1 7161 31415   9528928 Jun  8  2018 mysqlshow
-rwxr-xr-x. 1 7161 31415   9626000 Jun  8  2018 mysqlslap
-rwxr-xr-x. 1 7161 31415   5959832 Jun  8  2018 mysql_ssl_rsa_setup
-rwxr-xr-x. 1 7161 31415 202520704 Jun  8  2018 mysqltest_embedded
-rwxr-xr-x. 1 7161 31415   5135512 Jun  8  2018 mysql_tzinfo_to_sql
-rwxr-xr-x. 1 7161 31415  13124840 Jun  8  2018 mysql_upgrade
-rwxr-xr-x. 1 7161 31415  25113368 Jun  8  2018 mysqlxtest
-rwxr-xr-x. 1 7161 31415   5659000 Jun  8  2018 perror
-rwxr-xr-x. 1 7161 31415   5312560 Jun  8  2018 replace
-rwxr-xr-x. 1 7161 31415   5521328 Jun  8  2018 resolveip
-rwxr-xr-x. 1 7161 31415   5601720 Jun  8  2018 resolve_stack_dump
-rwxr-xr-x. 1 7161 31415    123928 Jun  8  2018 zlib_decompress
[root@git bin]# yum -y install libaio gcc make
--安装依赖包 
[root@git bin]# ./mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --initialize
2018-08-03T08:22:14.766220Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-08-03T08:22:14.768375Z 0 [ERROR] Can't find error-message file '/usr/local/mysql/share/errmsg.sys'. Check error-message file location and 'lc-messages-dir' configuration directive.
2018-08-03T08:22:15.280059Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-08-03T08:22:15.372315Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-08-03T08:22:15.435202Z 0 [Warning] No existing UUID has been found, so we assume that thisis the first time that this server has been started. Generating a new UUID: 543f3d19-96f6-11e8-a609-525400603f3a.
2018-08-03T08:22:15.486161Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-08-03T08:22:15.486770Z 1 [Note] A temporary password is generated for root@localhost: wM<vlfOyU8kN
//到这里就安装完了

3、修改配置

[root@git /]# cat /etc/my.cnf
[mysqld]
secure_file_priv=/usr/local/mysql
port=3306                     	# 端口
basedir=/usr/local/mysql      	# 数据库安装路径
datadir=/usr/local/mysql/data 	# 数据文件保存路径
socket=/tmp/mysql.sock          # socket文件保存位置
character_set_server=utf8       # 数据库编码格式
user=mysql 			# 启动用户
max_connections=1500 		# 最大连接数
skip-grant-tables 		# 免密码登录

# Disabling symbolic-links is recommended to prevent assorted security risks
# symbolic-links=0 		# 是否支持符号链接,即数据库或表可以存储在my.cnf中指定datadir之外的分区或目录,为0不开启

[mysqld_safe]
pid-file=/usr/local/mysql/data/mysql.pid  # PID文件
log-error=/usr/local/mysql/data/error.log # 错误日志文件

# include all files from the config directory
!includedir /etc/my.cnf.d

4、启动

[root@git mysql-5.7.23-el7-x86_64]# pwd
/mysql-5.7.23-el7-x86_64 
[root@git mysql-5.7.23-el7-x86_64]# ls
bin  COPYING  docs  include  lib  man  README  share support-files 
[root@git mysql-5.7.23-el7-x86_64]# cp support-files/mysql.server /etc/init.d/mysqld
#复制启动文件到/etc/init.d目录
[root@git mysql-5.7.23-el7-x86_64]# cp -r bin/ /usr/local/mysql/
[root@git mysql-5.7.23-el7-x86_64]# chown -R mysql:mysql /usr/local/mysql/ 
# 设置属组属主
[root@git mysql-5.7.23-el7-x86_64]# cd /
[root@git /]# /etc/init.d/mysqld start
restartShutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS!
# 进入mysql会报无mysql命令解决方法
-bash: mysql: command not found
[root@git /]# ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql 

5、修改root用户默认密码

新安装好的MySQL没有修改root默认密码不能执行命令,会一直报错!
mysql> use mysql;
ERROR 1820 (HY000): Unknown error 1820

解决方法:
SET PASSWORD = PASSWORD('新密码'); # 修改root密码

6、F&Q

6.1、忘记密码并修改密码

  • 使用免密码登录,修改密码
1、修改my.conf配置文件
编辑/etc/my.cnf配置文件在[mysqld]的段中加上一句“skip-grant-tables”参数

例如: 
[mysqld] 
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock 
skip-grant-tables 

2.重新启动mysql服务
[root@git bin]# service mysqld restart 
Stopping MySQL: [ OK ] 
Starting MySQL: [ OK ]

3.登录MySQL并修改MySQL的root密码
[root@nginx-test ~]# mysql -uroot -p
Enter password: # 不需要输入密码直接接入MySQL
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 14159
Server version: 5.7.27 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> use mysql ; 
Database changed 
mysql> UPDATE user SET Password = password ( 'new-password' ) WHERE User = 'root'; 
Query OK, 0 rows affected (0.00 sec) Rows matched: 2 Changed: 0 Warnings: 0 
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec) 
mysql>quit ;
  • 第二种修改密码方法
mysql> use mysql;
mysql> set password for root@localhost = password('123456');
如果报错需要先执行
mysql> flush privileges;

6.2、mysql用户远程链接

mysql>use mysql;
mysql>update user set host = '%' where user= 'root';
mysql>select host, user from user;

6.3、mysql 全目录可执行

ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql

6.5、阿里云远程访问

如果换用户无法访问,检查有无访问链接路径的权限
阿里云需要配置安全组规则,开放3306端口
详见https://blog.csdn.net/wei389083222/article/details/78286629
0
广告 广告

评论区