1. 程式人生 > >linux下mysql的解除安裝,安裝,忘記密碼修改密碼,允許遠端連線的設定

linux下mysql的解除安裝,安裝,忘記密碼修改密碼,允許遠端連線的設定

一、linux下mysql的解除安裝

       檢視已經安裝了的MySql的包

             rpm -qa|grep -i mysql

[[email protected] ~]# rpm -qa|grep -i mysql

MySQL-client-5.6.10-1.rhel5.x86_64

MySQL-server-5.6.10-1.linux_glibc2.5.x86_64

[[email protected] ~]#

      解除安裝這些安裝了的包

             rpm -e --nodeps 包名

[[email protected]

 ~]# rpm -e --nodeps MySQL-client-5.6.10-1.rhel5.x86_64

[[email protected] ~]# rpm -e --nodeps MySQL-server-5.6.10-1.linux_glibc2.5.x86_64

[[email protected] ~]#

     刪除老版本mysql的開發標頭檔案和庫

     rm -fr /usr/lib/mysql

     rm -fr /usr/include/mysql       

     注意:解除安裝後/var/lib/mysql中的資料及/etc/my.cnf不會刪除,如果確定沒用後就手工刪除

     rm -f /etc/my.cnf

     rm -fr /var/lib/mysql

[[email protected] ~]# rm -fr /usr/lib/mysql

[[email protected] ~]# rm -fr /usr/include/mysql

[[email protected] ~]# rm -fr /var/lib/mysql

[[email protected] ~]#

二、linux下mysql的安裝

安裝之前需要準備的包

(1)MySQL-server-5.6.10-1.linux_glibc2.5.x86_64.rpm:MySQL伺服器;

(2)MySQL-client-5.6.10-1.rhel5.x86_64.rpm:MySQL客戶端;

(3)MySQL-devel-5.6.10-1.rhel5.x86_64.rpm:Mysql開發依賴包。(可選)

安裝server包

rpm -ivh MySQL-server-5.6.10-1.linux_glibc2.5.x86_64.rpm

出現錯誤conflicts with file from package mariadb-libs,刪除yum remove mysql-libs

FATAL ERROR: please install the following Perl modules before executing /usr/bin/mysql_install_db:

Data::Dumper

yum -y install autoconf

Preparing... ########################################### [100%]

1:MySQL-server ########################################### [100%]

2007-01-02 14:56:12 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).

2007-01-02 14:56:12 13407 [Note] InnoDB: The InnoDB memory heap is disabled

2007-01-02 14:56:12 13407 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins

2007-01-02 14:56:12 13407 [Note] InnoDB: Compressed tables use zlib 1.2.3

2007-01-02 14:56:12 13407 [Note] InnoDB: CPU does not support crc32 instructions

2007-01-02 14:56:12 13407 [Note] InnoDB: Using Linux native AIO

2007-01-02 14:56:12 13407 [Note] InnoDB: Initializing buffer pool, size = 128.0M

2007-01-02 14:56:12 13407 [Note] InnoDB: Completed initialization of buffer pool

2007-01-02 14:56:12 13407 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!

2007-01-02 14:56:12 13407 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB

2007-01-02 14:56:12 13407 [Note] InnoDB: Database physically writes the file full: wait...

2007-01-02 14:56:12 13407 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB

2007-01-02 14:56:13 13407 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB

2007-01-02 14:56:14 13407 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0

2007-01-02 14:56:14 13407 [Warning] InnoDB: New log files created, LSN=45781

2007-01-02 14:56:14 13407 [Note] InnoDB: Doublewrite buffer not found: creating new

2007-01-02 14:56:15 13407 [Note] InnoDB: Doublewrite buffer created

2007-01-02 14:56:15 13407 [Note] InnoDB: 128 rollback segment(s) are active.

2007-01-02 14:56:15 13407 [Warning] InnoDB: Creating foreign key constraint system tables.

2007-01-02 14:56:15 13407 [Note] InnoDB: Foreign key constraint system tables created

2007-01-02 14:56:15 13407 [Note] InnoDB: Creating tablespace and datafile system tables.

2007-01-02 14:56:15 13407 [Note] InnoDB: Tablespace and datafile system tables created.

2007-01-02 14:56:15 13407 [Note] InnoDB: Waiting for purge to start

2007-01-02 14:56:15 13407 [Note] InnoDB: 1.2.10 started; log sequence number 0

A random root password has been set. You will find it in '/root/.mysql_secret'.

2007-01-02 14:56:19 13407 [Note] Binlog end

2007-01-02 14:56:19 13407 [Note] InnoDB: FTS optimize thread exiting.

2007-01-02 14:56:19 13407 [Note] InnoDB: Starting shutdown...

2007-01-02 14:56:21 13407 [Note] InnoDB: Shutdown completed; log sequence number 1625977

2007-01-02 14:56:21 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).

2007-01-02 14:56:21 13430 [Note] InnoDB: The InnoDB memory heap is disabled

2007-01-02 14:56:21 13430 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins

2007-01-02 14:56:21 13430 [Note] InnoDB: Compressed tables use zlib 1.2.3

2007-01-02 14:56:21 13430 [Note] InnoDB: CPU does not support crc32 instructions

2007-01-02 14:56:21 13430 [Note] InnoDB: Using Linux native AIO

2007-01-02 14:56:21 13430 [Note] InnoDB: Initializing buffer pool, size = 128.0M

2007-01-02 14:56:21 13430 [Note] InnoDB: Completed initialization of buffer pool

2007-01-02 14:56:21 13430 [Note] InnoDB: Highest supported file format is Barracuda.

2007-01-02 14:56:21 13430 [Note] InnoDB: 128 rollback segment(s) are active.

2007-01-02 14:56:21 13430 [Note] InnoDB: Waiting for purge to start

2007-01-02 14:56:21 13430 [Note] InnoDB: 1.2.10 started; log sequence number 1625977

2007-01-02 14:56:21 13430 [Note] Binlog end

2007-01-02 14:56:21 13430 [Note] InnoDB: FTS optimize thread exiting.

2007-01-02 14:56:21 13430 [Note] InnoDB: Starting shutdown...

2007-01-02 14:56:23 13430 [Note] InnoDB: Shutdown completed; log sequence number 1625987

A RANDOM PASSWORD HAS BEEN SET FOR THE MySQL root USER !

You will find that password in '/root/.mysql_secret'.

You must change that password on your first connect,

no other statement but 'SET PASSWORD' will be accepted.

See the manual for the semantics of the 'password expired' flag.

Also, the account for the anonymous user has been removed.

In addition, you can run:

/usr/bin/mysql_secure_installation

which will also give you the option of removing the test database.

This is strongly recommended for production servers.

See the manual for more instructions.

Please report any problems with the /usr/bin/mysqlbug script!

The latest information about MySQL is available on the web at

Support MySQL by buying support/licenses at http://shop.mysql.com

WARNING: Found existing config file /usr/my.cnf on the system.

Because this file might be in use, it was not replaced,

but was used in bootstrap (unless you used --defaults-file)

and when you later start the server.

The new default config file was created as /usr/my-new.cnf,

please compare it with your file and take the changes you need.

[[email protected] software]#

安裝client包

rpm -ivh MySQL-client-5.6.10-1.rhel5.x86_64.rpm

Preparing... ########################################### [100%]

1:MySQL-client ########################################### [100%]

[[email protected] software]#

至此就算安裝完成了

啟動mysql

service mysql start

Starting MySQL... ERROR! The server quit without updating PID file (/var/lib/mysql/zyoub.pid).

mysql_install_db --user=mysql --ldata=/var/lib/mysql/

[[email protected] software]# service mysql start

Starting MySQL. [ OK ]

[[email protected] software]#

登入mysql

 登陸MySql的命令是mysql,mysql 的使用語法如下:

mysql [-u username] [-h host] [-p[password]] [dbname]

username 與 password 分別是 MySQL 的使用者名稱與密碼,mysql的初始管理帳號是root,沒有密碼,注意:這個root使用者不是Linux的系統使用者。MySQL預設使用者是root,由於初始沒有密碼,第一次進時只需鍵入mysql即可

有可能我是之前安裝過的原因還是什麼其他原因,無論我是用mysql -u root -p 登入還是隻輸入mysql都進不去,報如下錯誤

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

好像是密碼正確,但我又不知道初始密碼是什麼(據說是空 '' 但是我試過了不行),於是我想到了忘記密碼修改密碼。

三、忘記密碼修改密碼

停止mysql服務

service mysql stop

[[email protected] software]# service mysql stop

Shutting down MySQL.. [ OK ]

[[email protected] software]#

修改/usr/my.cnf

vi /usr/my.cnf

在[mysqld]的段中加上一句:skip-grant-tables  

例如:

[mysqld]

# Remove leading # and set to the amount of RAM for the most important data

# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.

# innodb_buffer_pool_size = 128M

# Remove leading # to turn on a very important data integrity option: logging

# log_bin

# These are commonly set, remove the # and set as required.

# basedir = .....

# datadir = .....

# port = .....

# server_id = .....

# socket = .....

skip-grant-tables

儲存退出

重新啟動mysqld 

[[email protected] software]# service mysql start

Starting MySQL. [ OK ]

[[email protected] software]#

登入並修改MySQL的root密碼 

這時只輸入mysql就能登入進來

進入mysql庫

mysql> use mysql ; 

Reading table information for completion of table and column names

Database changed

mysql>

修改密碼

update user SET Password = password ( 'new-password' ) WHERE User = 'root' ; (new-password為新密碼,我這裡是root)

mysql> update user SET Password = password ( 'root' ) WHERE User = 'root' ;

Query OK, 4 rows affected (0.01 sec)

Rows matched: 4 Changed: 4 Warnings: 0

mysql>

重新整理MySQL的系統許可權相關表,否則會出現拒絕訪問

flush privileges ;

mysql> flush privileges ;

Query OK, 0 rows affected (0.00 sec)

mysql>

退出

mysql> exit

Bye

[[email protected] software]#

將/usr/my.cnf該回原樣

vi /usr/my.cnf

將剛才新增的那句skip-grant-tables刪除或註釋

儲存退出

重新啟動mysql

[[email protected] software]# service mysql restart

Shutting down MySQL.. [ OK ]

Starting MySQL. [ OK ]

[[email protected] software]#

至此以可以正常使用msyql了

但是本機上可以使用,有的同學遠端連線就不行,總是報“不允許連線到mysql伺服器”

這其實是沒有允許遠端連線的問題

四、設定允許遠端連線

用root登入mysql

 [[email protected] software]# mysql -u root -p 

Enter password:

輸入密碼登入mysql

第一次登陸有可能提示You must SET PASSWORD before executing this statement

用mysql> SET PASSWORD = PASSWORD('root');這句話重新設定一次密碼就可以了(這裡的root是我的密碼,你可以自己隨便定)

進入mysql庫

mysql> use mysql

Reading table information for completion of table and column names

Database changed

mysql>

將user表中的host欄位為localhost且user欄位為root的那條記錄的host改成%

mysql> update user set host = '%' where host = 'localhost' and user = 'root';

Query OK, 1 row affected (0.01 sec)

Rows matched: 1 Changed: 1 Warnings: 0

重啟服務

[[email protected] software]# service mysql restart

Shutting down MySQL.. [ OK ]

Starting MySQL. [ OK ]

[[email protected] software]#

OK現在遠端連線就可以了