使用CentOS7安装MySQL的两种方法

avatar 2019年9月10日14:40:20 评论 1,213 次浏览

首先说一下mysql的安装方式,分别为yum安装和源码包安装,两者的区别是源码包安装需要编译,yum安装不需要编译。在安装之前首先确认系统已经卸载干净,或者没有安装过mysql,这样会避免一些没有必要的麻烦。检查是否安装过mysql:

rpm -qa | grep mysql

这只是源码包安装的mysql查看方法,如果是yum安装的就不能使用rpm查看了。下面是针对rpm安装和yum安装的卸载方法:

rpm -e mysql-libs-5.1.71-1.el6.x86_64   //普通删除模式
rpm -e --nodeps mysql-libs-5.1.71-1.el6.x86_64    // 强力删除模式,如果使用上面命令删除时,提示有依赖的其它文件,则用该命令可以对其进行强力删除
或者 
yum remove mysql-libs-5.1.71-1.el6.x86_64    //yum卸载

安装准备

在安装之前需要下载文件,目前很多大公司都有自己的软件库,并对外开放,可以在网上搜索一下,这里说简单说一下下载方法:

1、下载地址:http://ftp.ntu.edu.tw/MySQL/Downloads/MySQL-5.6/
   文件名:mysql-5.6.44-linux-glibc2.12-x86_64.tar.gz  文件名有glibc的为免编译的二进制安装包
2、http://dev.mysql.com/downloads/mysql/
Select Platform:  选择 -->  linux - Generic   然后选择 (mysql-5.6.44-linux-glibc2.12-x86_64.tar.gz)

一、编译安装

1、下载文件

wget http://ftp.ntu.edu.tw/MySQL/Downloads/MySQL-5.6/mysql-5.6.44.tar.gz
tar -zxf mysql-5.6.44.tar.gz 
mv mysql-5.6.44 mysql
mv mysql /usr/local/
useradd -s  /sbin/nologin mysql
mkdir /var/lib/mysql/

2、安装编译代码需要的包

yum -y install make gcc-c++ cmake bison-devel  ncurses-devel openssl-devel bison
cd /usr/local/mysql
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DSYSCONFDIR=/etc \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_MEMORY_STORAGE_ENGINE=1 \
-DWITH_READLINE=1 \
-DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock \
-DMYSQL_TCP_PORT=3306 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci
make && make install

3、修改配置和启动脚本

chown -R mysql:mysql /usr/local/mysql
cp support-files/my-default.cnf /etc/my.cnf
cp support-files/mysql.server /etc/init.d/mysqld
chmod a+x /etc/init.d/mysqld
chkconfig  --level 345 mysqld on
vim /etc/my.cnf
......................
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306

也可以这样自定义初始化

scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql --ldata=/var/lib/mysql

注:mysql5.6一下包括5.6版本建议使用mysql_install_db,以上建议改成 mysqld --initialize 完成实例初始化。

初始化之后可以启动一下,主要是为了方便启动的时候会自动生成mysql相关的文件,避免配置之后启动生成失败。这个是需要重点注意的,避免踩坑。

4、修改配置文件并启动脚本

cp support-files/my-default.cnf /etc/my.cnf
cp: overwrite ‘/etc/my.cnf’? y
vim /etc/my.cnf //编辑或者修改
[mysqld]       // 在这里修改
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
socket = /tmp/mysql.sock     //注意路径不能出错,否则启动出错 上次少了/
cp support-files/mysql.server /etc/init.d/mysqld

5、配置环境变量

vim /etc/profile
export PATH=/usr/local/mysql/bin/:$PATH
source /etc/profile
chmod +x /usr/local/mysql/scripts/mysql_install_db
/usr/local/mysql/scripts/mysql_install_db --user=mysql --defaults-file=/etc/my.cnf --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
-bash: /usr/local/mysql/scripts/mysql_install_db: Permission denied

6、启动服务

安全模式启动以下

/usr/local/mysql/bin/mysqld_safe &

使用脚本启动

/etc/init.d/mysqld restart

加入开机启动

chkconfig --add mysqld
chkconfig mysqld on
service mysqld start

注:在启动mysql的时候出现了重启更新不了(he server quit without updating PID file (/usr/local/mysql/data/MySql.pid).).把selinex关闭掉重启一下,重新初始化。

7、设置mysql的root密码

vim /etc/my.cnf
在[mysqld]下面增加一行
skip-grant-tables

重启mysql

/etc/init.d/mysqld restart 
/usr/local/mysql/bin/mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.44 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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> 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 Password = PASSWORD('root') where User ='root';
Query OK, 4 rows affected (0.01 sec)
Rows matched: 4  Changed: 4  Warnings: 0

退出mysql,然后把增加的skip-grant-tables注释掉,然后重启一下mysql

/etc/init.d/mysqld restart          
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 
 /usr/local/mysql/bin/mysql -uroot -p
Enter password:    #刚刚输入的root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.44 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, 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 grants for 'root'@'127.0.0.1';  #查看远程连接的用户
+----------------------------------------------------------------------------------------------------------------------------------------+
| Grants for root@127.0.0.1                                                                                                              |
+----------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' IDENTIFIED BY PASSWORD '*81F5E21E35407D884A6CD4A731AEBFB6AF209E1B' WITH GRANT OPTION |
+----------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> flush privileges;
mysql> quit

以上修改mysql密码方法也可以用到yum安装。

二、yum安装mysql

1、下载yum源并安装yum源

wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
yum -y install mysql57-community-release-el7-8.noarch.rpm

2、检查一下mysql源是否安装成功

yum repolist enabled | grep "mysql.*-community.*"

3、安装mysql

yum -y install mysql-community-server

后面可以跟版本号,默认会选择最新版本安装。

4、启动mysql

systemctl start mysqld

查看mysql的启动状态

systemctl status mysqld
mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled)
   Active: active (running) since Fri 2019-05-31 13:32:47 CST; 10s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 13019 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 12945 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 13023 (mysqld)
   CGroup: /system.slice/mysqld.service
           └─13023 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

May 31 13:32:45 MySql systemd[1]: Starting MySQL Server...
May 31 13:32:47 MySql systemd[1]: Started MySQL Server.

5、开启启动

systemctl enable mysqld
systemctl daemon-reload

6、修改root的本地密码

mysql安装之后,在/var/log/mysqld.log文件中给root生成了一个默认密码,如下:

more /var/log/mysqld.log
..............................
[Note] A temporary password is generated for root@localhost: lT%j:Ua0pgIu

问题扩展

mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

解决方法:

mysql>  alter user user() identified by "********"; #密码必须复杂
Query OK, 0 rows affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

两中mysql的安装方法已经安装完成,这里有几个注意实现:

1、mysql存储数据路径

2、mysql的日志存放路径

3、mysql的端口设置方法

这几个文件设置建议选择默认的设置,也可以根据自己的习惯选择,方便后期在使用的时候不至于找不到文件。

avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: