新手学习MySQL,在阿里云主机上安装MySQL5.7.18.
参考文档: 安装步骤
错误检查: Can’t connect to local mysql server through socket ‘/tmp/mysql.sock’ (2)
从网上找MySQL的安装包,(官网上太难找了,在别的博文上找到一个镜像下载地址)
获取安装包,解压至安装目录,添加用户和组,更改所有者和所属组
wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz
tar zxf mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz -C /usr/local
cd /usr/local
mv mysql-5.7.18-linux-glibc2.5-x86_64/ mysql
groupadd mysql
useradd -r -g mysql mysql
chown -R mysql:mysql mysql/
5.7.18版本中./support-files目录下缺少一个my-default.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]
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
# 一般配置选项
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
socket = /var/run/mysqld/mysqld.sock
#网上很多教程里socket的路径是/tmp/mysql.sock
#我一直不行,按照本文配置成功,后续还有地方要修改
character-set-server=utf8
#下面是可选项,要不要都行,如果出现启动错误,则全部注释掉,保留最基本的配置选项,然后尝试添加某些配置项后启动,检测配置项是否有误(首次安装报错,以下内容已删)
back_log = 300
max_connections = 3000
max_connect_errors = 50
table_open_cache = 4096
max_allowed_packet = 32M
#binlog_cache_size = 4M
max_heap_table_size = 128M
read_rnd_buffer_size = 16M
sort_buffer_size = 16M
join_buffer_size = 16M
thread_cache_size = 16
query_cache_size = 128M
query_cache_limit = 4M
ft_min_word_len = 8
thread_stack = 512K
transaction_isolation = REPEATABLE-READ
tmp_table_size = 128M
#log-bin=mysql-bin
long_query_time = 6
server_id=1
innodb_buffer_pool_size = 1G
innodb_thread_concurrency = 16
innodb_log_buffer_size = 16M
innodb_log_file_size = 512M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
innodb_file_per_table = on
[mysqldump]
quick
max_allowed_packet = 32M
[mysql]
no-auto-rehash
default-character-set=utf8
safe-updates
[myisamchk]
key_buffer = 16M
sort_buffer_size = 16M
read_buffer = 8M
write_buffer = 8M
[mysqlhotcopy]
interactive-timeout
[mysqld_safe]
open-files-limit = 8192
注册和初始化MySQL服务
一些老版本的MySQL是通过mysql_install_db的方式安装,在5.7的版本中不推荐,改用mysqld安装
bin/mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
将support-files目录下的文件拷贝到/etc/
cp ./support-files/my-default.cnf /etc/my.cnf
cp ./support-files/mysql.service /etc/init.d/mysqld
启动MySQL服务
./bin/mysqld_safe --user=mysql &
加了&表示后台运行,执行jobs
看到mysqld_safe在执行即可
出现以下报错
Can’t connect to local mysql server through socket ‘/tmp/mysql.sock’ (2)
报错恢复是因为在my.conf配置文件中socket = /var/run/mysqld/mysqld.sock,而不是/tmp目录下,所以要创建一个软连接
ln -s /var/run/mysqld/mysqld.sock /tmp/mysql.sock
正常情况应配置到/tmp/目录下,但本人一直配置不成功,改到/var/run,添加软连接成功
登陆MySQL
手工设置密码
mysqladmin -uroot password '123456'
登陆
[root@izuf6hhpsgmib1344k6jknz mysql]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.18 MySQL Community Server (GPL)
Copyright (c) 2000, 2017, 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>