Ubuntu mysql安裝與使用
阿新 • • 發佈:2019-01-01
Ubuntu 下安裝 mysql
執行下面的shell程式碼
#安裝mysql
sudo apt-get -y install mysql-server
sudo apt-get -y install mysql-client
sudo apt-get -y install libmysqlclient-dev
安裝過程中會提示你建立root賬戶
那就建立 要把root賬戶密碼記下來 不要弄丟咯
mysql 測試
sudo mysqld --initialize
2018-10-17T06:25:56.353286Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option ( see documentation for more details).
2018-10-17T06:25:56.354004Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.
2018-10-17T06:25:56.354012Z 0 [ERROR] Aborting
systemctl start mysqld
彈出一個messagebox 讓我輸入密碼我不確定是輸入mysql的root密碼還是Ubuntu的root密碼,感覺應該是要輸入mysql的root密碼.但是我還是決定要先試一下Ubuntu的root密碼.所以輸入Ubuntu的root密碼
結果輸出
Failed to start mysqld.service: Unit mysqld.service not found.
啟動失敗,猜想是輸入密碼錯誤了.換mysql的root密碼,這個root密碼就是在安裝mysql的時候初始建立的.
systemctl start mysqld
然後我用mysql的root密碼測試居然還是不行.
那個上面的問題應該就是輸入Ubuntu的密碼.至於為什麼不能成功應該從錯誤輸出找原因. 啟動mysqld服務失敗的原因是:
Unit mysqld.service not found
先不管 先看看別的
如果看到有mysql 的socket處於 listen 狀態則表示安裝成功
sudo netstat -tap|grep mysql
tcp 0 0 localhost:mysql *:* LISTEN 1071/mysqld
通過以下命令來檢查MySQL伺服器是否啟動:
ps -ef | grep mysqld
mysql 1071 1 0 10月17 ? 00:00:38 /usr/sbin/mysqld
yourname 12237 18990 0 12:10 pts/2 00:00:00 grep --color=auto mysqld
mysql -u root -p
可以登入
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.7.23-0ubuntu0.16.04.1 (Ubuntu)
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>
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> exit;
Bye
關閉MYSQL服務
./mysqladmin -u root -p shutdown
Enter password:
測試是否關閉成功
sudo netstat -tap|grep mysql
out put nothing
ps -ef | grep mysqld
yourname 20242 18990 0 12:15 pts/2 00:00:00 grep --color=auto mysqld
看不到mysql的服務了
然後我再把MYSQL的服務啟動起來
cd /usr/bin
./mysqld_safe &
2018-10-18T04:17:34.530613Z mysqld_safe Logging to syslog.
2018-10-18T04:17:34.540702Z mysqld_safe Logging to '/var/log/mysql/error.log'.
./mysqld_safe: 152: ./mysqld_safe: cannot create /var/log/mysql/error.log: Permission denied
2018-10-18T04:17:34.548777Z mysqld_safe Directory '/var/run/mysqld' for UNIX socket file don't exists.
./mysqld_safe: 152: ./mysqld_safe: cannot create /var/log/mysql/error.log: Permission denied
這幾乎代表著啟動失敗…
看一下
ps -ef|grep mysqld
liuzy 26197 18990 0 12:18 pts/2 00:00:00 grep --color=auto mysqld
看不到,服務沒有啟動起來,然後sudo加許可權 測試
sudo nohup ./mysqld_safe &
沒有出現什麼錯誤,然後測下
ps -ef|grep mysqld
liuzy 5254 18990 0 12:26 pts/2 00:00:00 grep --color=auto mysqld
跟剛才一樣… 換個命令測試
sudo netstat -tap|grep mysql
out put nothing
神馬也沒有輸出來,嘗試登入下mysql
mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
已經不能登入成功了…
當前msyql伺服器執行環境
cat /etc/issue
Ubuntu 16.04.5 LTS \n \l
檢視mysqld啟動是的預設選項
mysqld --print-defaults
mysqld would have been started with the following arguments:
--user=mysql --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/run/mysqld/mysqld.sock --port=3306 --basedir=/usr --datadir=/var/lib/mysql --tmpdir=/tmp --lc-messages-dir=/usr/share/mysql --skip-external-locking --bind-address=127.0.0.1 --key_buffer_size=16M --max_allowed_packet=16M --thread_stack=192K --thread_cache_size=8 --myisam-recover-options=BACKUP --query_cache_limit=1M --query_cache_size=16M --log_error=/var/log/mysql/error.log --expire_logs_days=10 --max_binlog_size=100M
檢視mysqld啟動配置檔案的優先順序
mysqld --verbose --help|grep -A 1 "default options"
mysqld: Can't change dir to '/var/lib/mysql/' (Errcode: 13 - Permission denied)
--no-defaults Don't read default options from any option file,
except for login file.
--defaults-file=# Only read default options from the given file #.
--defaults-extra-file=# Read this file after the global files are read.
檢視當前的my.cnf配置檔案
sudo mysqld &
ps -ef|grep mysql
root 9758 18990 0 12:50 pts/2 00:00:00 sudo mysqld
liuzy 10745 18990 0 12:51 pts/2 00:00:00 grep --color=auto mysql
ps -ef | grep mysqld
root 9758 18990 0 12:50 pts/2 00:00:00 sudo mysqld
liuzy 12677 18990 0 12:52 pts/2 00:00:00 grep --color=auto mysqld
ps -ef|grep mysql|grep -v grep
root 9758 18990 0 12:50 pts/2 00:00:00 sudo mysqld
service mysql start
need to input root password
接著
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.23-0ubuntu0.16.04.1 (Ubuntu)
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>
ps -ef|grep mysql|grep -v grep
yourname 20232 18990 0 13:18 pts/2 00:00:00 vim mysqld.cnf
mysql 28771 1 0 13:23 ? 00:00:00 /usr/sbin/mysqld
這就是說在Ubuntu中可以用service來啟動mysql服務
那麼我嘗試下用service關閉 mysql服務
service mysql stop
need to input root password
ps -ef|grep mysql|grep -v grep
yourname 20232 18990 0 13:18 pts/2 00:00:00 vim mysqld.cnf