前言
在linux上安裝mysql5.6版本,並遠程連接mysql數據庫操作
安裝mysql
mysql的安裝可以用yum安裝更方便
[root@yoyo ~]# cd /usr/local/
[root@yoyo ~]# mkdir mysql-community-release
[root@yoyo ~]# cd mysql-community-release
[root@yoyo ~]# wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
[root@yoyo ~]# rpm -ivh mysql-community-release-el7-5.noarch.rpm
[root@yoyo ~]# yum -y install mysql-community-server
安裝完成后查看版本號:mysql -V
[root@yoyo local]# mysql -V
mysql Ver 14.14 Distrib 5.6.42, for Linux (x86_64) using EditLine wrapper
安裝完成后重啟mysql服務,查看狀態是 Active: active (running) ,說明啟動成功
啟動服務:service mysqld restart
[root@yoyo local]# service mysqld restart
查看mysql運行狀態:systemctl status mysql.service
[root@yoyo local]# systemctl status mysql.service
[root@yoyo local]# service mysqld restart
Redirecting to /bin/systemctl restart mysqld.service
Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.
[root@yoyo ~]# systemctl status mysql.service
● mysqld.service - MySQL Community Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Tue 2019-01-15 09:53:42 CST; 47s ago
Main PID: 946 (mysqld_safe)
CGroup: /system.slice/mysqld.service
├─ 946 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
└─1282 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/v...
Jan 15 09:53:39 yoyo systemd[1]: Starting MySQL Community Server...
Jan 15 09:53:40 yoyo mysqld_safe[946]: 190115 09:53:40 mysqld_safe Logging to '/var/log/mysqld.log'.
Jan 15 09:53:40 yoyo mysqld_safe[946]: 190115 09:53:40 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
Jan 15 09:53:42 yoyo systemd[1]: Started MySQL Community Server.
mysql重置密碼
方法一:
初次安裝使用mysql,root賬戶默認是沒設置密碼的,系統會給個臨時密碼,在/var/log/mysqld.log可以查看
[root@yoyo local]# grep 'temporary password' /var/log/mysqld.log
如下圖所示,出現的就是臨時密碼,復制出來就可以登錄mysql了
[root@yoyo local]# mysql -u root -p
看到Enter password: 輸入密碼,重置密碼后exit退出mysql
[root@yoyo local]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 26
Server version: 5.6.42 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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> update user set password = Password('root') where User = 'root';
# 回車后執行(刷新MySQL系統權限相關的表):
mysql> flush privileges;
# 再執行exit退出:
mysql> exit;
Bye
[root@yoyo local]#
方法二:
要是上一步找不到臨時密碼,那就用此方法,先停掉mysql,以安全方式啟動
[root@yoyo local]# systemctl stop mysql.service
以安全方式啟動mysql:
[root@yoyo local]# /usr/bin/mysqld_safe --skip-grant-tables >/dev/null 2>&1 &
然后執行
[root@yoyo local]# /usr/bin/mysql -u root mysql
出現“mysql>”提示符后輸入:
mysql> update user set password = Password('root') where User = 'root';
回車后執行(刷新MySQL系統權限相關的表):
mysql> flush privileges;
再執行exit退出:
mysql> exit;
退出后,使用以下命令登陸mysql,試試是否成功:
[root@yoyo local]#mysql -u root -p
按提示輸入密碼:root
[root@yoyo local]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 27
Server version: 5.6.42 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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>
出現Welcome to the MySQL那就是登錄成功了
查看mysql端口號
mysql默認端口是3306,如何查看msyql端口號呢?可以用root賬號登錄后,執行show variables like 'port';
[root@yoyo local]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 33
Server version: 5.6.42 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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> show variables like 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 3306 |
+---------------+-------+
1 row in set (0.00 sec)
mysql>
授權mysql遠程連接
mysql在linux上安裝完成后,為了方便的查看,可以在本地電腦上安裝一個遠程連接數據庫的客戶端,遠程連上mysql
方法一:
先創建一個root新用戶,登錄密碼為password,可以自己隨便命名
mysql> create user 'root'@'%' identified by 'password';
[root@yoyo sysconfig]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
mysql> create user 'root'@'%' identified by 'password';
ERROR 1396 (HY000): Operation CREATE USER failed for 'root'@'%'
mysql> exit
方法二:
查看user表,把host為localhost,user為root的記錄更新host為%就是允許遠程訪問了
操作mysql時候,先執行use mysql
[root@yoyo sysconfig]# mysql -u root -p
Enter password:
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> select user,host,password from user;
+-------+-----------+-------------------------------------------+
| user | host | password |
+-------+-----------+-------------------------------------------+
| root | yoyo | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| root | 127.0.0.1 | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| root | ::1 | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| | yoyo | |
| root1 | % | *668425423DB5193AF921380129F465A6425216D0 |
| root | localhost | *2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19 |
+-------+-----------+-------------------------------------------+
6 rows in set (0.00 sec)
如果看到root后面的host對應的是%說明有遠程訪問權限,顯示localhost就 update更新它,如何flush privileges刷新系統權限
mysql> use mysql
mysql> update user set host = '%' where user = 'root';
ERROR 1062 (23000): Duplicate entry '%-root' for key 'PRIMARY'
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> select user,host,password from user;
+-------+-----------+-------------------------------------------+
| user | host | password |
+-------+-----------+-------------------------------------------+
| root | yoyo | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| root | 127.0.0.1 | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| root | ::1 | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
| | yoyo | |
| root1 | % | *668425423DB5193AF921380129F465A6425216D0 |
| root | % | *2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19 |
+-------+-----------+-------------------------------------------+
6 rows in set (0.00 sec)
mysql> exit
方法三:
授權法,給root用戶遠程登錄的權限
# 想root使用123456從任何主機連接到mysql服務器的話
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123546' WITH GRANT OPTION;
# 如果你想允許用戶root從ip為192.168.1.3的主機連接到mysql服務器,並使用123456作為密碼
mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.1.3' IDENTIFIED BY '123456' WITH GRANT OPTION;
接下來去阿里雲ECS服務后台安全組-添加規則,新增3306端口訪問權限,使用Navicat遠程工具就可以連上了
這里的賬號密碼就是前面方法一里面設置的“root” 和“password”
開啟與關閉服務
1啟動mysql
service mysqld start
2查看mysql運行狀態
service mysqld status # 或者 systemctl status mysql.service
3停掉mysql服務
service mysqld stop # 或者 systemctl stop mysql.service
4重啟mysql
systemctl restart mysql.service
5查看運行進程
ps -ef | grep mysqld
[root@yoyo sysconfig]# ps -ef | grep mysql
mysql 2506 1 0 12:51 ? 00:00:00 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
mysql 2674 2506 0 12:51 ? 00:00:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64mysql/plugin --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
root 2748 1668 0 12:55 pts/0 00:00:00 grep --color=auto mysql
6查看mysql端口
netstat -tulpn |grep mysql
[root@yoyo sysconfig]# netstat -tulpn |grep mysql
tcp6 0 0 :::3306 :::* LISTEN 2674/mysqld
遇到問題
啟動mysql的時候,出現Failed to start MySQL Community Server.具體報錯如下
[root@yoyo ~]# systemctl status mysql.service
● mysqld.service - MySQL Community Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: failed (Result: start-limit) since Mon 2019-01-14 20:31:27 CST; 13h ago
Process: 26800 ExecStartPost=/usr/bin/mysql-systemd-start post (code=exited, status=0/SUCCESS)
Process: 26799 ExecStart=/usr/bin/mysqld_safe --basedir=/usr (code=exited, status=1/FAILURE)
Process: 26786 ExecStartPre=/usr/bin/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
Main PID: 26799 (code=exited, status=1/FAILURE)
Jan 14 20:31:27 yoyo systemd[1]: Failed to start MySQL Community Server.
Jan 14 20:31:27 yoyo systemd[1]: Unit mysqld.service entered failed state.
Jan 14 20:31:27 yoyo systemd[1]: mysqld.service failed.
Jan 14 20:31:27 yoyo systemd[1]: mysqld.service holdoff time over, scheduling restart.
Jan 14 20:31:27 yoyo systemd[1]: start request repeated too quickly for mysqld.service
Jan 14 20:31:27 yoyo systemd[1]: Failed to start MySQL Community Server.
Jan 14 20:31:27 yoyo systemd[1]: Unit mysqld.service entered failed state.
Jan 14 20:31:27 yoyo systemd[1]: mysqld.service failed.
剛開始以為是哪里配置有問題,后來百度搜了下,reboot重啟linux服務器就解決了。
交流QQ群:779429633