MySQL初体验--安装MySQL
Posted 满格
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL初体验--安装MySQL相关的知识,希望对你有一定的参考价值。
操作系统版本:redhat 6.7 64位
[[email protected] ~]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 6.7 (Santiago)
数据库版本:MySQL5.7
下载地址:https://downloads.mysql.com/archives/community/
我下载的包为:mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
(一)安装前的准备
(1)查看服务器上是否已经安装了MySQL。RedHat Linux系统安装时,如果选择了MySQL数据库,那么服务器上会存在MySQL数据库,需要先卸载。
# 查看是否存在mysql安装包 [[email protected] etc]# rpm -qa| grep mysql mysql-community-common-5.7.21-1.el7.x86_64 # 如果存在,需要先卸载 [[email protected] etc]# rpm -e mysql-community-common-5.7.21-1.el7.x86_64
(2)查看服务器上是否存在mariadb数据库。mariadb是MySQL的一个分支,需要卸载
[[email protected] mysql]# rpm -qa |grep mariadb
(3) 删除/etc/my.cnf文件。该文件类似Oracle的参数文件
[[email protected] mysql]# rm /etc/my.cnf rm: cannot remove `/etc/my.cnf‘: No such file or directory
(4)创建mysql用户来管理数据库
[[email protected] mysql]# cat /etc/passwd|grep mysql mysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bash [[email protected] mysql]# cat /etc/group |grep mysql mysql:x:27:
[[email protected] mysql]# groupadd mysql
[[email protected] mysql]# useradd -g mysql mysql
修改mysql用户密码:
[[email protected] mysql]# passwd mysql Changing password for user mysql. New password: BAD PASSWORD: it is too short BAD PASSWORD: is too simple Retype new password: passwd: all authentication tokens updated successfully.
(二)安装MySQL数据库
mysql安装路径:/usr/local/mysql/
数据存放位置::/usr/local/mysql/data
(1)解压MySQL安装包
[[email protected] mysql]# tar -xzvf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
(2)将解压后的文件拷贝到安装路径下
[[email protected] mysql]# mv mysql-5.7.24-linux-glibc2.12-x86_64 /usr/local/mysql/
(3)修改文件的权限
[[email protected] mysql]# cd /usr/local/ [[email protected] local]# ls bin etc games include lib lib64 libexec mysql sbin share src [[email protected] local]# chown -R mysql mysql/ [[email protected] local]# chgrp -R mysql mysql/
(4)创建存放data的文件夹
[[email protected] mysql]# mkdir data [[email protected] mysql]# chown -R mysql:mysql data
(5)在/etc下创建mysql的参数文件my.cnf
[[email protected] etc]# vim my.cnf
[mysql] # 设置mysql客户端默认字符集 default-character-set=utf8 [mysqld] skip-name-resolve # 设置3306端口 port = 3306 # 设置mysql的安装目录 basedir=/usr/local/mysql # 设置mysql数据库的数据的存放目录 datadir=/usr/local/mysql/data # 允许最大连接数 max_connections=200 # 服务端使用的字符集默认为8比特编码的latin1字符集 character-set-server=utf8 # 创建新表时将使用的默认存储引擎 default-storage-engine=INNODB lower_case_table_names=1 max_allowed_packet=16M
(6)安装数据库
[[email protected] bin]# pwd /usr/local/mysql/bin [[email protected] bin]# ./mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data 2019-04-20 22:17:08 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize 2019-04-20 22:17:14 [WARNING] The bootstrap log isn‘t empty: 2019-04-20 22:17:14 [WARNING] 2019-04-20T14:17:08.662276Z 0 [Warning] --bootstrap is deprecated. Please consider using 2019-04-20T14:17:08.663008Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5000) 2019-04-20T14:17:08.663018Z 0 [Warning] Changed limits: table_open_cache: 407 (requested 2000)
(6)设置启动项
[[email protected] mysql]# ls bin COPYING data docs include lib man README share support-files [[email protected] mysql]# cd ./support-files/mysql mysqld_multi.server mysql-log-rotate mysql.server [[email protected] mysql]# cp ./support-files/mysql.server /etc/init.d/mysqld [[email protected] mysql]# chown 777 /etc/my.cnf [[email protected] mysql]# chmod a+x /etc/init.d/mysqld
(7)开启数据库
[[email protected] mysql]# /etc/init.d/mysqld start Starting MySQL. [ OK ]
查看数据库状态
[[email protected] mysql]# service mysqld status MySQL running (39674) [ OK ] # 或者 [[email protected] mysql]# ps -ef|grep mysql mysql 39674 1 0 22:53 pts/0 00:00:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --log-error=mysql.err --pid-file=/usr/local/mysql/data/mysql.pid --port=3306 root 40719 3339 0 23:13 pts/0 00:00:00 grep mysql
(8)修改数据库的初始root密码
(8.1)查看初始密码
[[email protected] mysql]# cat /root/.mysql_secret # Password set for user ‘[email protected]‘ at 2019-04-20 22:17:08 bkro9k_?=jrM
(8.2)使用初始密码登录数据库,修改初始密码为123456
[[email protected] 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.24 mysql> set password=password(‘123456‘); Query OK, 0 rows affected, 1 warning (0.00 sec)
(9)确认数据库状态,运行正常
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec)
安装完成。
以上是关于MySQL初体验--安装MySQL的主要内容,如果未能解决你的问题,请参考以下文章