CentOs 安装MySql数据库
Posted 灵动思维文化传媒
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CentOs 安装MySql数据库相关的知识,希望对你有一定的参考价值。
周一至五早8点半!精品技术文章准时送上!
上篇我们说了在linux环境上配置Tomcat环境,具体参看《CentOs 安装Tomcat环境》,本篇我们来配置mysql数据库。
01
Centos7将默认数据库mysql替换成了Mariadb,所以先卸载掉
[root@instance-jbf586lj ~]# rpm -qa|grep mariadb mariadb-libs-5.5.44-2.el7.centos.x86_64 [root@instance-jbf586lj ~]# rpm -e --nodeps mariadb-libs-5.5.44-2.el7.centos.x86_64 |
02
[root@instance-jbf586lj ~]# rm -rf /etc/my.cnf |
03
[root@instance-jbf586lj ~]# groupadd mysql |
04
[root@instance-jbf586lj ~]# useradd -g mysql mysql |
05
06
07
[root@instance-jbf586lj ~]# tar -zxvf /mysql-5.6.41-linux-glibc2.12-x86_64.tar.gz -C /usr/local/java |
08
[root@instance-jbf586lj ~]# cd /usr/local/java [root@instance-jbf586lj java]# rename mysql-5.6.41-linux-glibc2.12-x86_64 mysql5.6 mysql-5.6.41-linux-glibc2.12-x86_64 |
09
[root@instance-jbf586lj java]# cd mysql5.6 [root@instance-jbf586lj mysql5.6]# cd support-files [root@instance-jbf586lj support-files]# cp my-default.cnf /etc/my.cnf |
[mysql] default-character-set=utf8 socket=/var/lib/mysql/mysql.sock [mysqld] skip-name-resolve port = 3306 socket=/var/lib/mysql/mysql.sock basedir=/usr/local/java/mysql5.6 datadir=/usr/local/java/mysql5.6/data max_connections=200 character-set-server=utf8 default-storage-engine=INNODB lower_case_table_name=1 max_allowed_packet=16M innodb_buffer_pool_size =50M key_buffer_size =12M |
10
-- 进入mysql安装的顶级目录 [root@instance-jbf586lj java]# cd mysql.5.6 -- 修改目录拥有者为mysql用户 [root@instance-jbf586lj mysql5.6]# chown -R mysql:mysql ./ |
11
[root@instance-jbf586lj mysql5.6]# ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/java/mysql5.6 --datadir=/usr/local/java/mysql5.6/data -- 可能报错 FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db: Data::Dumper -- 解决办法 yum -y install autoconf |
12
授予my.cnf最大权限
[root@instance-jbf586lj /]# cd ~ [root@instance-jbf586lj ~]# chmod 755 /etc/my.cnf |
设置开机自启动服务控制脚本
①复制启动脚本到资源目录
[root@instance-jbf586lj mysql5.6]# cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld |
②增加mysqld服务控制脚本执行权限
[root@instance-jbf586lj mysql5.6]# chmod +x /etc/rc.d/init.d/mysqld |
③将mysqld服务加入到系统服务
[root@instance-jbf586lj mysql5.6]# chkconfig --add mysqld |
④检查mysqld服务是否已经生效
[root@instance-jbf586lj mysql5.6]# chkconfig --list mysqld |
启动Mysql
[root@instance-jbf586lj mysql5.6]# service mysqld start [root@instance-jbf586lj mysql5.6]# service mysqld stop |
注:在启动过程中可能出现一下错误,是因为socket=/var/lib/mysql/mysql.sock配置的目录不存,如果存在就是没有权限就行读写操作。
.The server quit without updating PID file (/usr/local/ja[FAILED]5.6/data/instance-jbf586lj.pid)
解决办法:
在/var/lib添加mysql文件夹,并赋予读写权限。
chmod 755 /var/lib/mysql
设置root 密码
[root@instance-jbf586lj ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 7 Server version: 5.6.41 MySQL Community Server (GPL) Copyright (c) 2000, 2018, 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 databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.00 sec) 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('!qaz2wsx') where user = 'root'; Query OK, 4 rows affected (0.00 sec) Rows matched: 4 Changed: 4 Warnings: 0 mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '!qaz2wsx' WITH GRANT OPTION; mysql> exit Bye |
注意:如果出现-bash: mysql: command not found,原因:这是由于系统默认会查找/usr/bin下的命令,如果这个命令不在这个目录下,当然会找不到命令,我们需要做的就是映射一个链接到/usr/bin目录下,相当于建立一个链接文件。
# ln -s /usr/local/java/mysql5.6/bin/mysql /usr/bin |
更多Java、数据库、微服务、分布式的原创系列文章正在路上,欢迎扫描下方二维码,持续关注:
灵动思维文化传媒(id:itjhzx)
多年技术经验倾囊相授
以上是关于CentOs 安装MySql数据库的主要内容,如果未能解决你的问题,请参考以下文章