一、准备工作
1.下载mysql安装包并传到服务器上进行解压后得到如下一系列安装包:
[root@HZHG-P mysql]# ls data mysql-community-libs-5.7.19-1.el6.x86_64.rpm mysql-community-client-5.7.19-1.el6.x86_64.rpm mysql-community-libs-compat-5.7.19-1.el6.x86_64.rpm mysql-community-common-5.7.19-1.el6.x86_64.rpm mysql-community-server-5.7.19-1.el6.x86_64.rpm mysql-community-devel-5.7.19-1.el6.x86_64.rpm //由于是采用编译式安装,所以是rpm包的形式
2.安装前检查下是否有操作系统自带的mysql版本,如果有的话现卸载:
[root@HZHG-P mysql]# rpm -qa|grep mysql mysql-libs-5.1.71-1.el6.x86_64 [root@HZHG-P mysql]# rpm -ev mysql-libs-5.1.71-1.el6.x86_64 --nodeps //--nodeps表示强制删除,忽略依赖 [root@HZHG-P mysql]# rpm -qa|grep mysql //再次检查,无任何输出,表示已卸载 [root@HZHG-P mysql]#
二、安装:
1.安装rpm包
[root@HZHG-P mysql]# rpm -ivh mysql-community-libs-5.7.19-1.el6.x86_64.rpm mysql-community-client-5.7.19-1.el6.x86_64.rpm mysql-community-libs-compat-5.7.19-1.el6.x86_64.rpm mysql-community-common-5.7.19-1.el6.x86_64.rpm mysql-community-server-5.7.19-1.el6.x86_64.rpm mysql-community-devel-5.7.19-1.el6.x86_64.rpm warning: mysql-community-libs-5.7.19-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY error: Failed dependencies: libnuma.so.1()(64bit) is needed by mysql-community-server-5.7.19-1.el6.x86_64 libnuma.so.1(libnuma_1.1)(64bit) is needed by mysql-community-server-5.7.19-1.el6.x86_64 libnuma.so.1(libnuma_1.2)(64bit) is needed by mysql-community-server-5.7.19-1.el6.x86_64 //报错 [root@HZHG-P mysql]# yum install numactl //解决方法 [root@HZHG-P mysql]# rpm -ivh mysql-community-libs-5.7.19-1.el6.x86_64.rpm mysql-community-client-5.7.19-1.el6.x86_64.rpm mysql-community-libs-compat-5.7.19-1.el6.x86_64.rpm mysql-community-common-5.7.19-1.el6.x86_64.rpm mysql-community-server-5.7.19-1.el6.x86_64.rpm mysql-community-devel-5.7.19-1.el6.x86_64.rpm warning: mysql-community-libs-5.7.19-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY Preparing... ########################################### [100%] 1:mysql-community-common ########################################### [ 17%] 2:mysql-community-libs ########################################### [ 33%] 3:mysql-community-client ########################################### [ 50%] 4:mysql-community-server ########################################### [ 67%] 5:mysql-community-libs-co########################################### [ 83%] 6:mysql-community-devel ########################################### [100%] //出现以上情况表示安装成功 由于这几个包有顺序依赖,所以建议全部一起安装,系统会自动识别
2.修改配置文件:
[root@HZHG-P mysql]# vi /etc/my.cnf //默认mysql配置文件路径 datadir=/backup/mysql/data //mysql数据存储目录 socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid log-bin=/backup/mysql/data/mysql-bin //开启binlog日志,为搭建主从做准备 server_id=123458 //设定server_id,为搭建主从做准备
3.启动mysql服务,初始化数据库和密码:
[root@HZHG-P mysql]# service mysqld start //启动mysql服务 Initializing MySQL database: [ OK ] Starting mysqld: [ OK ] [root@HZHG-P mysql]# mysqld --defaults-file=/etc/my.cnf --initialize-insecure --user=mysql 2019-04-17T07:08:18.494959Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2019-04-17T07:08:18.496950Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting. 2019-04-17T07:08:18.496973Z 0 [ERROR] Aborting /* 由于启动mysql服务时已经初始化了,导致配置文件中datadir路径下已经存在数据,该问题可以通过删除datadir路径下的所有东西来解决,初始化成功后可在mysql日志中 查看初始化的临时密码*/ [root@HZHG-P data]# rm -rf * /*执行该操作中在第一步启动mysql服务时提示了已初始化数据库,安装的时候没查看日志是否已初始化, 下次安装时可以先检查一下,再进行后续的删除文件,初始化步骤*/ [root@HZHG-P data]# mysqld --defaults-file=/etc/my.cnf --initialize-insecure --user=mysql //初始化 [root@HZHG-P data]# ls auto.cnf ibdata1 ib_logfile1 mysql-bin.000001 performance_schema ib_buffer_pool ib_logfile0 mysql mysql-bin.index sys //初始化成功后可在mysql日志中查看临时密码 [root@HZHG-P data]# vi /var/log/mysqld.log 2019-04-17T07:07:38.928820Z 1 [Note] A temporary password is generated for root@localhost: 9sqss4;Klp1y //该密码是初始化时随机生成的,只用于首次登陆 [root@HZHG-P data]# mysql -uroot -p9sqss4;Klp1y //登陆mysql Type 'help;' or 'h' for help. Type 'c' to clear the current input statement. mysql> set password = password('Password'); //设定密码 Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> grant all privileges on *.* to root@"%" identified by "Password" with grant option; //授权可远程登陆mysql Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> flush privileges; ERROR 1194 (HY000): Table 'user' is marked as crashed and should be repaired /*原因应该是在之前步骤中删除datadir路径下的所有文件, 导致user表被损坏,可用如下方式修复表*/ mysql> repair table user; //修复表 +------------+--------+----------+------------------------------------+ | Table | Op | Msg_type | Msg_text | +------------+--------+----------+------------------------------------+ | mysql.user | repair | warning | Number of rows changed from 3 to 4 | | mysql.user | repair | status | OK | +------------+--------+----------+------------------------------------+ mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
至此,rpm方式安装mysql成功。