centos7安装mysql5.7并设置开机自启动详细步骤

1.下载mysql5.7版本

[root@localhost home]#wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
2.卸载系统自带的Mariadb
Centos7将默认数据库mysql替换成了Mariadb,如果想继续使用mysql 需要卸载Mariadb 再安装mysql
rpm -qa|grep mariadb
rpm -e --nodeps 文件名 (上面指令查出的所有文件)

[root@localhost home]# rpm -qa|grep mariadb
mariadb-libs-5.5.44-2.el7.centos.x86_64
[root@localhost home]# rpm -e --nodeps mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz 

3.删除etc目录下的my.cnf文件

[root@localhost home]# rm -rf /etc/my.cnf
4.创建mysql用户组和mysql用户,并加入mysql组

[root@localhost home]# groupadd mysql
[root@localhost home]# useradd -r -g mysql mysql
5.将下载的压缩包放到 /usr/local/ 目录下(通过mv 要移动的文件  /usr/local/)

[root@localhost home]# mv mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz /usr/local/
6.进入/usr/local/下,解压安装包

[root@localhost home]# cd /usr/local/
[root@localhost local]# tar -zxvf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz 
7. 将解压好的文件夹重命名为mysql

[root@localhost local]# mv mysql-5.7.17-linux-glibc2.5-x86_64 mysql
8.在 etc 下新建配置文件my.cnf,并在该文件中添加以下配置代码:

[root@localhost ~]# vi /etc/my.cnf 
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
socket=/var/lib/mysql/mysql.sock

[mysqld]
skip-name-resolve
#设置3306端口
port = 3306
socket=/var/lib/mysql/mysql.sock
# 设置mysql的安装目录
basedir=/usr/local/mysql
# 设置mysql数据库的数据的存放目录
datadir=/home/local/mysql/data
# 允许最大连接数
max_connections=500
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf-8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M


9.创建上面配置文件需要用到的目录,并给用到的目录修改拥有者为mysql用户

[root@localhost local]# mkdir /var/lib/mysql
[root@localhost local]# mkdir /home/local/mysql/data
[root@localhost local]# chown -R mysql:mysql /home/local/mysql/data
[root@localhost local]# chown -R mysql:mysql /var/lib/mysql
[root@localhost local]# chown -R mysql:mysql /usr/local/mysql
10.配置环境变量
[root@localhost bin]# vi ~/.bash_profile

修改PATH,增加mysql的bin目录:
PATH=$PATH:$HOME/bin:/usr/local/mysql/bin

11.执行下面的命令是修改的内容立即生效:

[root@localhost bin]# source ~/.bash_profile

12.授予/etc/my.cnf最大权限

root@localhost local]# chown 777 /etc/my.cnf
13.设置开机自启动服务控制脚本:(将{mysql}/ support-files/mysql.server 拷贝为/etc/init.d/mysqld并设置运行权限)

[root@localhost mysql]# cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@localhost mysql]# chmod +x /etc/rc.d/init.d/mysqld
[root@localhost mysql]# chkconfig --add mysqld
[root@localhost mysql]# chkconfig --list mysqld
结果显示:

mysqld          0:关    1:关    2:开    3:开    4:开    5:开    6:关

表明mysqld服务已经生效,在2、3、4、5运行级别随系统启动而自动启动,以后可以使用systemctl 命令控制mysql的启动和停止。

13.初始化数据库

[root@localhost mysql]# /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/home/local/mysql/data/
14.启动数据库

[root@localhost mysql]# systemctl start mysqld
15.初次安装执行安全配置向导

[root@localhost bin]# mysql_secure_installation
 a)为root用户设置密码
  b)删除匿名账号
  c)取消root用户远程登录
  d)删除test库和对test库的访问权限
  e)刷新授权表使修改生效
具体选项如下:
Securing the MySQL server deployment.
Enter password for user root: 
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.

Estimated strength of the password: 100 
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y

New password: 

Re-enter new password: 

Estimated strength of the password: 100 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : no

 ... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done! 

16.登录mysql,修改远程访问权限

[root@localhost /]# mysql -uroot -p
mysql> use mysql;
Database changed
mysql> update user set host = '%' where user = 'root';
Query OK, 1 row affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)



  • 0
    点赞
  • 1
    评论
  • 9
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

©️2022 CSDN 皮肤主题:编程工作室 设计师:CSDN官方博客 返回首页

打赏作者

nickLpq

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

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

打赏作者

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

抵扣说明:

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

余额充值