linux下mysql卸载,安装,忘记密码修改密码,允许远程连接的设置
一、linux下mysql的卸载
查看已经安装了的MySql的包
rpm -qa|grep -i mysql
[root@localhost ~]# 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
[root@localhost ~]#
卸载这些安装了的包
rpm -e --nodeps 包名
[root@localhost ~]# rpm -e --nodeps MySQL-client-5.6.10-1.rhel5.x86_64
[root@localhost ~]# rpm -e --nodeps MySQL-server-5.6.10-1.linux_glibc2.5.x86_64
[root@localhost ~]#
删除老版本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
[root@localhost ~]# rm -fr /usr/lib/mysql
[root@localhost ~]# rm -fr /usr/include/mysql
[root@localhost ~]# rm -f /etc/my.cnf
[root@localhost ~]# rm -fr /var/lib/mysql
[root@localhost ~]#
二、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
[root@localhost software]# rpm -ivh MySQL-server-5.6.10-1.linux_glibc2.5.x86_64.rpm
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
http://www.mysql.com
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.
[root@localhost software]#
安装client包
rpm -ivh MySQL-client-5.6.10-1.rhel5.x86_64.rpm
[root@localhost software]# rpm -ivh MySQL-client-5.6.10-1.rhel5.x86_64.rpm
Preparing... ########################################### [100%]
1:MySQL-client ########################################### [100%]
[root@localhost 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/
[root@localhost software]# service mysql start
Starting MySQL. [ OK ]
[root@localhost 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
[root@localhost software]# service mysql stop
Shutting down MySQL.. [ OK ]
[root@localhost 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
[root@localhost software]# service mysql start
Starting MySQL. [ OK ]
[root@localhost 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
[root@localhost software]#
将/usr/my.cnf该回原样
vi /usr/my.cnf
将刚才添加的那句skip-grant-tables删除或注释
保存退出
重新启动mysql
[root@localhost software]# service mysql restart
Shutting down MySQL.. [ OK ]
Starting MySQL. [ OK ]
[root@localhost software]#
至此以可以正常使用msyql了
但是本机上可以使用,有的同学远程连接就不行,总是报“不允许连接到mysql服务器”
这其实是没有允许远程连接的问题
四、设置允许远程连接
用root登录mysql
[root@localhost 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
重启服务
[root@localhost software]# service mysql restart
Shutting down MySQL.. [ OK ]
Starting MySQL. [ OK ]
[root@localhost software]#
OK现在远程连接就可以了