Centos7 安装mysql5.7.x

centos7 x64 安装mysql5.7+

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.30-linux-glibc2.12-x86_64.tar.gz)【本文中使用的是这个版本】

 

1、上传 mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz 到服务器 /usr/local/ 目录

2、解压

tar -xzvf mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz

3、改名

mv mysql-5.1.73-linux-x86_64-glibc23 mysql

4、创建Mysql用户组

1)创建mysql 组
groupadd mysql

2)创建MySQL用户但该用户不能登陆(-s /bin/false参数指定mysql用户仅拥有所有权,而没有登录权限)
useradd -r -g mysql -s /bin/false mysql

3)把刚创建的mysql用户加入到mysql组下
chown -R mysql:mysql ./
# 更改所属的组和用户
[root@cdh03 ~]# cd /usr/local/
[root@cdh03 local]# chown -R mysql mysql/
[root@cdh03 local]# chgrp -R mysql mysql/
[root@cdh03 local]# cd mysql/
[root@cdh03 mysql]# mkdir data
[root@cdh03 mysql]# chown -R mysql:mysql data 

5、创建mysql 安装初始化配置文件

vi /etc/my.cnf
[mysql]
socket=/usr/local/mysql/mysql.sock
# set mysql client default chararter
default-character-set=utf8

[mysqld]
socket=/usr/local/mysql/mysql.sock
# set mysql server port  
port =3306 
# 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

transaction-isolation = READ-COMMITTED
symbolic-links = 0
key_buffer_size = 32M
max_allowed_packet = 32M
thread_stack = 256K
thread_cache_size = 64
query_cache_limit = 8M
query_cache_size = 64M
query_cache_type = 1

max_connections = 550

#log_bin=/usr/local/mysql/mysql_binary_log

server_id=1

binlog_format = mixed

read_buffer_size = 2M
read_rnd_buffer_size = 16M
sort_buffer_size = 8M
join_buffer_size = 8M

innodb_file_per_table = 1
innodb_flush_log_at_trx_commit  = 2
innodb_log_buffer_size = 64M
innodb_buffer_pool_size = 4G
innodb_thread_concurrency = 8
innodb_flush_method = O_DIRECT
innodb_log_file_size = 512M

[mysql.server]
#user=mysql
basedir=/usr/local/mysql

 

6、安装和初始化

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

设置目录及目录权限

[root@cdh03 mysql]# cp ./support-files/mysql.server /etc/init.d/mysqld
[root@cdh03 mysql]# chown 777 my.cnf
[root@cdh03 mysql]# ls
bin  COPYING  data  docs  include  lib  man  my.cnf  README  share  support-files
[root@cdh03 mysql]# ls -l
total 60
drwxr-xr-x  2 root  root   4096 Mar  8 15:56 bin
-rw-r--r--  1  7161 31415 17987 Dec 21 18:39 COPYING
drwxr-x---  5 mysql mysql  4096 Mar  8 16:21 data
drwxr-xr-x  2 root  root   4096 Mar  8 15:56 docs
drwxr-xr-x  3 root  root   4096 Mar  8 15:56 include
drwxr-xr-x  5 root  root   4096 Mar  8 15:56 lib
drwxr-xr-x  4 root  root   4096 Mar  8 15:56 man
-rw-r--r--  1   777 root    516 Mar  8 16:19 my.cnf
-rw-r--r--  1  7161 31415  2478 Dec 21 18:39 README
drwxr-xr-x 28 root  root   4096 Mar  8 15:56 share
drwxr-xr-x  2 root  root   4096 Mar  8 15:56 support-files
[root@cdh03 mysql]# chmod +x /etc/init.d/mysqld
[root@cdh03 mysql]# mkdir data
[root@cdh03 mysql]# chown -R mysql:mysql data
[root@cdh03 mysql]#

7、启动msql

#启动mysql
[root@cdh03 software]# /etc/init.d/mysqld restart
MySQL server PID file could not be found!                  [失败]
Starting MySQL..The server quit without updating PID file ([失败]ocal/mysql/data/cdh03.pid).

#出现错误,解决方案如下:
#找到是否已经有进程占用
[root@cdh03 mysql]# ps -ef|grep mysql

#关闭找到的进程
[root@cdh03 software]# kill -9 9682 9848

# 重启mysql
[root@cdh03 mysql]# /etc/init.d/mysqld restart
Shutting down MySQL...                                     [  确定  ]
Starting MySQL....                                         [  确定  ]
[root@cdh03 mysql]# 

8、设置开机启动

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

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

mysqld             0:关    1:关    2:开    3:开    4:开    5:开    6:关
[root@cdh03 mysql]# service mysqld status
MySQL running (13894)                                      [  确定  ]
[root@cdh03 mysql]# chmod +x /etc/rc.d/init.d/mysqld
[root@cdh03 mysql]#  chkconfig --add mysqld
[root@cdh03 mysql]#  chkconfig --list mysqld

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

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

9、修改配置文件

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

10、获取mysl初始密码

1)获取mysql初始密码

[root@cdh03 mysql]# cat /root/.mysql_secret
# Password set for user 'root@localhost' at 2020-08-11 09:38:00 
Gm,tJEJ_0g%h
[root@cdh03 mysql]#

2)修改密码

[root@cdh03 mysql]# mysql -uroot -p
Enter password:   使用刚刚查询到的密码
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.30

Copyright (c) 2000, 2020, 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('root');
Query OK, 0 rows affected, 1 warning (0.00 sec)

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

mysql> exit
Bye
[root@cdh03 mysql]#

11、使用新密码登录

[root@cdh03 mysql]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.30 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, 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 databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql> 

12、添加远程访问权限

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> 

 

 

 

 

 

posted @ 2020-08-11 10:35  桥头堡洗脚城  阅读(298)  评论(0编辑  收藏  举报