所有在服務器上執行的命令,都在 # 後面
1、命令安裝mysql
# yum install mysql mysql-server mysql-devel -y
最後提示 Complete! 表示安裝成功
2、查看是否生成了mysqld服務, 並設置隨機啓動
# chkconfig --list |grep mysql
數字代碼服務器啓動級別,off 代表不隨機啓動mysqld服務,on代表隨機啓動服務
我們需要設置mysqld隨機啓動,執行下面命令進行設置
# chkconfig mysqld on
這樣的結果代表正常
# chkconfig --list |grep mysql
3、啓動mysqld服務
執行如下命令進行啓動,兩種方法都可以:
# /etc/init.d/mysqld start
# service mysqld start
啓動後,ps一下,看下進程是否起來
# ps -ef |grep mysql|grep -v grep
root 1582 1 0 23:26 pts/0 00:00:00 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --socket=/var/lib/mysql/mysql.sock --pid-file=/var/run/mysqld/mysqld.pid --basedir=/usr --user=mysql
mysql 1684 1582 1 23:26 pts/0 00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
根據進程信息可以看到,mysql的數據庫data目錄是 /var/lib/mysql ,錯誤日誌文件是 /var/log/mysqld.log
查看都有哪些庫
# cd /var/lib/mysql
# ls -l
發現有兩個庫,都是mysql默認自帶的,如何手動創建數據庫,會在後續的教程中說明。
查看佔用端口,默認佔用3306端口
# netstat -nutlp | grep mysql
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1684/mysqld
4、停止mysqld服務
執行如下命令進行停止,兩種方法都可以:
# /etc/init.d/mysqld stop
# service mysqld stop
5、重啓mysqld服務
執行如下命令進行重啓,兩種方法都可以:
# /etc/init.d/mysqld restart
# service mysqld srestart
6、使用root登錄時遇到了ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)錯誤. 如下所示
原因:Mysql 5.6及以後版本出處於安全考慮,root密碼已經不爲空了。
解決方法:
首先停掉mysql服務器
[root@DB-Server init.d]# /etc/rc.d/init.d/mysql stop
Shutting down MySQL..[ OK ]
然後使用mysqld_safe命令在啓動mysql,更新root賬號的密碼
--skip-grant-tables:不啓動grant-tables(授權表),跳過權限控制。
--skip-networking :跳過TCP/IP協議,只在本機訪問(從網上有些資料看,這個選項不是必須的。可以不用)
[root@DB-Server init.d]# mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
[2] 9396
[root@DB-Server init.d]# 140722 14:59:46 mysqld_safe Logging to '/var/lib/mysql/DB-Server.err'.
140722 14:59:46 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
執行上面命令後,此會話窗口會出現無反應的狀態,需要使用CTRL+C中斷會話,檢查/var/lib/mysql/DB-Server.err日誌,發現也無其它異常信息。沒有弄明白該會話窗口爲啥會出現無響應狀態。
tail -200 /var/lib/mysql/DB-Server.err | more
2014-07-22 14:59:41 9346 [Note] Shutting down plugin 'binlog'
2014-07-22 14:59:41 9346 [Note] /usr/sbin/mysqld: Shutdown complete
140722 14:59:41 mysqld_safe mysqld from pid file /var/lib/mysql/DB-Server.pid ended
140722 14:59:46 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
2014-07-22 14:59:47 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timesta
mp server option (see documentation for more details).
2014-07-22 14:59:47 9516 [Note] Plugin 'FEDERATED' is disabled.
2014-07-22 14:59:47 9516 [Note] InnoDB: Using atomics to ref count buffer pool pages
2014-07-22 14:59:47 9516 [Note] InnoDB: The InnoDB memory heap is disabled
2014-07-22 14:59:47 9516 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2014-07-22 14:59:47 9516 [Note] InnoDB: Compressed tables use zlib 1.2.3
2014-07-22 14:59:47 9516 [Note] InnoDB: Using Linux native AIO
2014-07-22 14:59:47 9516 [Note] InnoDB: Using CPU crc32 instructions
2014-07-22 14:59:47 9516 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2014-07-22 14:59:47 9516 [Note] InnoDB: Completed initialization of buffer pool
2014-07-22 14:59:47 9516 [Note] InnoDB: Highest supported file format is Barracuda.
2014-07-22 14:59:47 9516 [Note] InnoDB: 128 rollback segment(s) are active.
2014-07-22 14:59:47 9516 [Note] InnoDB: Waiting for purge to start
2014-07-22 14:59:47 9516 [Note] InnoDB: 5.6.19 started; log sequence number 1626087
2014-07-22 14:59:47 9516 [Note] /usr/sbin/mysqld: ready for connections.
Version: '5.6.19' socket: '/var/lib/mysql/mysql.sock' port: 0 MySQL Community Server (GPL)
[root@DB-Server init.d]# mysql -u root mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.19 MySQL Community Server (GPL)
Copyright (c) 2000, 2014, 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> select Host, User, Password,password_expired
-> from user
-> where user='root' and host='root' or host='localhost';
+-----------+------+-------------------------------------------+------------------+
| Host | User | Password | password_expired |
+-----------+------+-------------------------------------------+------------------+
| localhost | root | *A848DE7CCD839E924921BEE41711991DDA0D529E | Y |
+-----------+------+-------------------------------------------+------------------+
1 row in set (0.00 sec)
mysql> update user set password=PASSWORD('p12#456')
-> where user='root' and host='root' or host='localhost';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
新設置用戶或更改密碼後需用flush privileges刷新MySQL的系統權限相關表,否則會出現拒絕訪問,還有一種方法,就是重新啓動mysql服務器,來使新設置生效。
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
[root@DB-Server init.d]# /etc/rc.d/init.d/mysql restart
Shutting down MySQL..140722 15:02:27 mysqld_safe mysqld from pid file /var/lib/mysql/DB-Server.pid ended
[ OK ]
Starting MySQL.[ OK ]
[2]- Done mysqld_safe --user=mysql --skip-grant-tables --skip-networking
[root@DB-Server init.d]# mysql -u root p
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@DB-Server init.d]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.19
Copyright (c) 2000, 2014, 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數據庫後執行腳本遭遇 ERROR 1820 (HY000): You must SET PASSWORD before executing this statement,可以使用重新設置一次密碼即可解決問題.
mysql>set password = password('p12#456');
至此問題解決,但是還有不少地方有些疑惑,爲啥出現這個錯誤? 其實這麼多資料都只是說了解決方法,但是都回避了問題的原因。
參考資料:
http://huangyifa163.blog.163.com/blog/static/262875752011127102215790/
http://sundful.iteye.com/blog/704337
http://wenku.baidu.com/view/735ffa41be1e650e52ea995a.html
http://www.cnblogs.com/sunson/articles/2172086.html
http://blog.163.com/eric1945@126/blog/static/164934572201081494343373/
http://www.cnblogs.com/likai198981/archive/2013/04/06/3002518.html