Mysql安装

  31 Mar 2018


mysql 安装配置

# 检查是否本机已安装,如存在卸载
rpm -qa | grep mysql
rpm -e 已经存在的MySQL全名

# 下载
wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz

tar -zxvf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz -C /usr/local/
ln -s /usr/local/mysql-5.7.17-linux-glibc2.5-x86_64  /usr/local/mysql5.7

# 配置
cp mysql5.7/my-default.cnf  /etc/my.cnf

# 编辑my.cnf进行设置
[mysql]
default-character-set=utf8

[mysqld]
default-storage-engine=INNODB
default-character-set=utf8
# 如果 default-character-set=utf8 有问题换为: character_set_server=utf8

# 复制support-files/mysql.server至/etc/init.d/mysql(开机自启) 并编辑mysql
basedir=/usr/local/mysql5.7
datadir=/usr/local/mysql5.7/data


# 安全考虑创建mysql用户用于mysql操作
groupadd mysql
useradd -r -g mysql mysql
passwd mysql
chown -R mysql:mysql /usr/local/mysql5.7/


# 初始化 mysql 的数据库
/usr/local/mysql5.7/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql5.7 --datadir=/usr/local/mysql5.7/data
# 如果找不到libaio.so.1
yum install libaio(apt-get install libaio1 libaio-dev)

# 数据库加密
./mysql_ssl_rsa_setup --datadir=/usr/local/mysql5.7/data

# 启动数据库
./mysqld_safe --user=mysql &

# 开启默认端口
firewall-cmd --zone=public --add-port=3306/tcp --permanent

# 登陆
ln -s /usr/local/mysql/bin/mysql /usr/local/bin/mysql
mysql -uroot -p
// 设置新密码
> set password=password('新密码')
// 设置远程访问(root-新密码)
> grant all privileges on *.* to root@'%' identified by '访问密码';
// 刷新
> flush privileges;
> quit


# 开机启动
chkconfig  --add mysql
chkconfig  mysql on

# 环境变量,/etc/profile添加
export PATH=$JAVA_HOME/bin:/usr/local/mysql/bin:$PATH