Linux centos7x64系统下安装mysql(在线/离线)

centos7下离线安装mysql5.7

服务器环境:

centos7 x64

需要安装mysql5.7+

一、卸载CentOS7系统自带mariadb

# 查看系统自带的Mariadb
rpm -qa|grep mariadb
如:mariadb-libs-5.5.44-2.el7.centos.x86_64
# 卸载系统自带的Mariadb
rpm -e --nodeps mariadb-libs-5.5.44-2.el7.centos.x86_64
# 删除etc目录下的my.cnf
rm /etc/my.cnf

二、检查mysql是否存在

# 检查mysql是否存在
rpm -qa | grep mysql

三、查看用户和组是否存在

1)检查mysql组合用户是否存在

# 检查mysql组和用户是否存在,如无则创建
cat /etc/group | grep mysql
cat /etc/passwd | grep mysql 

# 查询全部用户(只是做记录,可以不执行)

cat /etc/passwd|grep -v nologin|grep -v halt|grep -v shutdown|awk -F ":" '{print $1 "|" $3 "1" $4}' | more

2)若不存在,则创建mysql组和用户

# 创建mysql用户组
[root@xhh ~]# groupadd mysql
# 创建一个用户名为mysql的用户,并加入mysql用户组
[root@xhh ~]# useradd -g mysql mysql
# 制定password 为 mysql
[root@xhh-141 ~]# passwd mysql
Changing password for user mysql.
New password:
BAD PASSWORD: The password is a palindrome
Retype new password:
passwd: all authentication tokens updated successfully.

四、下载mysql离线安装包tar文件

官网下载地址:MySQL :: Download MySQL Community Server

版本选择,可以选择一下两种方式:

1)使用Red Hat Enterprise Linux
Select Version:5.7.25
Select Operating System:Red Hat Enterprise Linux / Oracle Linux
Select OS Version:Red Hat Enterprise Linux 7 / Oracle Linux 7 (x86, 64-bit)
列表中下载:
Compressed TAR Archive:(mysql-5.7.25-el7-x86_64.tar.gz)
2)使用Linux - Generic
Select Version:5.7.25
Select Operating System:Linux - Generic
Select OS Version:Linux - Generic (glibc 2.12) (x86, 64-bit)
列表中下载:
Compressed TAR Archive:(mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz)【本文中使用的是这个版本】
注意:上边两种方式找mysql离线安装包的方式都可以。

五、上传第四步下载的mysql TAR包

# 进入/usr/local/文件夹
[root@xhh ~]# cd /usr/local/
# 上传mysql TAR包
# 解压mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
[root@xhh local]# ls
bin  full-path-to-mysql-VERSION-OS  include  lib64    mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz  share
etc  games                          lib      libexec  sbin                                 src
[root@xhh local]# tar -zxvf mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
mysql-5.7.25-lin
...
mysql-5.7.25-linux-glibc2.12-x86_64/share/install_rewriter.sql
mysql-5.7.25-linux-glibc2.12-x86_64/share/uninstall_rewriter.sql
mysql-5.7.25-linux-glibc2.12-x86_64/support-files/magic
mysql-5.7.25-linux-glibc2.12-x86_64/support-files/mysql.server
mysql-5.7.25-linux-glibc2.12-x86_64/docs/INFO_BIN
mysql-5.7.25-linux-glibc2.12-x86_64/docs/INFO_SRC
[root@xhh local]# ls
bin  full-path-to-mysql-VERSION-OS  include  lib64    mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz  share
etc  games                          lib      libexec  mysql-5.7.25-linux-glibc2.12-x86_64  sbin                                        src
# 进入/usr/local下,修改为mysql
[root@xhh local]# mv mysql-5.7.25-linux-glibc2.12-x86_64 mysql
[root@xhh local]# ls
bin  etc  full-path-to-mysql-VERSION-OS  games  include  lib  lib64  libexec  mysql  mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz  sbin  share  src

六、更改所属的组和用户

# 更改所属的组和用户
[root@xhh ~]# cd /usr/local/
[root@xhh local]# chown -R mysql mysql/
[root@xhh local]# chgrp -R mysql mysql/

七、在/usr/local/mysql下创建my.cnf文件

# 进入/usr/local/mysql文件夹下
[root@xhh ~]# cd /usr/local/mysql
# 创建my.cnf文件
[root@xhh mysql]# touch my.cnf #或者cd ''>my.cnf
# 编辑my.cnf
[root@xhh mysql]# vi my.cnf
[mysql]
socket=/var/lib/mysql/mysql.sock
# set mysql client default chararter
default-character-set=utf8

[mysqld]
socket=/var/lib/mysql/mysql.sock
# set mysql server port  
port = 3323 #默认是3306,这里发现3306已经被占用,因此防止这种情况发生,可以避免使用3306mysql默认端口
# set mysql install base dir
basedir=/usr/local/mysql
# set the data store dir
datadir=/usr/local/mysql/data
# set the number of allow max connnection
max_connections=200
# set server charactre default encoding
character-set-server=utf8
# the storage engine
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M
explicit_defaults_for_timestamp=true

[mysql.server]
user=mysql
basedir=/usr/local/mysql
[root@CDH-141 mysql]# 

八、进入mysql文件夹,并安装mysql

# 进入mysql
[root@xhh local]# cd /usr/local/mysql
# 安装mysql
[root@xhh mysql]# bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
2020-08-07 11:11:07 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2019-08-07 11:11:24 [WARNING] The bootstrap log isn't empty:
2019-08-07 11:11:24 [WARNING] 2019-03-08T10:11:07.208602Z 0 [Warning] --bootstrap is deprecated. Please consider using --initialize instead

设置文件及目录权限:

[root@xhh mysql]# cp ./support-files/mysql.server /etc/init.d/mysqld
[root@xhh mysql]# chmod 644 my.cnf
[root@xhh mysql]# ls
bin  COPYING  data  docs  include  lib  man  my.cnf  README  share  support-files
[root@xhh mysql]# chmod +x /etc/init.d/mysqld
[root@Cxhh mysql]#
[root@xhh mysql]# mkdir data
[root@xhh mysql]#
[root@xhh mysql]# chown -R mysql:mysql data
[root@xhh mysql]# chown -R mysql.mysql /var/lib/mysql/

九、启动mysql

# 启动mysql
[root@xhh mysql]# /etc/init.d/mysqld restart
MySQL server PID file could not be found![FAILED]
Starting MySQL.Logging to '/usr/local/mysql/data/xhh.err'.
..The server quit without updating PID file (/usr/local/mysql/data/CDH-141.pid).[FAILED]
[root@xhh mysql]# 

出现错误,解决方案如下:

#找到是否已经有进程占用
[root@xhh mysql]# ps aux|grep mysql
root     32483  0.0  0.0 113252  1620 pts/0    S    11:04   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/CDH-141.pid
mysql    32684  0.1  0.1 1119892 178224 pts/0  Sl   11:04   0:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=CDH-141.err --pid-file=/usr/local/mysql/data/CDH-141.pid --port=3323
root     35137  0.0  0.0 112648   944 pts/0    S+   11:12   0:00 grep --color=auto mysql

#关闭进程
[root@xhh mysql]# kill -9 32684
[root@xhh mysql]# /usr/local/mysql/bin/mysqld_safe: line 198: 32684 Killed                  nohup /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=CDH-141.err --pid-file=/usr/local/mysql/data/CDH-141.pid --port=3323 < /dev/null > /dev/null 2>&1
#确认是否还占用
[root@xhh mysql]#  ps aux|grep mysql
root     35501  0.0  0.0 112644   948 pts/0    S+   18:13   0:00 grep --color=auto mysql
[root@xhh mysql]# /etc/init.d/mysqld restart
MySQL server PID file could not be found![FAILED]
Starting MySQL..[  OK  ]
[root@xhh mysql]#

# 重启mysql
[root@xhh mysql]# /etc/init.d/mysqld restart
Shutting down MySQL..[  OK  ]
Starting MySQL..[  OK  ]
[root@xhh mysql]# 

十、设置开机自启动

#设置开机启动
[root@xhh mysql]# chkconfig --level 35 mysqld on
[root@xhh mysql]# chkconfig --list mysqld

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
[root@xhh mysql]# chmod +x /etc/rc.d/init.d/mysqld
[root@xhh mysql]# chkconfig --add mysqld
[root@xhh mysql]# chkconfig --list mysqld

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
[root@CDH-141 mysql]# service mysqld status
MySQL running (26122)[  OK  ]
[root@CDH-141 mysql]# 

十一、修改配置文件

# 进入/etc/profile文件夹
[root@xhh mysql]# vim /etc/profile
修改/etc/profile,在最后添加如下内容
# 修改/etc/profile文件
#set mysql environment
export PATH=$PATH:/usr/local/mysql/bin
# 使文件生效
[root@xhh mysql]# source /etc/profile

十二、获得mysql初始密码

 1)获得mysql初始密码

[root@xhh mysql]#  cat /root/.mysql_secret  
# Password set for user 'root@localhost' at 2019-03-08 17:40:42
poc3u0mO_luv
[root@xhh mysql]# 

2)修改密码

[root@xhh mysql]# mysql -uroot -p
Enter password: #此处填写上边获取到的初始密码‘poc3u0mO_luv’
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.25

Copyright (c) 2000, 2015, 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>  set PASSWORD = PASSWORD('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

3)验证新密码是否登录成功:

[root@xhh mysql]# mysql -uroot -p
Enter password: #此处输入新密码‘123456’
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.25 MySQL Community Server (GPL)

Copyright (c) 2000, 2015, 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> show tables;
ERROR 1046 (3D000): No database selected
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> 

十三、添加远程访问权限

# 添加远程访问权限
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set host='%' where user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select host,user from user;
+-----------+---------------+
| host      | user          |
+-----------+---------------+
| %         | root          |
| localhost | mysql.session |
| localhost | mysql.sys     |
+-----------+---------------+
3 rows in set (0.00 sec)

mysql> 

十四、重启mysql生效

# 重启mysql
[root@xhh mysql]# /etc/init.d/mysqld restart
Shutting down MySQL..[  OK  ]
Starting MySQL..[  OK  ]
[root@xhh mysql]# 

centos7下线安装mysql5.7

1、下载、添加仓库和更新

wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum update

2、安装MySql

yum install mysql-server
systemctl start mysqld  

3、配置MySql

运行如下脚本文件

mysql_secure_installation

提示输入root密码,初始为null,回车就可以设置root密码了

之后提示一连串的问题,根据需要自己选择y 或者 n吧

4、连接MySql

mysql -uroot -p

5、查看MySql的编码并修改

#查看mysql的编码
mysql>show variables like '%character%'; 

这里写图片描述

可以看到database和server编码都为latin1,我们修改为utf8

#1.将my-default.cnf 拷贝到 /etc/my.cnf  根据情况找到自己的目录
cp /usr/share/doc/mysql-community-server-5.6.31/my-default.cnf  /etc/my.cnf
#2.编辑/usr/my.cnf在[client](如果没有就自己加)下增加
default-character-set = utf8
#3.编辑/usr/my.cnf在[mysqld]下增加
character_set_server =  utf8
#4.重启MySql服务  这句代码和之前使用的net start mysql作用应该是一样的 开启服务 centos7使用以下代码
systemctl restart  mysql.service
#5.重现连接mysql并查看编码如下:
mysql -uroot –proot
mysql>show variables like '%character%';

这里写图片描述

6、允许远程登录

用root用户登录mysql

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root';
FLUSH PRIVILEGES;

7、开放3306端口

firewall-cmd --zone=public --add-port=3306/tcp --permanent
firewall-cmd --reload

小编在使用mysql时,随手做个记录,希望对大家有所帮助,如还有疑问或者有更好的推荐可留言~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小慌慌

感谢博友的鼓励,快乐分享~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值