在安装过程中所遇到的问题有:
1、my-default.cnf文件找不到
2、mysql启动报错
3、mysql临时密码无法登录
安装环境centos 6.0
下载数据库:
[root@CLanguage src]# pwd
/usr/local/src #下载文件存放路径(个人原因,可随意。)
[root@CLanguage src]#
wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz
[root@CLanguage src]# tar -zxvf mysql-5.7.21-linux-glibc2.12-x86_64.tar.gz #解压
[root@CLanguage src]# mv mysql-5.7.21-linux-glibc2.12-x86_64 mysql
#更改文件名
卸载旧版本:
[root@CLanguage src]# rpm -qa |grep mysql
mysql-5.1.47-4.el6.x86_64
mysql-libs-5.1.47-4.el6.x86_64
mysql-devel-5.1.47-4.el6.x86_64
[root@CLanguage src]# rpm -e --nodeps mysql-5.1.47-4.el6.x86_64
[root@CLanguage src]# rpm -e --nodeps mysql-libs-5.1.47-4.el6.x86_64
[root@CLanguage src]# rpm -e --nodeps mysql-devel-5.1.47-4.el6.x86_64
[root@CLanguage src]# rpm -qa |grep mysql
[root@CLanguage src]#
创建数据库目录以及用户和组:
[root@CLanguage src]#mkdir /data
[root@CLanguage src]#mkdir /data/mysqldata #数据存放目录
[root@CLanguage src]#groupadd mysql #创建mysql组
[root@CLanguage src]#useradd -r -s /sbin/nologin -g mysql mysql -d /usr/local/src/mysql #添加mysql用户使其shell模式为nologin(禁止登录),
[root@CLanguage mysql]# chown -R mysql .
[root@CLanguage mysql]# chgrp -R mysql .
[root@CLanguage mysql]#chown -R mysql /data/mysqldata
安装前先安装numactl组件,或者会报错:bin/mysqld: error while loading shared libraries: libnuma.so.1
[root@CLanguage mysql]# yum -y install numactl
(可以使用本地CD yum源进行安装,前提是搭建好后。)
配置参数:
[root@CLanguage src]# cd mysql
[root@CLanguage mysql]# bin/mysqld --initialize --user=mysql --basedir=/usr/local/src/mysql --datadir=/data/mysqldata/ #配置参数
2018-01-31T08:41:32.144065Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-01-31T08:41:34.501273Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-01-31T08:41:34.943351Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-01-31T08:41:35.016168Z 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: 8b673fc1-0662-11e8-a70a-000c298536ba.
2018-01-31T08:41:35.021358Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-01-31T08:41:35.025019Z 1 [Note] A temporary password is generated for root@localhost: dhaMrqAVZ5-C {最后的root@localhost:后面为临时密码}
--basedir=/usr/local/src/mysql #mysql文件所在目录
--datadir=/data/mysqldata/ #数据库存放目录
[root@CLanguage mysql]# bin/mysql_ssl_rsa_setup --datadir=/data/mysqldata/
#配置参数
修改系统配置文件:
在support-files目录未发现my-default.cnf文件,所以只能手动创建了。
[root@CLanguage support-files]# vim /etc/my.cnf (创建my.cnf,将以下代码复制进去,记得下面几个目录需要自己手动修改为自己定义的目录)
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'
basedir=/usr/local/src/mysql #文件存放目录
datadir=/data/mysqldata/ #数据库存放目录
socket=/tmp/mysql.sock #以上面socket值要一致
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
lower_case_table_names = 1
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
max_connections=5000
default-time_zone = '+8:00'
(这里参数不全,可以去其他地方拷贝一个my.cnf文件)
或者拷贝以下内容:
[mysqld]
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES # 一般配置选项
basedir = /usr/local/src/mysql #需修改
datadir = /data/mysqldata #需修改
port = 3306
socket = /var/run/mysqld/mysqld.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
[root@CLanguage support-files]# cp mysql.server /etc/init.d/mysqld
[root@CLanguage support-files]# vim /etc/init.d/mysqld
修改/etc/init.d/mysqld在basedir和datadir后填入文件存放路径和数据存放路径
启动mysql:
[root@CLanguage mysql]# bin/mysqld_safe --user=mysql & #检测
[root@CLanguage run]# /etc/init.d/mysql start (这里会报错,因为找不到mysqld文件夹,所以要创建并赋予权限。)
[root@CLanguage mysql]# cd /var/run/
[root@CLanguage run]# mkdir mysqld
[root@CLanguage run]# chown -R mysql mysqld
[root@CLanguage run]# chgrp -R mysql mysqld
[root@CLanguage run]# /etc/init.d/mysqld start
Starting MySQL... [ OK ]
登录mysql已经修改密码:
如果使用bin/mysql -uroot -p无法登录时,可以使用以下办法进行密码重设:
修改/etc/my.cnf文件
在[mysqld]下面添加“skip-grant-tables”
(skip-grant-tables命令为跳过验证,直接登录。个人认为好比linux的单用户模式,来进行mysql的密码修改。)
重启mysql
在命令行中输入bin/mysql进行登录(直接输入mysql命令可能提示找不到命令。因为path路径未设定,后面添加path路径说明。)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> USE mysql; #选择数据库
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> update user set authentication_string=password('123abc') where user='root';
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 1
mysql> flush privileges; #刷新权限
Query OK, 0 rows affected (0.00 sec)
mysql> quit;
Bye
重新登录mysql
[root@CLanguage run]#mysql -uroot -p123abc
修改完成后,注销my.cnf中的“skip-grant-tables”
修改环境变量:
将mysql的bin路径添加到/etc/profile
[root@CLanguage mysql]# vim /etc/profile
添加:
export PATH=/usr/local/src/mysql/bin:$PATH
[root@CLanguage mysql]# source /etc/profile
配置mysql自动启动
[root@CLanguage mysql]#chmod 755 /etc/init.d/mysqld
[root@CLanguage mysql]#chkconfig --add mysqld
[root@CLanguage mysql]#chkconfig --level 345 mysqld on
在使用过程中,发现重启电脑,mysql无法启动。报PID文件找不到,存放PID文件的路径为:/var/run/mysqld下面,每次重启完在/var/run/目录中都找不到mysqld文件夹,都需要重新创建一次mysqld文件夹和修改文件夹的所属用户和组,才能正常使用。
解决方法
因为之所以/var/run/mysqld 目录每次重启后都需要手动去创建,是因为/var/run/目录下建立文件夹是在内存中,故每次重启后内存被清空导致/var/run/mysqld 也被清除,从而导致无法启动mysql。
创建一个mysqld.pid文件存放目录,重新修改文件夹所属用户和组。修改my.cnf文件,找到pid-file=/var/run/mysqld/mysqld.pid将mysqld.pid存放路径修改到其他存放位置。具体操作如下:
重新启动mysql正常运行。