数据库
首页 > 数据库> > centos7.0 安装MySQL数据库

centos7.0 安装MySQL数据库

作者:互联网

1、下载 MySQL:mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz。
上传至/usr/local目录下,
解压: [root@node3 local]# tar -zxvf mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz
重命名: [root@node3 local]# mv mysql-5.7.27-linux-glibc2.12-x86_64/ mysql
创建软连接: [root@node3 ~]# ln -s /usr/local/mysql/ mysql3306

2、增加一个用户组: [root@node3 ~]# groupadd mysql
查看用户组mysql是否增加成功: [root@node3 ~]# cat /etc/group
删除用户组: [root@node3 ~]# groupdel mysql
修改用户组: [root@node3 ~]# groupmod mysql
查看用户组成员: [root@node3 ~]# groups mysql
参考文章:https://www.cnblogs.com/jackyyou/p/5498083.html

3、在mysql用户组中增加一个用户: [root@node3 ~]# useradd -r -g mysql -s /bin/false mysql
注意一篇文章解释了/sbin/nologin和/bin/false的区别:
https://www.cnblogs.com/iSun520/p/11142832.html
useradd和adduser的区别:
https://blog.csdn.net/danson_yang/article/details/65629948

也可以用下列语句:useradd -r -s /sbin/nologin -g mysql mysql -d /usr/local/mysql ---新建msyql用户禁止登录shell

4、创建数据仓库目录
[root@node3 /]# mkdir -p /data/mysql

5、改变目录属有者
[root@node3 ~]# cd /usr/local/mysql
[root@node3 mysql]# chown -R mysql .
[root@node3 mysql]# chgrp -R mysql .
[root@node3 mysql]# chown -R mysql /data/mysql

6、配置参数
[root@node3 mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql
2019-10-16T10:09:37.760483Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-10-16T10:09:39.495129Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-10-16T10:09:40.070740Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-10-16T10:09:40.468516Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 112181ab-effd-11e9-8790-000c297fe80a.
2019-10-16T10:09:40.471175Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-10-16T10:09:40.570194Z 1 [Note] A temporary password is generated for root@localhost: Um4?a.27i:/e

注意:1、记住返回信息末尾处的临时密码:Um4?a.27i:/e
2、mysql版本如果是5.7.6以下的版本,数据库初始化密码是不一样的:
[root@node3 mysql]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/data/mysql --user=mysql

[root@node3 mysql]# bin/mysql_ssl_rsa_setup --datadir=/data/mysql
Generating a 2048 bit RSA private key
..........+++
......+++
writing new private key to 'ca-key.pem'
-----
Generating a 2048 bit RSA private key
.+++
.................................+++
writing new private key to 'server-key.pem'
-----
Generating a 2048 bit RSA private key
....................................................................................+++
.................................................+++
writing new private key to 'client-key.pem'
-----

7、修改系统配置文件
[root@node3 mysql]# cd support-files/
[root@node3 mysql]# cp my-default.cnf /etc/my.cnf
--我的support-files下并没有发现my-default.cnf,但是/etc/my.cnf文件是已经存在的,暂不执行此命令复制文件
[root@node3 mysql]# cp mysql.server /etc/init.d/mysql

在/etc/init.d/mysql中设置datadir,basedir:
basedir=/usr/local/mysql
datadir=/data/mysql

8、启动mysql
[root@node3 support-files]# /etc/init.d/mysql start
Starting MySQL.2019-10-16T10:26:54.592444Z mysqld_safe error: log-error set to '/var/log/mariadb/mariadb.log', however file don't exists. Create writable for user 'mysql'.
ERROR! The server quit without updating PID file (/var/lib/mysql/node3.pid).
出现上述错误,是因为mysql启动的时候找不到mariadb.log,需手动创建该文件?
[root@node3 var]# mkdir /var/log/mariadb
[root@node3 var]# touch /var/log/mariadb/mariadb.log
[root@node3 var]# chown -R mysql:mysql /var/log/mariadb/

再次尝试启动mysql:
[root@node3 support-files]# /etc/init.d/mysql start
Starting MySQL.. SUCCESS!

9、登录myql
[root@node3 support-files]# mysql -hlocalhost -uroot -p
-bash: mysql: command not found
注意:如果出现上面错误提示,需执行以下命令:
[root@node3 support-files]# ln -s /usr/local/mysql/bin/mysql /usr/bin
再次尝试登录:
[root@node3 support-files]# mysql -hlocalhost -uroot -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
输入之前的临时密码后,如果出现上述错误,应该是找不到/tmp/mysql.sock文件。
在/etc/my.cnf里面有设置socket的路径,可以用此路径建立软连接,链接到/tmp/mysql.sock。如下:
[root@node3 support-files]# cd /tmp
[root@node3 tmp]# ln -s /var/lib/mysql/mysql.sock mysql.sock
再次尝试登录,成功:
[root@node3 tmp]# mysql -hlocalhost -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.27
--修改密码
mysql> set password=password('root');
--设置root账户的host地址(修改了才可以远程连接)
mysql>grant all privileges on *.* to 'root'@'%' identified by 'root';
mysql>flush privileges;
--查看表
mysql> use mysql;
mysql> select host,user from user;
--这里就可以使用远程连接测试了;如果不能远程用navicat链接,需要用如下命令设置端口:
[root@node3 tmp]# /sbin/iptables -I INPUT -p tcp --dport 3306 -j ACCEPT

10、添加系统路径
[root@node3 tmp]# vim /etc/profile
添加:export PATH=/usr/local/mysql/bin:$PATH
[root@node3 tmp]# source /etc/profile

11、配置mysql自动启动
[root@node3 ~]# chmod 755 /etc/init.d/mysql
[root@node3 ~]# chkconfig --add mysql
[root@node3 ~]# chkconfig --level 345 mysql on

12、关闭防火墙iptables
临时关闭:service iptables stop
永久关闭:chkconfig iptables off

关闭selinux:
vi /etc/sysconfig/selinux
将SELINUX修改为DISABLED,即SELINUX=DISABLED

13、补充几个mysql的命令
--退出mysql命令窗口
[root@node3 ~]#exit
--查看mysql状态
[root@node3 ~]#service mysql status
--停止mysql
[root@node3 ~]#service mysql stop
--启动mysql
[root@node3 ~]#service mysql start

14、附加my.cnf文件内容
/etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
# *** DO NOT EDIT THIS FILE. It's a template which will be copied to the
# *** default location during install, and will be replaced if you
# *** upgrade to a newer version of MySQL.

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
innodb_buffer_pool_size = 10G

# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
log_bin
character-set-server=utf8
collation-server=utf8_bin
init-connect='SET NAMES utf8'
# These are commonly set, remove the # and set as required.
basedir = /usr/local/mysql
datadir = /export/mysql/var
port = 3306
server_id = 22206
socket = /export/mysql/mysql.sock
binlog_format = statement
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
join_buffer_size = 128M
sort_buffer_size = 2M
read_rnd_buffer_size = 2M
log_bin_trust_function_creators = on
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

lower_case_table_names=1

标签:local,数据库,MySQL,etc,usr,mysql,centos7.0,node3,root
来源: https://www.cnblogs.com/Ryan-Fei/p/11790631.html