阿里云Centos7.x MySql安装教程示例

创建用户

useradd mysql;
passwd mysql;

下载(比如:5.5.61)

地址

https://dev.mysql.com/downloads/mysql/5.6.html#downloads

选择符合服务器CPU架构的二进制包


cd /home/mysql && wget https://cdn.mysql.com//Downloads/MySQL-5.5/mysql-5.5.61-linux-glibc2.12-x86_64.tar.gz;

解压

tar vxzf mysql-5.5.61-linux-glibc2.12-x86_64.tar.gz;

重命名

mv mysql-5.5.61-linux-glibc2.12-x86_64 mysql-5.5.61;

配置

新建配置文件

vi /home/mysql/port-3306/my.cnf

[client]
default-character-set=utf8    #客户端连接编码

[mysql]
auto-rehash                   #客户端tab补全
default-character-set=utf8    #编码

[mysqld]
user=mysql                    #运行账户
port=3306                     #定义端口
log-bin=mysql-bin             #开启二进制日志
server-id=1001                #定义服务ID

binlog_format=row          # binlog类型
binlog_do_db=MDA        # 设置binlog数据库

max_allowed_packet=50M        ###最大
wait_timeout=30             ###等待超时默认s
interactive_timeout=30      ###活动超时

innodb_buffer_pool_size = 128M
join_buffer_size = 128M
sort_buffer_size = 2M
read_rnd_buffer_size = 2M

basedir=/home/mysql/mysql-5.5.61           #MYSQL根目录
datadir=/home/mysql/port-3306/data      #MYSQL数据存放目录
socket=/home/mysql/port-3306/3306_mysql.sock              #套接字

init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8                #定义数据库默认字符 server\collation
collation-server=utf8_unicode_ci
skip-character-set-client-handshake

symbolic-links=0

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 

lower_case_table_names = 1             #对表的大小写不敏感

[mysqld_safe]
log-error=/home/mysql/port-3306/logs/err.log              #错误日志
pid-file=/home/mysql/port-3306/mysqld.pid   #pid,多协议通信 tcp/sock

安装

依赖

yum install -y libaio;

初始化数据库

mkdir -p /data/mysql/port-3306/data;
cd /home/mysql/mysql-5.5.61 && scripts/mysql_install_db --basedir=/home/mysql/mysql-5.5.61 --datadir=/home/mysql/port-3306/data --defaults-file=/home/mysql/port-3306/my.cnf;

注意:生产环境推荐采用这种方式/home/mysql/mysql-5.5.61/bin/mysql_secure_installation移除测试数据库和匿名用户。

启动

mkdir -p /home/mysql/port-3306/logs && mkdir -p /home/mysql/port-3306/data && touch /home/mysql/port-3306/logs/err.log;
/home/mysql/mysql-5.5.61/bin/mysqld_safe --defaults-file=/home/mysql/port-3306/my.cnf --user=mysql &

错误


备注:在配置文件正确的情况下,解决这个错误很简单,修改安装目录所有文件的所有者和群租都为mysql。命令chown mysql:mysql /home/mysql/*

正确


修改管理员密码

/home/mysql/mysql-5.5.61/bin/mysqladmin -u root password 'youngangel.c0m' -S /home/mysql/port-3306/3306_mysql.sock -hlocalhost -P3306

开放外网连接权限

连接

/home/mysql/mysql-5.5.61/bin/mysql -S /home/mysql/port-3306/3306_mysql.sock -hlocalhost -P3306 -uroot -p'youngangel.c0m'

执行脚本

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'youngangel.c0m' WITH GRANT OPTION; 
FLUSH PRIVILEGES;  

测试

连接

/home/mysql/mysql-5.5.61/bin/mysql -S /home/mysql/port-3306/3306_mysql.sock -hlocalhost -P3306 -uroot -p'youngangel.c0m'

远程连接

如果是阿里云上装MySql需要在安全组里开启3306端口,同时查看本机防火墙,如果运行,也需要开启3306端口,命令如下:

systemctl status firewalld;
firewall-cmd --permanent --zone=public --add-port=3306/tcp;
firewall-cmd --complete-reload;

备注:千万不要忽略本机防火墙,不要认为阿里云安全组里添加端口就行了。

posted @ 2018-09-15 15:59  justmine  阅读(1356)  评论(0编辑  收藏  举报