Linux安装Mysql

一.准备工作:
# 安装依赖 yum -y install perl perl-devel autoconf libaio
1.添加MySQL用户组
# groupadd mysql
2.添加mysql用户
# useradd -g mysql mysql
-g 指定用户组
二.正文:
1.下载mysql glibc版 https://dev.mysql.com/downloads/mysql/
选择Linux-Generic 64位tar.gz版本
 
 
 
 
2.解压mysql 并移动到usr/local/下 
# tar zxvf mysql-5.7.16-linux-glibc2.5-x86_64.tar.gz
# mv mysql-5.7.16-linux-glibc2.5-x86_64 /usr/local/mysql
创建data文件夹
# mkdir data
3.改变文件夹所有者
# chown -R mysql /usr/local/mysql
# chgrp -R mysql /usr/local/mysql
4.安装mysql
# cd /usr/local/mysql #进入安装目录
# cd bin
执行:
./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
 
 gh+6!shTb/Nj
红框部分为产生一个临时的密码,登录mysql的时候需要用到。
A、执行以下命令创建RSA private key
进入musql的bin目录
执行: ./mysql_ssl_rsa_setup --datadir=/usr/local/mysql/data
B、cd .. 退出到mysql目录 修改当前目录拥有者为root用户
执行:sudo chown -R root:root ./
C、修改当前data目录拥有者为mysql用户
 sudo chown -R mysql:mysql data
 
 
5.测试安装
# cd /usr/local/mysql/support-files
# ./mysql.server start
显示 start Starting MySQL.. OK!说明成功
6.设置mysql启动初始化文件
# cd /usr/local/mysql/support-files
# cp my-default.cnf /etc/my.cnf
配置my.cnf
vim /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 = 128M
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
# These are commonly set, remove the # and set as required.
 basedir = /usr/local/mysql
 datadir = /usr/local/mysql/data
 port = 3306
# server_id = ...
 socket = /tmp/mysql.sock
 pid-file = /usr/local/mysql/mysql.pid
 max_connections = 1500
 read_buffer_size = 2M
 character_set_server=utf8
 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 
# 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 
[client]
default-character-set=utf8
 
7.设置mysql开机自启动
# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
# chmod 755 /etc/init.d/mysql                                     # 增加执行权限
# chkconfig --list mysql                                       # 检查自启动项列表中没有mysql这个,
# chkconfig --add mysql                                        # 如果没有就添加mysqld:
# chkconfig mysql on                                          # 用这个命令设置开机启动:
 
至此mysql全部安装完毕
因为上面步骤已经去启动了mysql,先kill掉mysql的进程
执行: ps -aux|grep mysql  把所有mysql的进程kill掉
然后运行命令:service mysql start 启动mysql
# ./usr/local/mysql/bin/mysql -uroot -p
PASSWORD: #EYHDx!_w3:U
输入刚产生的临时密码 (我设置的密码为123456)
修改 root 密码 set password for 'root'@'localhost' = '你的密码';
 
注:
错误:
root@DB-02 ~]# mysql -u root
-bash: mysql: command not found
遇上-bash: mysql: command not found的情况别着急,这个是因为/usr/local/bin目录下缺失mysql导致,只需要一下方法建立软链接,即可以解决:
把mysql安装目录,比如MYSQLPATH/bin/mysql,映射到/usr/local/bin目录下: 
# cd /usr/local/bin
# ln -fs /MYSQLPATH/bin/mysql mysql
[注意1:远程连接需要防火墙加入3306端口]
防火墙加入3306端口:
vi /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
加入文本 然后重启防火墙:
/etc/init.d/iptables restart
 
8、设置远程登录密码(以下远程登录密码也为123456) 一般不建议root用户开放远程连接:
mysql -uroot -p123456 
GRANT ALL PRIVILEGES ON *.* TO root@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
然后刷新权限:
flush privileges;
 
[注意1: 一般不建议使用管理员设置远程登陆 ] 比如真实项目中需要一个mytest的数据库
使用管理员账号在服务器端创建数据库:
mysql>create database mytest charater set utf8;
为改数据库创建一个用户名:xiaolizi 密码:ermazi 的账号
mysql>create user xiaolizi identified by 'ermazi';
授权mytest 的所有权限
mysql>grant all on mytest.* to xiaolizi ;
这样就可以使用该账号管理真实项目中mytest这个库了
[注意2:进入mysql这个库查看user表,如果host字段为%表示允许远程连接,远程链接没问题,但是你要是现在Linux本机上登陆xiaoliz这个账号的话,也要加上本机的ip]
如:Linux的ip
链接时提示错误
链接方式:mysql -uxiaolizi -h192.168.19.135 -permazi
就可以链接到数据库了
 
 
 

posted @ 2021-03-12 17:06  Liang_cr  阅读(87)  评论(0编辑  收藏  举报