多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
服务器环境: ***** centos7 x64 需要安装mysql5.7+ ***** ## 1、卸载centos7自带的mariadb # 查看系统自带的Mariadb [root@CDH-141 ~]# rpm -qa|grep mariadb mariadb-libs-5.5.44-2.el7.centos.x86_64 # 卸载系统自带的Mariadb [root@CDH-141 ~]# rpm -e --nodeps mariadb-libs-5.5.44-2.el7.centos.x86_64 # 删除etc目录下的my.cnf [root@CDH-141 ~]# rm /etc/my.cnf ## 2、检查mysql是否存在 # 检查mysql是否存在 [root@CDH-141 ~]# rpm -qa | grep mysql [root@CDH-141 ~]# ## 3、查看用户和是否存在 #### 1、)检查mysql组和用户是否存在 ``` # 检查mysql组和用户是否存在,如无则创建 [root@CDH-141 ~]# cat /etc/group | grep mysql [root@CDH-141 ~]# cat /etc/passwd | grep mysql ``` #### 2、)若不存在,则创建mysql组和用户 # 创建mysql用户组 [root@CDH-141 ~]# groupadd mysql # 创建一个用户名为mysql的用户,并加入mysql用户组 [root@CDH-141 ~]# useradd -g mysql mysql # 制定password 为111111 [root@CDH-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. ## 4、下载mysql离线安装包tar文件 官网下载地址:[https://dev.mysql.com/downloads/mysql/5.7.html#downloads]() 版本选择,可以选择一下两种方式: 使用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) ## 5、上传下载的mysql安装包到服务器 /usr/local/ # 进入/usr/local/文件夹 [root@CDH-141 ~]# cd /usr/local/ # 解压mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz [root@CDH-141 local]# tar -zxvf mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz # 进入/usr/local下,将解压后的文件夹修改为mysql [root@CDH-141 local]# mv mysql-5.7.25-linux-glibc2.12-x86_64 mysql ## 6、更改所属用户组 # 更改所属的组和用户 [root@CDH-141 ~]# cd /usr/local/ [root@CDH-141 local]# chown -R mysql mysql/ [root@CDH-141 local]# chgrp -R mysql mysql/ [root@CDH-141 local]# cd mysql/ [root@CDH-141 mysql]# mkdir data [root@CDH-141 mysql]# chown -R mysql:mysql data ## 7、在 /etc/下创建my.cnf文件 mysql配置文件可放在 /etc/ 或者 /usr/local/etc 或者mysql目录下 # 进入/usr/local/mysql文件夹下 [root@CDH-141 ~]# cd /usr/local/mysql # 创建my.cnf文件 [root@CDH-141 mysql]# touch my.cnf #或者cd ''>my.conf # 编辑my.cnf [root@CDH-141 mysql]# vi my.conf ``` [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 = 3306 #默认是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]# ## 8、进入mysql文件夹,安装mysql ``` # 进入mysql [root@CDH-141 local]# cd /usr/local/mysql # 安装mysql [root@CDH-141 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 ``` 此处如果出现错误: 2021-06-30T18:14:43.919417Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5000) 2021-06-30T18:14:43.919426Z 0 [Warning] Changed limits: table_open_cache: 431 (requested 2000) 问题原因: 用户需要打开的文件数超过了上限,通过命令“ulimit -a”可查看如下信息: 可以看到该用户的最大打开文件数为1024个,而我启动的mysql服务需要打开15000个,所以出现了上述的警告。出现了这个警告后,mysql服务是正常可用的,只是性能没有达到最优。下面我们着手解决这个问题。 首先在文件sudo vi /etc/security/limits.conf中追加下面信息: root hard nofile 65535 ``` #* soft core 0 #* hard rss 10000 #@student hard nproc 20 #@faculty soft nproc 20 #@faculty hard nproc 50 #ftp hard nproc 0 #@student - maxlogins 4 root hard nofile 65535 ``` 其中root 为我当前使用的linux用户。 然后退出该终端,再重启一个终端,执行命令:ulimit -n 65535 重新安装,问题解决 设置文件及目录权限: [root@CDH-141 mysql]# cp ./support-files/mysql.server /etc/init.d/mysqld [root@CDH-141 mysql]# chown 777 my.cnf [root@CDH-141 mysql]# ls bin COPYING data docs include lib man my.cnf README share support-files [root@CDH-141 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@CDH-141 mysql]# chmod +x /etc/init.d/mysqld [root@CDH-141 mysql]# [root@CDH-141 mysql]# mkdir data [root@CDH-141 mysql]# [root@CDH-141 mysql]# chown -R mysql:mysql data [root@CDH-141 mysql]#` ``` ## 9、启动mysql # 启动mysql [root@CDH-141 mysql]# /etc/init.d/mysqld restart MySQL server PID file could not be found![FAILED] Starting MySQL.Logging to '/usr/local/mysql/data/CDH-141.err'. ..The server quit without updating PID file (/usr/local/mysql/data/CDH-141.pid).[FAILED] [root@CDH-141 mysql]# 、 出现错误解决方法 #找到是否已经有进程占用 [root@CDH-141 mysql]# ps aux|grep mysql root 32483 0.0 0.0 113252 1620 pts/0 S 18: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 18: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+ 18:12 0:00 grep --color=auto mysql #关闭进程 [root@CDH-141 mysql]# kill -9 32684 [root@CDH-141 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@CDH-141 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@CDH-141 mysql]# /etc/init.d/mysqld restart MySQL server PID file could not be found![FAILED] Starting MySQL..[ OK ] [root@CDH-141 mysql]# # 重启mysql [root@CDH-141 mysql]# /etc/init.d/mysqld restart Shutting down MySQL..[ OK ] Starting MySQL..[ OK ] [root@CDH-141 mysql]# 此时报错: ``` [root@localhost etc]# /etc/init.d/mysqld restart ERROR! MySQL server PID file could not be found! Starting MySQL.Logging to '/usr/local/mysql/data/localhost.localdomain.err'. 2021-07-01T06:27:54.013397Z mysqld_safe Directory '/var/lib/mysql' for UNIX socket file don't exists. ERROR! The server quit without updating PID file (/usr/local/mysql/data/localhost.localdomain.pid). ``` #### 解决方法: mkdir /var/lib/mysql chmod 777 /var/lib/mysql ## 10、设置开机启动 [root@CDH-141 mysql]# chkconfig --level 35 mysqld on [root@CDH-141 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]# chmod +x /etc/rc.d/init.d/mysqld [root@CDH-141 mysql]# chkconfig --add mysqld [root@CDH-141 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]# ## 11、修改配置文件 # 进入/etc/profile文件夹 [root@CDH-141 mysql]# vim /etc/profile 修改/etc/profile,在最后添加如下内容 # 修改/etc/profile文件 #set mysql environment export PATH=$PATH:/usr/local/mysql/bin # 使文件生效 [root@CDH-141 mysql]# source /etc/profile ## 12、获取mysql root初始密码 [root@CDH-141 mysql]# cat /root/.mysql_secret # Password set for user 'root@localhost' at 2019-03-08 17:40:42 poc3u0mO_luv [root@CDH-141 mysql]# 修改密码后尝试登陆mysql,出现错误: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) 解决步骤: 1)检查服务有没有启动 2)在my.cnf文件中查看socket参数指定的路径,查看这个路径有没有访问权限 3)到那个路径下去看一下到底有没有这个mysql.sock文件,如果该路径下没有sock文件,我们先用find命令找出这个文件的位置,如果find也找不到,重启一下mysql服务即可,会自动生成一个。然后把他复制到soctek参数指定的路径下去。或者建立一个软连接,这也是比较推荐的方法,比如这个文件在/var/lib/mysql.sock,socket参数指定的路径是/tmp/mysql.sock。 我们就可以这样创建: ln -s /var/lib/mysql.sock /tmp/mysql.sock 创建完之后,再尝试连接 另外需要注意的是,mysql.sock文件默认是在/tmp下,数据库启动的时候,系统也默认去这个文件下找mysql.sock文件,但是/tmp目录有时会被某个定时任务给清除,那么我们可以给/tmp目录加一个sticky权限,保护其不被删除,chmod +t /tmp即可,使得/tmp下的文件只能由文件所有者和root用户才能删除 ## 13、修改密码 [root@CDH-141 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 ## 14、验证修改密码是否成功 [root@CDH-141 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> ## 15、添加远程访问权限 # 添加远程访问权限 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> # 16、重启mysql时配置生效 # 重启mysql [root@CDH-141 mysql]# /etc/init.d/mysqld restart Shutting down MySQL..[ OK ] Starting MySQL..[ OK ] [root@CDH-141 mysql]# *********************************************************** **测试远程连接时,发现无法连接mysql,可尝试防火墙开放3306端口** *********************************************************** ``[root@localhost ~]# firewall-cmd --zone=public --add-port=3306/tcp --permanent success` **命令含义:** \--zone #作用域 \--add-port=3306/tcp #添加端口,格式为:端口/通讯协议 \--permanent #永久生效,没有此参数重启后失效 #### 重启防火墙 `[root@localhost ~]# systemctl restart firewalld.service` linux下mysql开启远程访问权限及防火墙开放3306端口 1、登陆mysql mysql -u root -p 2、设置访问地址 如果你想允许用户root从ip为192.168.1.123的主机连接到mysql服务器,并使用root作为密码 ``GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.1.123'IDENTIFIED BY 'password' WITH GRANT OPTION; `` 3、刷新 `flush privileges;` 防火墙开启 1、开启端口3306 `firewall-cmd --zone=public --add-port=3306/tcp --permanent` 2、重启防火墙 `firewall-cmd --reload` 查看已经开放的端口: `firewall-cmd --list-ports`