操作系統:
[root@host-192-168-1-21 mysql]# cat /etc/issue
CentOS release 6.6 (Final)
Kernel \r on an \m
mysql5.7安裝包:
選擇tar包安裝:
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz
tar -xf mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz -C /usr/local/mysql5718 --strip-components 1
# install the basie lib
yum install cmake -y
groupadd mysql
useradd -g mysql mysql
autoreconf --force --install
libtoolize --automake --force
automake --force --add-missing
yum install -y libtoolize
yum install gcc gcc-c++ -y
yum install -y ncurses-devel.x86_64
yum install -y cmake.x86_64
yum install -y libaio.x86_64
yum install -y bison.x86_64
yum install -y gcc-c++.x86_64
yum install make -y
# add mysql account,create the basic directory
mkdir -p /data/mysql/data
cd /data/mysql/data
chown -R mysql.mysql /data
chown -R mysql.mysql /usr/local/mysql5711
mkdir -p /data/mysql/binlog/
chown -R mysql.mysql /data/mysql/binlog/
cd /usr/local/mysql5711/
# init databases (缺少my.cnf)
rm -rf /data/mysql/data/*
echo " 配置內容 " > my.cnf
#cp my.cnf /usr/local/mysql5711/my.cnf
chown -R mysql.mysql /usr/local/mysql5711/my.cnf
time bin/mysqld --defaults-file=/usr/local/mysql5711/my.cnf --initialize-insecure --user=mysql #順序很重要--defaults-file在前
# set the auto start on linux server started
cp support-files/mysql.server /etc/init.d/mysql
#chmod 700 /etc/init.d/mysql
echo "export PATH=$PATH:/usr/local/mysql5711/bin">>/etc/profile
source /etc/profile
chkconfig --add mysql
# do a soft link to start mysql
cd /usr/local/
ln -s /usr/local/mysql5711 mysql
# remove default my.cnf
mv /etc/my.cnf /tmp/
# start the mysql server
/usr/local/mysql/bin/mysqld_safe --defaults-file=/usr/local/mysql/my.cnf --user=mysql
service mysql start
mysql5.7之前版本安裝啓動之後,第一次登陸不需要密碼,直接回車可以登錄。
mysql5.7安裝之後,無法再無密碼登錄,而是會產生一個隨機密碼。
2017-05-10T03:21:15.969414Z 1 [Note] A temporary password is generated for root@localhost: c9uow;j8,7Yt
如果記錄了密碼,可以使用此密碼做臨時登錄,登錄之後mysql會提示用戶修改密碼;如果忘了臨時密碼,可以做一下操作:
關閉mysql,跳過權限表啓動mysql
[root@host-192-168-1-21 mysqld]# service mysql stop
Stopping mysqld: [ OK ]
[root@host-192-168-1-21 mysqld]# service mysql start --skip-grant-tables
Starting mysqld: [ OK ]
直接登錄:
[root@host-192-168-1-21 mysqld]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
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>
修改root密碼:
mysql> update user set authentication_string=password("123456") where user="root";
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
注:5.7中mysql.user表已經沒有password字段,加密後的用戶密碼存儲在authentication_string字段
flush之後退出:
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
這時發現在root目錄下產生了.mysql_history文件
[root@host-192-168-1-21 ~]# cat .mysql_history
_HiStOrY_V2_
show\040databases;
use\040mysql;
show\040tables;
desc\040user;
select\040user,host,authentication_string\040from\040user;
show\040warnings;
flush\040privileges;
quit
show\040grants;
show\040databases;
quit
再登錄mysql,查看權限:
mysql> show grants;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
還需要alter user重置密碼:
mysql> alter user root@'localhost' identified by '123';
Query OK, 0 rows affected (0.00 sec)
在另一臺同一網段的機器上登錄這臺新裝的mysql:(ip爲新裝mysql服務器的ip)
[root@host-192-168-1-128 ~]# mysql -u root -h192.168.1.21 -p
Enter password:
ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.1.21' (113)
先查看是否有遠程登錄的權限:
[email protected] : (none) 01:51:20> show grants for root;
+-------------------------------------------------------------+
| Grants for root@% |
+-------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION |
+-------------------------------------------------------------+
1 row in set (0.00 sec)
可以遠程登錄。查看報錯信息:
[root@host-192-168-1-128 ~]# perror 113
OS error code 113: No route to host
ping 192.168.1.21可以ping通。
造成這個的最有可能是系統防火牆的原因。關閉192.168.1.21的防火牆,service iptables stop,然後再遠程訪問,發現可以訪問。