千家信息网

Linux 7如何安装Mysql 5.7

发表于:2025-01-22 作者:千家信息网编辑
千家信息网最后更新 2025年01月22日,这篇文章主要为大家展示了"Linux 7如何安装Mysql 5.7",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"Linux 7如何安装Mysql 5.7"
千家信息网最后更新 2025年01月22日Linux 7如何安装Mysql 5.7

这篇文章主要为大家展示了"Linux 7如何安装Mysql 5.7",内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下"Linux 7如何安装Mysql 5.7"这篇文章吧。

在Oracle Linux 7.1中安装MySql 5.7 。mysql安装位置:/mysqlsoft/mysql,数据库文件数据位置:/mysqldata/mysql。
1.首先下载安装介质

mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz

2. 在根目录下创建文件夹mysqlsoft和数据库数据文件/mysqldata/mysql

[root@cs2 /]# mkdir -p /mysqlsoft[root@cs2 /]# mkdir -p /mysqldata/mysql[root@cs2 /]# ls -lrt /mysqldata/total 0drwxr-xr-x 2 root root 6 May 31 11:58 mysql

3.上传介质mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz到/mysqlsoft目录中并解压

[root@cs2 /]# cd mysqlsoft[root@cs2 mysqlsoft]# ls -lrttotal 628704-rw-r--r-- 1 root root 643790848 Apr 20  2018 mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz[root@cs2 mysqlsoft]# tar -zxvf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz[root@cs2 mysqlsoft]# ls -lrttotal 628704-rw-r--r-- 1 root root 643790848 Apr 20  2018 mysql-5.7.22-linux-glibc2.12-x86_64.tar.gzdrwxr-xr-x 9 root root       120 May 31 12:11 mysql-5.7.22-linux-glibc2.12-x86_64

建议一般不要修改默认文件名,通过软连接来完成

[root@cs2 mysqlsoft]# mv mysql-5.7.22-linux-glibc2.12-x86_64  mysql[root@cs2 mysqlsoft]# ls -lrttotal 628704-rw-r--r-- 1 root root 643790848 Apr 20  2018 mysql-5.7.22-linux-glibc2.12-x86_64.tar.gzdrwxr-xr-x 9 root root       120 May 31 13:28 mysql-5.7.22-linux-glibc2.12-x86_64lrwxrwxrwx 1 root root        35 May 31 13:33 mysql -> mysql-5.7.22-linux-glibc2.12-x86_64[root@cs2 mysqlsoft]# cd mysql[root@cs2 mysql]# lsbin  COPYING  docs  include  lib  man  README  share  support-files

4. 创建mysql用户与用户组

[root@cs2 mysql]# groupadd mysql[root@cs2 mysql]# useradd -r -g mysql -s /bin/false mysql

因为用户只用于所有权目的,而不是登录目的,useradd命令使用-r与-s /bin/false选项来创建一个用户没有登录服务器主机的权限。

5.修改/mysqlsoft/mysql与/mysqldata/mysql目录权限

[root@cs2 /]# chown -R mysql:mysql /mysqlsoft/mysql[root@cs2 /]# chown -R mysql:mysql /mysqldata/mysql[root@cs2 /]# chmod -R 775 /mysqlsoft/mysql[root@cs2 /]# chmod -R 775 /mysqldata/mysql

6. MySQL对于libaio库有依赖性。台果这个libaio库没有安装那么数据目录初始化与后续的数据库服务启动将会失败,安装libaio库执行以下操作:
查询是否安装了libaio库

[root@cs2 local]# yum search libaioLoaded plugins: langpacksRepodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast=========================================================================================================== N/S matched: libaio ============================================================================================================libaio.i686 : Linux-native asynchronous I/O access librarylibaio.x86_64 : Linux-native asynchronous I/O access librarylibaio-devel.i686 : Development files for Linux-native asynchronous I/O accesslibaio-devel.x86_64 : Development files for Linux-native asynchronous I/O access  Name and summary matches only, use "search all" for everything.

如果没有安装,可以执行下面的命令来安装

[root@cs2 local]# yum install libaioLoaded plugins: langpacksRepodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fastfile:///run/media/jy/OL-7.1%20Server.x86_64/repodata/repomd.xml: [Errno 14] curl#37 - "Couldn't open file /run/media/jy/OL-7.1%20Server.x86_64/repodata/repomd.xml"Trying other mirror.Package libaio-0.3.109-12.el7.x86_64 already installed and latest versionNothing to do

7.配置mysql参数
只是设置几个简单的mysql运行参数

[root@cs2 ~]# vi /mysqlsoft/mysql/my.cnf [mysqld]basedir=/mysqlsoft/mysqldatadir=/mysqldata/mysqlbind-address=0.0.0.0user=mysqlport=3306log-error=/mysqldata/mysql/mysql.errpid-file=/mysqldata/mysql/mysqld.pidsocket = /mysqldata/mysql/mysql.sockcharacter-set-server=utf8default-storage-engine=INNODBexplicit_defaults_for_timestamp = true"/mysqlsoft/mysql/my.cnf" [New] 67L, 1642C written

注意:log-error 一定要配置,因为如果mysql启动错误,可以从日志文件中找到错误原因。其次bind-address配置0.0.0.0是为了监听所有的连接。还有就是socket参数所指定的mysql.sock文件的路径最好设置为/tmp/mysql.sock,因为unix socket文件的缺省位置在/tmp目录中。

8.初始化mysql

[root@cs2 /]# cd /mysqlsoft/mysql/bin[root@cs2 bin]# ./mysqld --user=mysql  --defaults-file=/mysqlsoft/mysql/my.cnf --basedir=/mysqlsoft/mysql --datadir=/mysqldata/mysql --initialize[root@cs2 mysql]# cat mysql.err2019-05-31T06:01:50.260643Z 0 [Warning] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.2019-05-31T06:01:50.260731Z 0 [Warning] 'NO_AUTO_CREATE_USER' sql mode was not set. 100 1002019-05-31T06:01:53.795162Z 0 [Warning] InnoDB: New log files created, LSN=457902019-05-31T06:01:54.049268Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.2019-05-31T06:01:54.102171Z 0 [ERROR] unknown variable 'defaults-file=/mysqlsoft/mysql/my.cnf'2019-05-31T06:01:54.102193Z 0 [ERROR] Aborting

网上有人说是文件权限的问题,然后重新授权chmod 664 my.cnf 但并没有解决,有人建议调整一下参数顺序就好!!!最后调整了一下参数顺序果然有效:

[root@cs2 bin]# ./mysqld  --defaults-file=/mysqlsoft/mysql/my.cnf --initialize --basedir=/mysqlsoft/mysql --datadir=/mysqldata/mysql --user=mysql[root@cs2 mysql]# cat mysql.err2019-05-31T06:05:06.362925Z 0 [Warning] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.2019-05-31T06:05:06.362994Z 0 [Warning] 'NO_AUTO_CREATE_USER' sql mode was not set. 100 1002019-05-31T06:05:09.779913Z 0 [Warning] InnoDB: New log files created, LSN=457902019-05-31T06:05:10.026707Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.2019-05-31T06:05:10.094462Z 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: 0be6983a-836a-11e9-a341-005056a092af.2019-05-31T06:05:10.109209Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.2019-05-31T06:05:10.110107Z 1 [Note] A temporary password is generated for root@localhost: ,;pm93qnL%-j2019-05-31T06:05:14.966324Z 1 [Warning] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode.2019-05-31T06:05:14.966373Z 1 [Warning] 'user' entry 'mysql.session@localhost' ignored in --skip-name-resolve mode.2019-05-31T06:05:14.966391Z 1 [Warning] 'user' entry 'mysql.sys@localhost' ignored in --skip-name-resolve mode.2019-05-31T06:05:14.966419Z 1 [Warning] 'db' entry 'performance_schema mysql.session@localhost' ignored in --skip-name-resolve mode.2019-05-31T06:05:14.966428Z 1 [Warning] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.2019-05-31T06:05:14.966441Z 1 [Warning] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.2019-05-31T06:05:14.966493Z 1 [Warning] 'tables_priv' entry 'user mysql.session@localhost' ignored in --skip-name-resolve mode.2019-05-31T06:05:14.966508Z 1 [Warning] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.

其中[Note] A temporary password is generated for root@localhost: ,;pm93qnL%-j的root@localhost: 后面跟的是mysql数据库登录的临时密码,各人安装生成的临时密码不一样。可以看到到日志文件没有报错,而且有了临时密码,表示初始化成功。

9. 如果想服务能够部署自动支持安全连接,使用mysql_ssl_rsa_setup工具来创建缺省SSL与RSA文件

[root@cs2 bin]# ./mysql_ssl_rsa_setup --datadir=/mysqldata/mysqlGenerating a 2048 bit RSA private key......................................................................+++..............................................................+++writing new private key to 'ca-key.pem'-----Generating a 2048 bit RSA private key.............+++..............+++writing new private key to 'server-key.pem'-----Generating a 2048 bit RSA private key.....................................+++................................................+++writing new private key to 'client-key.pem'-----

9.启动mysql服务

[root@cs2 /]# sh /mysqlsoft/mysql/support-files/mysql.server start/mysqlsoft/mysql/support-files/mysql.server: line 239: my_print_defaults: command not found/mysqlsoft/mysql/support-files/mysql.server: line 259: cd: /usr/local/mysql: No such file or directoryStarting MySQL ERROR! Couldn't find MySQL server (/usr/local/mysql/bin/mysqld_safe)

启动mysql服务命令会报错,因为没有修改mysql的配置文件
修改Mysql配置文件,修改前为以下内容

if test -z "$basedir"then  basedir=/usr/local/mysql  bindir=/usr/local/mysql/bin  if test -z "$datadir"  then    datadir=/usr/local/mysql/data  fi  sbindir=/usr/local/mysql/bin  libexecdir=/usr/local/mysql/binelse  bindir="$basedir/bin"  if test -z "$datadir"  then    datadir="$basedir/data"  fi  sbindir="$basedir/sbin"  libexecdir="$basedir/libexec"fi

修改后的内容如下

[root@cs2 ~]# vi /mysqlsoft/mysql/support-files/mysql.serverif test -z "$basedir"then  basedir=/mysqlsoft/mysql  bindir=/mysqlsoft/mysql/bin  if test -z "$datadir"  then    datadir=/mysqldata/mysql  fi  sbindir=/mysqlsoft/mysql/bin  libexecdir=/mysqlsoft/mysql/binelse  bindir="$basedir/bin"  if test -z "$datadir"  then    datadir="$basedir/data"  fi  sbindir="$basedir/sbin"  libexecdir="$basedir/libexec"fi[root@cs2 ~]# cp /mysqlsoft/mysql/support-files/mysql.server  /etc/init.d/mysqld[root@cs2 ~]# chmod 755 /etc/init.d/mysqld

10.启动mysql

[root@cs2 ~]# service mysqld startStarting MySQL.. SUCCESS!

11.配置环境变量

[root@cs2 ~]# vi /etc/profile# /etc/profile# System wide environment and startup programs, for login setup# Functions and aliases go in /etc/bashrc# It's NOT a good idea to change this file unless you know what you# are doing. It's much better to create a custom.sh shell script in# /etc/profile.d/ to make custom changes to your environment, as this# will prevent the need for merging in future updates.export MYSQL_HOME=/mysqlsoft/mysql/export PATH=$PATH:$MYSQL_HOME/bin

11.登录Mysql
初始化成功后,查看初始化密码

[root@cs2 ~]# cat /mysqldata/mysql/mysql.err | grep password2019-05-31T06:05:10.110107Z 1 [Note] A temporary password is generated for root@localhost: ,;pm93qnL%-j

并输入刚刚复制的密码,但是 却提示不能通过mysql.sock文件实现连接

[root@cs2 bin]# ./mysqladmin -u root -p passwordEnter password: mysqladmin: connect to server at 'localhost' failederror: 'Can't connect to local MySQL server through socket '/mysqlsoft/mysql/mysql.sock' (2)'Check that mysqld is running and that the socket: '/mysqlsoft/mysql/mysql.sock' exists!

这里就奇怪了,因为在my.cnf文件中设置的socket文件路径为/mysqldata/mysql/mysql.sock,但mysql所使用的文件不是启动服务所生成的。

[root@cs2 ~]# cat /mysqlsoft/mysql/my.cnf[mysqld]basedir=/mysqlsoft/mysqldatadir=/mysqldata/mysqlbind-address=0.0.0.0user=mysqlport=3306log-error=/mysqldata/mysql/mysql.errpid-file=/mysqldata/mysql/mysqld.pidsocket = /mysqldata/mysql/mysql.sockcharacter-set-server=utf8default-storage-engine=INNODBexplicit_defaults_for_timestamp = true[root@cs2 mysql]# ls -lrttotal 286896-rw-r----- 1 mysql mysql 134217728 May 31 14:05 ib_logfile1-rw-r----- 1 mysql mysql        56 May 31 14:05 auto.cnfdrwxr-x--- 2 mysql mysql      8192 May 31 14:05 performance_schemadrwxr-x--- 2 mysql mysql      4096 May 31 14:05 mysqldrwxr-x--- 2 mysql mysql      8192 May 31 14:05 sys-rw------- 1 mysql mysql      1679 May 31 14:25 ca-key.pem-rw-r--r-- 1 mysql mysql      1107 May 31 14:25 ca.pem-rw------- 1 mysql mysql      1679 May 31 14:25 server-key.pem-rw-r--r-- 1 mysql mysql      1107 May 31 14:25 server-cert.pem-rw------- 1 mysql mysql      1679 May 31 14:25 client-key.pem-rw-r--r-- 1 mysql mysql      1107 May 31 14:25 client-cert.pem-rw------- 1 mysql mysql      1679 May 31 14:25 private_key.pem-rw-r--r-- 1 mysql mysql       451 May 31 14:25 public_key.pem-rw-r----- 1 mysql mysql       291 May 31 18:05 ib_buffer_pool-rw------- 1 mysql mysql         6 May 31 18:08 mysql.sock.locksrwxrwxrwx 1 mysql mysql         0 May 31 18:08 mysql.sock-rw-r----- 1 mysql mysql         6 May 31 18:08 mysqld.pid-rw-r----- 1 mysql mysql    101396 May 31 18:08 mysql.err-rw-r----- 1 mysql mysql  12582912 May 31 18:08 ibtmp1-rw-r----- 1 mysql mysql  12582912 May 31 18:08 ibdata1-rw-r----- 1 mysql mysql 134217728 May 31 18:08 ib_logfile0

可以看到在/var/lib/mysql目录中的mysql.sock是指向/mysqlsoft/mysql/mysql.sock文件的。

[mysql@cs2 ~]$ ls -lrt /var/lib/mysql 总用量 110604-rw-rw----. 1   27   27 50331648 10月 11 2017 ib_logfile1drwx------. 2   27   27     4096 10月 11 2017 performance_schemadrwx------. 2   27   27     4096 10月 11 2017 mysql-rw-rw----. 1   27   27       56 10月 11 2017 auto.cnf-rw-rw----. 1   27   27 50331648 5月  30 18:32 ib_logfile0-rw-rw----. 1   27   27 12582912 5月  30 18:32 ibdata1lrwxrwxrwx  1 root root       27 5月  31 15:44 mysql.sock -> /mysqlsoft/mysql/mysql.sock

如果使用-S选项来指定生成的mysql.sock文件进行登录是可以成功登录的

[mysql@cs2 mysql]$  mysql -S /mysqldata/mysql/mysql.sock -u root -pEnter password: Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 6Server version: 5.7.22 MySQL Community Server (GPL)Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> quit

修改socket文件路径为/mysqlsoft/mysql/mysql.sock

[root@cs2 ~]# cat /mysqlsoft/mysql/my.cnf[mysqld]basedir=/mysqlsoft/mysqldatadir=/mysqldata/mysqlbind-address=0.0.0.0user=mysqlport=3306log-error=/mysqldata/mysql/mysql.errpid-file=/mysqldata/mysql/mysqld.pidsocket = /mysqlsoft/mysql/mysql.sockcharacter-set-server=utf8default-storage-engine=INNODBexplicit_defaults_for_timestamp = true

再重启mysql服务

[root@cs2 ~]# service mysqld stopShutting down MySQL.. SUCCESS! [root@cs2 ~]# service mysqld startStarting MySQL.. SUCCESS![mysql@cs2 ~]$ mysql -u root -p Enter password: Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 4Server version: 5.7.22 MySQL Community Server (GPL)Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>

这就可以登录了。

12.重置root用户密码

[mysql@cs2 ~]$ mysql -u root -pEnter password: Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 2Server version: 5.7.22Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.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)

13.设置允许远程登录mysql
如果要远程访问数据库,只需要把拥有全部权限的root账号对应的记录的Host字段改为%就可以了

mysql> use mysql;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> update user set host='%' where user='root';Query OK, 1 row affected (0.00 sec)Rows matched: 1  Changed: 1  Warnings: 0mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)mysql> grant all privileges on *.* to root@'%';Query OK, 0 rows affected (0.00 sec)mysql> flush privileges;Query OK, 0 rows affected (0.00 sec)

测试远程登录

-bash-4.2$ mysql -h 10.11.13.19 -P 3306 -u root -pEnter password: Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 4Server version: 5.7.22 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 itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> use mysql;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> SELECT DISTINCT CONCAT('User: [', user, '''@''', host, '];') AS USER_HOST FROM user; +------------------------------------+| USER_HOST                          |+------------------------------------+| User: [root'@'%];                  || User: [mysql.session'@'localhost]; || User: [mysql.sys'@'localhost];     |+------------------------------------+3 rows in set (0.05 sec)

以上是"Linux 7如何安装Mysql 5.7"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

0