当前位置:   article > 正文

数据库:Centos7安装解压版mysql5.7图文教程,亲测成功_centos安装mysql5.7

centos安装mysql5.7

目录

1、卸载Centos7默认自带的mariadb数据库,避免冲突

2、下载解压版mysql并安装

3、配置mysql

4、mysql客户端访问


Centos7安装mysql5.7解压版完整教程避免踩坑,可以把数据目录和系统目录分开设置。

1、卸载Centos7默认自带的mariadb数据库,避免冲突

  1. #先查询是否安装,找到已安装的对应mariadb,
  2. yum list installed |grep mariadb
  3. #列表展示的是mariadb-libs.x86_64 ,执行如下命令进行安装
  4. yum remove mariadb-libs.x86_64

2、下载解压版mysql并安装

下载mysql5.7解压版,我下载的版本mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz

MySQL :: Download MySQL Community Server (Archived Versions)

  1. #创建MySQL上传目录
  2. mkdir /opt/tools
  3. #然后登录Linux服务器,将下载好的安装包上传到服务器的/opt/tools目录。执行解压命令
  4. cd /opt/tools
  5. tar -zxf /data/tools/mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz
  6. #解压后的目录改名
  7. mv mysql-5.7.27-linux-glibc2.12-x86_64 mysql
  8. #移动mysql到 /usr/local/mysql
  9. mv mysql-5.7.27-linux-glibc2.12-x86_64 /usr/local/mysql
  10. cd /usr/local/mysql
  11. #创建用户组和用户 mysql
  12. groupadd mysql
  13. useradd -r -g mysql mysql
  14. #目录授权
  15. chgrp -R mysql .
  16. chown -R mysql .
  17. #创建MySQL存储数据的目录
  18. mkdir /data/mysql/data
  19. mkdir /data/mysql/share
  20. 进入bin目录执行初始化
  21. cd /usr/local/mysql/bin
  22. ./mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/mysql/data/ --lc_messages_dir=/data/mysql/share --lc_messages=en_US

注意:执行完成后输出的内容最后一行是数据库root的密码,一定要先保存下来

进入support-files,修改mysql.server

cd /usr/local/mysql/support-files

  1. #!/bin/sh
  2. # Copyright Abandoned 1996 TCX DataKonsult AB & Monty Program KB & Detron HB
  3. # This file is public domain and comes with NO WARRANTY of any kind
  4. # MySQL daemon start/stop script.
  5. # Usually this is put in /etc/init.d (at least on machines SYSV R4 based
  6. # systems) and linked to /etc/rc3.d/S99mysql and /etc/rc0.d/K01mysql.
  7. # When this is done the mysql server will be started when the machine is
  8. # started and shut down when the systems goes down.
  9. # Comments to support chkconfig on RedHat Linux
  10. # chkconfig: 2345 64 36
  11. # description: A very fast and reliable SQL database engine.
  12. # Comments to support LSB init script conventions
  13. ### BEGIN INIT INFO
  14. # Provides: mysql
  15. # Required-Start: $local_fs $network $remote_fs
  16. # Should-Start: ypbind nscd ldap ntpd xntpd
  17. # Required-Stop: $local_fs $network $remote_fs
  18. # Default-Start: 2 3 4 5
  19. # Default-Stop: 0 1 6
  20. # Short-Description: start and stop MySQL
  21. # Description: MySQL is a very fast and reliable SQL database engine.
  22. ### END INIT INFO
  23. # If you install MySQL on some other places than /usr/local/mysql, then you
  24. # have to do one of the following things for this script to work:
  25. #
  26. # - Run this script from within the MySQL installation directory
  27. # - Create a /etc/my.cnf file with the following information:
  28. # [mysqld]
  29. # basedir=<path-to-mysql-installation-directory>
  30. # - Add the above to any other configuration file (for example ~/.my.ini)
  31. # and copy my_print_defaults to /usr/bin
  32. # - Add the path to the mysql-installation-directory to the basedir variable
  33. # below.
  34. #
  35. # If you want to affect other MySQL variables, you should make your changes
  36. # in the /etc/my.cnf, ~/.my.cnf or other MySQL configuration files.
  37. # If you change base dir, you must also change datadir. These may get
  38. # overwritten by settings in the MySQL configuration files.
  39. basedir=
  40. datadir=/data/mysql/data
  41. # Default value, in seconds, afterwhich the script should timeout waiting
  42. # for server start.
  43. # Value here is overriden by value in my.cnf.
  44. # 0 means don't wait at all
  45. # Negative numbers mean to wait indefinitely
  46. service_startup_timeout=900
  47. # Lock directory for RedHat / SuSE.
  48. lockdir='/var/lock/subsys'
  49. lock_file_path="$lockdir/mysql"
  50. # The following variables are only set for letting mysql.server find things.
  51. # Set some defaults
  52. mysqld_pid_file_path=
  53. if test -z "$basedir"
  54. then
  55. basedir=/usr/local/mysql
  56. bindir=/usr/local/mysql/bin
  57. if test -z "$datadir"
  58. then
  59. datadir=/data/mysql/data
  60. fi
  61. sbindir=/usr/local/mysql/bin
  62. libexecdir=/usr/local/mysql/bin
  63. else
  64. bindir="$basedir/bin"
  65. if test -z "$datadir"
  66. then
  67. datadir="/data/mysql/data"
  68. fi
  69. sbindir="$basedir/sbin"
  70. libexecdir="$basedir/libexec"
  71. fi
  72. # datadir_set is used to determine if datadir was set (and so should be
  73. # *not* set inside of the --basedir= handler.)
  74. datadir_set=
  75. #
  76. # Use LSB init script functions for printing messages, if possible
  77. #
  78. lsb_functions="/lib/lsb/init-functions"
  79. if test -f $lsb_functions ; then
  80. . $lsb_functions
  81. else
  82. log_success_msg()
  83. {
  84. echo " SUCCESS! $@"
  85. }
  86. log_failure_msg()
  87. {
  88. echo " ERROR! $@"
  89. }
  90. fi
  91. PATH="/sbin:/usr/sbin:/bin:/usr/bin:$basedir/bin"
  92. export PATH
  93. mode=$1 # start or stop
  94. [ $# -ge 1 ] && shift
  95. other_args="$*" # uncommon, but needed when called from an RPM upgrade action
  96. # Expected: "--skip-networking --skip-grant-tables"
  97. # They are not checked here, intentionally, as it is the resposibility
  98. # of the "spec" file author to give correct arguments only.
  99. case `echo "testing\c"`,`echo -n testing` in
  100. *c*,-n*) echo_n= echo_c= ;;
  101. *c*,*) echo_n=-n echo_c= ;;
  102. *) echo_n= echo_c='\c' ;;
  103. esac
  104. parse_server_arguments() {
  105. for arg do
  106. case "$arg" in
  107. --basedir=*) basedir=`echo "$arg" | sed -e 's/^[^=]*=//'`
  108. bindir="$basedir/bin"
  109. if test -z "$datadir_set"; then
  110. datadir="$basedir/data"
  111. fi
  112. sbindir="$basedir/sbin"
  113. libexecdir="$basedir/libexec"
  114. ;;
  115. --datadir=*) datadir=`echo "$arg" | sed -e 's/^[^=]*=//'`
  116. datadir_set=1
  117. ;;
  118. --pid-file=*) mysqld_pid_file_path=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
  119. --service-startup-timeout=*) service_startup_timeout=`echo "$arg" | sed -e 's/^[^=]*=//'` ;;
  120. esac
  121. done
  122. }
  123. wait_for_pid () {
  124. verb="$1" # created | removed
  125. pid="$2" # process ID of the program operating on the pid-file
  126. pid_file_path="$3" # path to the PID file.
  127. i=0
  128. avoid_race_condition="by checking again"
  129. while test $i -ne $service_startup_timeout ; do
  130. case "$verb" in
  131. 'created')
  132. # wait for a PID-file to pop into existence.
  133. test -s "$pid_file_path" && i='' && break
  134. ;;
  135. 'removed')
  136. # wait for this PID-file to disappear
  137. test ! -s "$pid_file_path" && i='' && break
  138. ;;
  139. *)
  140. echo "wait_for_pid () usage: wait_for_pid created|removed pid pid_file_path"
  141. exit 1
  142. ;;
  143. esac
  144. # if server isn't running, then pid-file will never be updated
  145. if test -n "$pid"; then
  146. if kill -0 "$pid" 2>/dev/null; then
  147. : # the server still runs
  148. else
  149. # The server may have exited between the last pid-file check and now.
  150. if test -n "$avoid_race_condition"; then
  151. avoid_race_condition=""
  152. continue # Check again.
  153. fi
  154. # there's nothing that will affect the file.
  155. log_failure_msg "555555The server quit without updating PID file ($pid_file_path)."
  156. return 1 # not waiting any more.
  157. fi
  158. fi
  159. echo $echo_n ".$echo_c"
  160. i=`expr $i + 1`
  161. sleep 1
  162. done
  163. if test -z "$i" ; then
  164. log_success_msg
  165. return 0
  166. else
  167. log_failure_msg
  168. return 1
  169. fi
  170. }
  171. # Get arguments from the my.cnf file,
  172. # the only group, which is read from now on is [mysqld]
  173. if test -x "$bindir/my_print_defaults"; then
  174. print_defaults="$bindir/my_print_defaults"
  175. else
  176. # Try to find basedir in /etc/my.cnf
  177. conf=/etc/my.cnf
  178. print_defaults=
  179. if test -r $conf
  180. then
  181. subpat='^[^=]*basedir[^=]*=\(.*\)$'
  182. dirs=`sed -e "/$subpat/!d" -e 's//\1/' $conf`
  183. for d in $dirs
  184. do
  185. d=`echo $d | sed -e 's/[ ]//g'`
  186. if test -x "$d/bin/my_print_defaults"
  187. then
  188. print_defaults="$d/bin/my_print_defaults"
  189. break
  190. fi
  191. done
  192. fi
  193. # Hope it's in the PATH ... but I doubt it
  194. test -z "$print_defaults" && print_defaults="my_print_defaults"
  195. fi
  196. #
  197. # Read defaults file from 'basedir'. If there is no defaults file there
  198. # check if it's in the old (depricated) place (datadir) and read it from there
  199. #
  200. extra_args=""
  201. if test -r "$basedir/my.cnf"
  202. then
  203. extra_args="-e $basedir/my.cnf"
  204. fi
  205. parse_server_arguments `$print_defaults $extra_args mysqld server mysql_server mysql.server`
  206. #
  207. # Set pid file if not given
  208. #
  209. if test -z "$mysqld_pid_file_path"
  210. then
  211. mysqld_pid_file_path=$datadir/`hostname`.pid
  212. else
  213. case "$mysqld_pid_file_path" in
  214. /* ) ;;
  215. * ) mysqld_pid_file_path="$datadir/$mysqld_pid_file_path" ;;
  216. esac
  217. fi
  218. case "$mode" in
  219. 'start')
  220. # Start daemon
  221. # Safeguard (relative paths, core dumps..)
  222. cd $basedir
  223. # 重启sql
  224. echo $echo_n "Starting MySQL"
  225. if test -x $bindir/mysqld_safe
  226. then
  227. # Give extra arguments to mysqld with the my.cnf file. This script
  228. # may be overwritten at next upgrade.
  229. $bindir/mysqld_safe --datadir="$datadir" --pid-file="$mysqld_pid_file_path" $other_args >/dev/null &
  230. wait_for_pid created "$!" "$mysqld_pid_file_path"; return_value=$?
  231. # Make lock for RedHat / SuSE
  232. if test -w "$lockdir"
  233. then
  234. touch "$lock_file_path"
  235. fi
  236. exit $return_value
  237. else
  238. log_failure_msg "Couldn't find MySQL server ($bindir/mysqld_safe)"
  239. fi
  240. ;;
  241. 'stop')
  242. # Stop daemon. We use a signal here to avoid having to know the
  243. # root password.
  244. if test -s "$mysqld_pid_file_path"
  245. then
  246. # signal mysqld_safe that it needs to stop
  247. touch "$mysqld_pid_file_path.shutdown"
  248. mysqld_pid=`cat "$mysqld_pid_file_path"`
  249. if (kill -0 $mysqld_pid 2>/dev/null)
  250. then
  251. echo $echo_n "Shutting down MySQL"
  252. kill $mysqld_pid
  253. # mysqld should remove the pid file when it exits, so wait for it.
  254. wait_for_pid removed "$mysqld_pid" "$mysqld_pid_file_path"; return_value=$?
  255. else
  256. log_failure_msg "MySQL server process #$mysqld_pid is not running!"
  257. rm "$mysqld_pid_file_path"
  258. fi
  259. # Delete lock for RedHat / SuSE
  260. if test -f "$lock_file_path"
  261. then
  262. rm -f "$lock_file_path"
  263. fi
  264. exit $return_value
  265. else
  266. log_failure_msg "MySQL server PID file could not be found!"
  267. fi
  268. ;;
  269. 'restart')
  270. # Stop the service and regardless of whether it was
  271. # running or not, start it again.
  272. if $0 stop $other_args; then
  273. $0 start $other_args
  274. else
  275. log_failure_msg "Failed to stop running server, so refusing to try to start."
  276. exit 1
  277. fi
  278. ;;
  279. 'reload'|'force-reload')
  280. if test -s "$mysqld_pid_file_path" ; then
  281. read mysqld_pid < "$mysqld_pid_file_path"
  282. kill -HUP $mysqld_pid && log_success_msg "Reloading service MySQL"
  283. touch "$mysqld_pid_file_path"
  284. else
  285. log_failure_msg "MySQL PID file could not be found!"
  286. exit 1
  287. fi
  288. ;;
  289. 'status')
  290. # First, check to see if pid file exists
  291. if test -s "$mysqld_pid_file_path" ; then
  292. read mysqld_pid < "$mysqld_pid_file_path"
  293. if kill -0 $mysqld_pid 2>/dev/null ; then
  294. log_success_msg "MySQL running ($mysqld_pid)"
  295. exit 0
  296. else
  297. log_failure_msg "MySQL is not running, but PID file exists"
  298. exit 1
  299. fi
  300. else
  301. # Try to find appropriate mysqld process
  302. mysqld_pid=`pidof $libexecdir/mysqld`
  303. # test if multiple pids exist
  304. pid_count=`echo $mysqld_pid | wc -w`
  305. if test $pid_count -gt 1 ; then
  306. log_failure_msg "Multiple MySQL running but PID file could not be found ($mysqld_pid)"
  307. exit 5
  308. elif test -z $mysqld_pid ; then
  309. if test -f "$lock_file_path" ; then
  310. log_failure_msg "MySQL is not running, but lock file ($lock_file_path) exists"
  311. exit 2
  312. fi
  313. log_failure_msg "MySQL is not running"
  314. exit 3
  315. else
  316. log_failure_msg "MySQL is running but PID file could not be found"
  317. exit 4
  318. fi
  319. fi
  320. ;;
  321. *)
  322. # usage
  323. basename=`basename "$0"`
  324. echo "Usage: $basename {start|stop|restart|reload|force-reload|status} [ MySQL server options ]"
  325. exit 1
  326. ;;
  327. esac
  328. exit 0

注意:修改mysql.server 文件里面数据目录为 /data/mysql/data/ 一定要正确设置

  1. #复制mysql启动文件到服务文件夹
  2. cp mysql.server /etc/init.d/mysql
  3. #通过服务命令启动mysql
  4. service mysql start
  5. #输出 success 表示启动成功
  6. #设置开机自启
  7. /sbin/chkconfig mysql on
  8. systemctl enable mysql
  9. #查看自启动配置
  10. /sbin/chkconfig --list

技巧:添加 mysql 软连接,方面在任何目录执行MySQL命令

 ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql

说明:如果需要额外配置参数,默认没有/etc/my.cnf 文件内容如下

vim /etc/my.cnf 

比如需要新增大小写不敏感配置,其余配置可根据业务需要进行配置

  1. [mysqld]
  2. lower_case_table_names = 1
然后保存后重启mysql服务
  1. service mysql stop
  2. service mysql start

3、配置mysql

  1. mysql -u root -p
  2. #输入初始化的密码
  3. #修改密码
  4. set password=password(“12345678”);
  5. #设置任意IP都能通过root用户访问该数据库
  6. GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '12345678' WITH GRANT OPTION;
  7. #刷新权限
  8. flush privileges;
  9. #重启mysql
  10. service mysql restart
  11. #设置防火墙
  12. firewall-cmd --zone=public --add-port=3306/tcp --permanent
  13. systemctl restart firewalld

4、mysql客户端访问

然后使用本地MySQL客户端访问,确认是否可以正常登录。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/799824
推荐阅读
相关标签
  

闽ICP备14008679号