在服务器中安装配置MySQL环境以及在Windows中使用navicat远程链接。
网上的安装教程很多,但是需要用到软件的时候,我第一个想到的还是conda,先用conda为MySQL创建一个干净的环境。然后查一下conda install mysql
一看返回结果,是可以用conda安装MySQL的。这比有的教程上来先让我们查一下服务Linux的发行版和版本,然后对着MySQL官网找安装包简单多了啊。
安装
conda activate mysql
conda install -c anaconda mysql
查看安装路径:
which mysql
../miniconda3/envs/mysql/bin/mysql
cd ../miniconda3/envs/mysql/bin # which 出来的文件路径 mysql的上一层
查看帮助文档:
mysql --help
mysql Ver 8.0.22 for Linux on x86_64 (conda-forge)
Copyright (c) 2000, 2020, 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.
Usage: mysql [OPTIONS] [database]
-?, --help Display this help and exit.
-I, --help Synonym for -?
--auto-rehash Enable automatic rehashing. One doesn't need to use
'rehash' to get table and field completion, but startup
and reconnecting may take a longer time. Disable with
--disable-auto-rehash.
(Defaults to on; use --skip-auto-rehash to disable.)
-A, --no-auto-rehash
./mysqlcheck
./mysqlcheck Ver 8.0.22 for Linux on x86_64 (conda-forge)
Copyright (c) 2000, 2020, 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.
这个文件很长,比较重要的几句话:
Default options are read from the following files in the given order:
/etc/my.cnf /etc/mysql/my.cnf ../miniconda3/envs/mysql/etc/my.cnf ~/.my.cnf
The following groups are read: mysqlcheck client
The following options may be given as the first argument:
--print-defaults Print the program argument list and exit.
--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 #.
可以看出my.cnf
是我们配置的主要文件之一。
ll /etc/my.cnf /etc/mysql/my.cnf ../miniconda3/envs/mysql/etc/my.cnf ~/.my.cnf
# 大部分是木有的,我们自己手动创建一个。
touch ../miniconda3/envs/mysql/etc/my.cnf
在网上找到几个模板比较一下,选择自己喜欢的,然后根据自己的mysql路径环境来配置,主要是替换路径名。然后执行初始化,这一步一般会报很多错,因为我们的配置是没有专业的人类指导。不过别拍,哪里错了就查哪里。
[转载]my.cnf 详解与优化
mysql之my.cnf详解
数据库的信息配置信息都在里面,在创建的时候遇到没有的路径先在安装路径下找找,没有就mkdir
创建一个, 没有的文件就先touch
一个。争取先进入MySQL的界面再说。
cd ../miniconda3/envs/mysql/bin
./mysqld --initialize-insecure # 初始化
2020-11-04T03:02:15.343794Z 0 [Warning] [MY-010139] [Server] Changed limits: max_open_files: 1024 (requested 8161)
2020-11-04T03:02:15.343811Z 0 [Warning] [MY-010142] [Server] Changed limits: table_open_cache: 431 (requested 4000)
2020-11-04T03:02:15.344383Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2020-11-04T03:02:15.344418Z 0 [Warning] [MY-011068] [Server] The syntax '--language/-l' is deprecated and will be removed in a future release. Please use '--lc-messages-dir' instead.
2020-11-04T03:02:15.345105Z 0 [System] [MY-013169] [Server] /miniconda3/envs/mysql/bin/mysqld (mysqld 8.0.22) initializing of server in progress as process 27658
2020-11-04T03:02:15.345280Z 0 [Warning] [MY-010339] [Server] Using pre 5.5 semantics to load error messages from /miniconda3/envs/mysql/share/mysql/english/. If this is not intended, refer to the documentation for valid usage of --lc-messages-dir and --language parameters.
2020-11-04T03:02:15.359349Z 0 [Warning] [MY-010122] [Server] One can only use the --user switch if running as root
2020-11-04T03:02:15.368886Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2020-11-04T03:02:16.423693Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2020-11-04T03:02:18.400100Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
初始化之后,查看mysql server 状态
./mysql.server
# Usage: mysql.server {start|stop|restart|reload|force-reload|status} [ MySQL server options ]
./mysql.server status
# MySQL is not running [失败]
用vi 打开./mysql.server
配置;里面的basedir
和datadir
。注意basedir是MySQL的安装路径不是上面我们which 出来的软件文件路径。这可以参考my.cnf
的配置。
在反复的调试过程中,需要不断查看mysql的进程,因为如果有的进程已经存在,任务无法重启,连试一试的机会都没有。
ps aux |grep mysql
这个任务可以用kill -9 pid
来杀除
配置好之后,启动MySQL的服务。启动MySQL有四种方式:
./mysql.server start
Starting MySQL.. [ 确定 ]
这基本上就可以了,我们启动MySQL。
mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.22 conda-forge
Copyright (c) 2000, 2020, 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默认没有密码,安装完毕增加密码的重要性是不言而喻的。
1、命令
usr/bin/mysqladmin -u root password 'new-password'
格式:mysqladmin -u用户名 -p旧密码 password 新密码
2、例子
例1:给root加个密码123456。
键入以下命令 :
[root@test1 local]# /usr/bin/mysqladmin -u root password 123456 本文来自无涯教程网:http://www.wuyapc.com
注:因为开始时root没有密码,所以-p旧密码一项就可以省略了。
当然也可以在mysql中修改密码。
use mysql;
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'new password';
FLUSH PRIVILEGES;
在 mysql 中 FLUSH PRIVILEGES;
为刷新权限 ,每次更改之后需要这样处理下才能更新。
设置别的服务器登陆的权限。这里推荐改表法,简单。一般是我们的帐号不允许从远程登陆,只能在localhost。这个时候只要在localhost的那台电脑,登入mysql后,更改"mysql" 数据库里的 "user" 表里的 "host"项,从"localhost"改称"%"
mysql -u root -p vmware
mysql>usemysql;
mysql>update user set host = '%' where user ='root';
mysql>select host, user from user;
mysql> select host, user from user;
+-----------+------------------+
| host | user |
+-----------+------------------+
| % | root |
| localhost | mysql.infoschema |
| localhost | mysql.session |
| localhost | mysql.sys |
+-----------+------------------+
4 rows in set (0.00 sec)
以上,为我们用Navicat远程登陆MySQL做好了准备。
在用Navicat登陆远程的服务器时,这里有两个选项都是需要填的常规
和SSH
。常规里面的信息试试数据库的用户名密码等。
这里需要注意的是,这里的root指的时数据库的root,而不是服务器的,许多人一看这个root就认为自己没有服务器root权限就不能安装和配置MySQL,这种想法是不对的。
SSH
中是我们用来登陆服务器的。需要让Navicat知道我们的MySQL数据库在哪里部署着。
这里留下一个思考题: 同一台服务器中同一个人,可以安装来两个不同的MySQL吗?如果可以的话,Navicat如何识别呢?
可能会遇到的问题以及解决办法。办法就是把报错信息复制到浏览器里面去,查看国际同行在面临该问题时给出的解决办法。
解决Navicat 报错:1130-host is not allowed MySQL不允许从远程访问的方法
https://blog.csdn.net/liangkaiping0525/article/details/88848420
mysql8 :客户端连接caching-sha2-password问题
如此这般,我们填好信息之后,基本上就可以连接了。
这样,我们就可以在windows的Navicat 或者 Linux的MySQL来操作自己的数据库了。
https://www.inmotionhosting.com/support/website/databases/connect-database-navicat/